Linux命令的执行过程和命令类型

Linux命令一般都是由shell来执行(shell是个程序但在Linux中一切皆文件)shell命令分为内部命令和外部命令,通过type这个命令来查询其他命令是不是内部命令
范例:第二条命令用来查询shell的大小
[root@xia ~]# echo $SHELL /bin/bash [root@xia ~]# ll /bin/bash -rwxr-xr-x. 1 root root 1219248 Nov 9 2019 /bin/bash [root@xia ~]# type echo echo is a shell builtin
外部命令 范例:
[root@xia ~]# type hostname hostname is hashed (/usr/bin/hostname) [root@xia ~]# ll /usr/bin/hostname -rwxr-xr-x. 1 root root 21664 May 11 2019 /usr/bin/hostname
查看命令和切换至外部命令 (第二条直接查看是否有外部命令和内部命令)
[root@xia ~]# type echo echo is a shell builtin [root@xia ~]# type -a echo echo is a shell builtin echo is /usr/bin/echo [root@xia ~]# which echo /usr/bin/echo
查看所有的内部命令:enable
禁用某个内部命令:enable -n [想要禁用的命令名] 使用的时候会使用外部命令
启用某个内部命令:enable [想要禁用的命令名]
查看所有的禁用命令:enable -n
[root@xia ~]# enable -n echo [root@xia ~]# enable -n enable -n echo [root@xia ~]# enable echo [root@xia ~]# enable -n [root@xia ~]#
查看是否为内部命令并查看存放外部命令的文件夹
[root@xia ~]# type hostname hostname is hashed (/usr/bin/hostname) [root@xia ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@xia ~]#
查看内部命令的执行次数例:
[root@xia ~]# hash hits command 1 /usr/bin/tty 5 /usr/bin/hostname 1 /usr/bin/lsblk 2 /usr/bin/cat 2 /usr/bin/ls 1 /usr/sbin/ip
hash缓存:hash -r name 清除名为name的记录
hash -r 清楚所有记录
hash -l 显示hash缓存
alias定义别名 范例:
[root@xia ~]# alias f='free -h' [root@xia ~]# f total used free shared buff/cache available Mem: 1.8Gi 307Mi 989Mi 9.0Mi 503Mi 1.3Gi Swap: 2.0Gi 0B 2.0Gi
命令执行的优先级:别名>内部命令>外部命令 例:(临时)
[root@xia ~]# alias free='free -h' [root@xia ~]# free total used free shared buff/cache available Mem: 1.8Gi 318Mi 977Mi 9.0Mi 504Mi 1.3Gi Swap: 2.0Gi 0B 2.0Gi [root@xia ~]# type free free is aliased to free -h'
持久改:[root@xia ~]# echo "alias free='free -h'" >> .bashrc
如果别名和原命令重名可用以下方法执行原命令:用unalias free、/free、'free'、"free" 删除所有别名:unalias -a 范例:[root@xia ~]# unalias free
[root@xia ~]# free
total used free shared buff/cache available
Mem: 1843864 326320 1001200 9324 516344 1348984
Swap: 2097148 0 2097148
查看已经定义的别名:范例[root@xia ~]# cat .bashrc

.bashrc

User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Source global definitions

if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias free='free -h'
alias osversion="cat /etc/os-release"
命令的格式:COMMAND [OPTIONS...] [ARGUMANTS...] COMMAND:命令名 内部命令或外部命令 OPTIONS:选项, Unix风格选项:-a -i GUN风格选项:--all --human BSD风格选项:a ARGUMANTS:参数, 命令的作用对象比如文件名 用户名 范例:[root@xia ~]# uname -r
4.18.0-193.el8.x86_64
[root@xia ~]# uname -h
uname: invalid option -- 'h'
Try 'uname --help' for more information.
[root@xia ~]# uname --help
Usage: uname [OPTION]...
Print certain system information. With no OPTION, same as -s.

-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
--help display this help and exit
--version output version information and exit

GNU coreutils online help: https://www.gnu.org/software/coreutils/
Full documentation at: https://www.gnu.org/software/coreutils/uname
or available locally via: info '(coreutils) uname invocation'
[root@xia ~]# uname --kernel-release
4.18.0-193.el8.x86_64
[root@xia ~]# ps aux
id命令,id越小权限越大 范例:[root@xia ~]# id -u
0
xiaxiangming@ubuntu-1804:~$ id -u
1000
下面是一个完整的命令格式:[root@xia ~]# id -u xiaxiangming
1000
ctrl+c退出和bc计算器 范例[root@xia ~]# bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type warranty'. 2^3 8 obase=2 1287 10100000111 ^C (interrupt) Exiting bc. [root@xia ~]#

常用命令

date:查看时间,date软件时间,clock硬件时间
[root@xia ~]# date Sun Jun 21 09:54:31 CST 2020 [root@xia ~]# clock 2020-06-21 09:54:36.334729+08:00
修改系统时间并参照硬件时间恢复系统时间,范例:
[root@xia ~]# date Sun Jun 21 09:59:45 CST 2020 [root@xia ~]# date -s '1 year' Mon Jun 21 10:00:24 CST 2021 [root@xia ~]# date Mon Jun 21 10:00:31 CST 2021 [root@xia ~]# clock -s [root@xia ~]# date Sun Jun 21 10:00:42 CST 2020
参照系统时间修改硬件时间,范例
[root@xia ~]# date -s '1 year' Mon Jun 21 10:03:27 CST 2021 [root@xia ~]# clock -w [root@xia ~]# clock 2021-06-21 10:03:39.742089+08:00 [root@xia ~]# date -s '-1 year' Sun Jun 21 10:03:57 CST 2020 [root@xia ~]# clock -w [root@xia ~]# clock 2020-06-21 10:04:05.835092+08:00
时区查看
[root@xia ~]# ll /etc/localtime lrwxrwxrwx. 1 root root 35 Jun 17 16:32 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
查看日历cal
`[root@xia ~]# cal
June 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

[root@xia ~]# cal y
cal: failed to parse timestamp or unknown month name: y
[root@xia ~]# cal -y
2020
[root@xia ~]# cal 2021
[root@xia ~]# cal 9 1752
September 1752
Su Mo Tu We Th Fr Sa
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
关机重启 关机:halt 、poweroff、inti 0 重启:reboot ,-f:强制,-p:切断电源 关机加重启:shutdown,shutdown [option] [time] [message] 0ption:-r重启 -h关机 重启定时并取消重启范例;[root@xia ~]# shutdown -r +5
Shutdown scheduled for Sun 2020-06-21 10:39:57 CST, use 'shutdown -c' to cancel.
[root@xia ~]# date
Sun Jun 21 10:35:09 CST 2020
[root@xia ~]# shutdown -c
查看开机时长:uptime[root@xia ~]# uptime
10:39:13 up 51 min, 1 user, load average: 0.00, 0.00, 0.00
查看用户信息 whoami:查看当前用户 who:查看所有用户 w:查看用户及其操作 文本编辑 nano 开一个新窗口执行ping命令 关了窗口后ping命令停止执行。解决方法:使用虚拟窗口 在centos或者Ubuntu上面运行screen命令如果显示未发现文件,输入命令yum install screen 输入两次确定安装完成 运行screen打开虚拟窗口。在虚拟窗口中运行命令关闭窗口后命令也不会停止执行 运行screen命令后关闭窗口之前root 3637 0.0 0.0 128552 1272 pts/4 S+ 09:44 0:00 ping 192.168.174.130
root 3639 0.0 0.0 155472 1860 pts/2 R+ 09:44 0:00 ps aux
关闭后root 3637 0.0 0.0 128552 1272 pts/4 S+ 09:44 0:00 ping 192.168.174.130
root 3639 0.0 0.0 155472 1860 pts/2 R+ 09:44 0:00 ps aux
可以看到他的运行进程依然在运行。 那么怎么返回之前运行的界面例:[root@xiaxiangming ~]# screen -ls
There are screens on:
4395.pts-5.xiaxiangming (Attached)
3603.pts-3.xiaxiangming (Attached)
3147.pts-2.xiaxiangming (Attached)
3 Sockets in /var/run/screen/S-root.

[root@xiaxiangming ~]# screen -r 4395
`
screen还可以开启远程协助
a通过xshell连接到服务器比如192.168.174.130,这时它遇到问题要请求别人帮忙,就需要别人(b)也通过xshell连接到192.168.174.130,这时a运行screen -S help ,b运行screen -x help这样双方就可以看到对方的操作。退出操作使用命令CTRL+a 送开按b

echo:表示回显
[root@xia ~]# echo I love you I love you
-n表示不换行还可以用后面加c来显示,两个命令写入一行可以使用封号隔开。 范例
[root@xia ~]# echo -n "I love you" I love you[root@xia ~]# I love you[root@xia ~]# echo -e "I love youc" I love you[root@xia ~]# [root@xia ~]# echo -n "I love you" ;echo -n "I love you me too" I love youI love you me too[root@xia ~]#
回车和换行,r表示回车 n表示换行,t表示插入tab。范例:
[root@xia ~]# echo -e 'aaaaaa bbb' bbbaaa [root@xia ~]# echo -e 'aaaaaa bbb' aaaaaa bbb [root@xia ~]# echo -e 'aaaaaa bbb' aaaaaa bbb

原文地址:https://www.cnblogs.com/xiaxiangming/p/13170493.html