常用命令操作
1.在 Linux 操作系统中,凡是在字符操作界面中输入的能够完成特定操作和任务的字符串都可以称为命令。严格来说,命令通常只代表了实现某一类功能的指令或程序的名称
一、Linux命令概述
1.命令分类
- 内部命令:是指集成于 Shell 解释器程序(如 Bash)内部的一些特殊指令,也称为内建(Built-in)指令。内部命令属于 Shell 的一部分,所以没有单独对应的系统文件,只要Shell 解释器被运行,内部指令也就自动载入内存了,用户可以直接使用。内部命令无须从硬盘中重新读取文件,因此执行效率较高。
- 外部命令:是指 Linux 操作系统中能够完成特定功能的脚本文件或二进制程序,每个外部命令对应系统中的一个文件,是属于 Shell 解释器程序之外的命令,所以称为外部命令。Linux 操作系统必须知道外部命令对应的文件位置,才能够由 Shell加载并执行。
2.命令行格式
命令字 选项 参数
- 命令字:即命令名称
- 选项:作用是调节命令的具体功能,决定这条命令如何执行
- 使用单个字符的选项时,一般在选项前使用“-”符号(半角的减号符)引导,称为短格式选项,如“-l”。多个单字符选项可以组合在一起使用,如“-al”等同于“-a -l”。
- 使用多个字符的选项时,一般在选项前使用“—”符号(两个半角的减号符)引导,称为长格式选项,如“—help”。
- 参数:命令参数是命令字的处理对象,通常情况下命令参数可以是文件名、目录(路径)名或用户名等内容。根据所使用命令字的不同,命令参数的个数可以是零到多个。
3.获得命令帮助
-
使用help命令
[root@CentOS7-4 ~]# help pwd pwd: pwd [-LP] 打印当前工作目录的名字。 选项: -L 打印 $PWD 变量的值,如果它命名了当前的 工作目录 -P 打印当前的物理路径,不带有任何的符号链接 默认情况下,`pwd' 的行为和带 `-L' 选项一致 退出状态: 除非使用了无效选项或者当前目录不可读,否则 返回状态为0。
-
使用 “ —help ” 选项
[root@CentOS7-4 ~]# ls --help 用法:ls [选项]... [文件]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified. Mandatory arguments to long options are mandatory for short options too. -a, --all 不隐藏任何以. 开始的项目 -A, --almost-all 列出除. 及.. 以外的任何项目 --author 与-l 同时使用时列出每个文件的作者 -b, --escape 以八进制溢出序列表示不可打印的字符 --block-size=SIZE scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below -B, --ignore-backups do not list implied entries ending with ~ -c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and sort by name; otherwise: sort by ctime, newest first -C list entries by columns --color[=WHEN] colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below -d, --directory list directories themselves, not their contents -D, --dired generate output designed for Emacs' dired mode -f do not sort, enable -aU, disable -ls --color -F, --classify append indicator (one of */=>@|) to entries --file-type likewise, except do not append '*' --format=WORD across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C --full-time like -l --time-style=full-iso -g 类似-l,但不列出所有者 --group-directories-first group directories before files; can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping -G, --no-group 以一个长列表的形式,不输出组名 -h, --human-readable 与-l 一起,以易于阅读的格式输出文件大小 (例如 1K 234M 2G) --si 同上面类似,但是使用1000 为基底而非1024 -H, --dereference-command-line follow symbolic links listed on the command line --dereference-command-line-symlink-to-dir follow each command line symbolic link that points to a directory --hide=PATTERN do not list implied entries matching shell PATTERN (overridden by -a or -A) --indicator-style=WORD append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F) -i, --inode print the index number of each file -I, --ignore=PATTERN do not list implied entries matching shell PATTERN -k, --kibibytes default to 1024-byte blocks for disk usage -l 使用较长格式列出信息 -L, --dereference 当显示符号链接的文件信息时,显示符号链接所指示 的对象而并非符号链接本身的信息 -m 所有项目以逗号分隔,并填满整行行宽 -n, --numeric-uid-gid 类似 -l,但列出UID 及GID 号 -N, --literal 输出未经处理的项目名称 (如不特别处理控制字符) -o 类似 -l,但不列出有关组的信息 -p, --indicator-style=slash 对目录加上表示符号"/" -q, --hide-control-chars print ? instead of nongraphic characters --show-control-chars show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal) -Q, --quote-name enclose entry names in double quotes --quoting-style=WORD use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape -r, --reverse 逆序排列 -R, --recursive 递归显示子目录 -s, --size 以块数形式显示每个文件分配的尺寸 -S sort by file size --sort=WORD sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X) --time=WORD with -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c); also use specified time as sort key if --sort=time --time-style=STYLE with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is interpreted like in 'date'; if FORMAT is FORMAT1<newline>FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with 'posix-', STYLE takes effect only outside the POSIX locale -t sort by modification time, newest first -T, --tabsize=COLS assume tab stops at each COLS instead of 8 -u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access time -U do not sort; list entries in directory order -v natural sort of (version) numbers within text -w, --width=COLS assume screen width instead of current value -x list entries by lines instead of by columns -X sort alphabetically by entry extension -1 list one file per line SELinux options: --lcontext Display security context. Enable -l. Lines will probably be too wide for most displays. -Z, --context Display security context so it fits on most displays. Displays only mode, user, group, security context and file name. --scontext Display only security context and file name. --help 显示此帮助信息并退出 --version 显示版本信息并退出 SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000). 使用色彩来区分文件类型的功能已被禁用,默认设置和 --color=never 同时禁用了它。 使用 --color=auto 选项,ls 只在标准输出被连至终端时才生成颜色代码。 LS_COLORS 环境变量可改变此设置,可使用 dircolors 命令来设置。 退出状态: 0 正常 1 一般问题 (例如:无法访问子文件夹) 2 严重问题 (例如:无法使用命令行参数) GNU coreutils online help: <http://www.gnu.org/software/coreutils/> 请向<http://translationproject.org/team/zh_CN.html> 报告ls 的翻译错误 要获取完整文档,请运行:info coreutils 'ls invocation'
-
使用man手册页
[root@CentOS7-4 ~]# man file
4.管道和重定向案例
[root@CentOS7-4 ~]# man ls | col -b > lshelp.txt
#col -b 过滤控制字符
[root@CentOS7-4 ~]# ls
anaconda-ks.cfg lsman.txt
[root@CentOS7-4 ~]# cat lsman.txt
- 管道:用竖杠符号“|”表示,用于将前面命令的屏幕输出结果作为后面命令的操作对象(输入)。就好像一根管道一样,从这头塞入内容,从另一头出来
- 重定向输出:用大于符号“>”表示,前面的命令成功执行以后,其屏幕输出结果将保存到“>”号后边指定的文件中,而不是直接输出到屏幕,因此称为重定向
二、目录和文件基本操作
1.查看及切换目录
-
pwd命令(查看当前的工作目录(Print Working Directory))
[root@CentOS7-4 ~]# pwd/root
-
cd 命令(切换工作目录(Change Directory))
[root@CentOS7-4 ~]# cd /boot/grub[root@CentOS7-4 grub]# pwd/boot/grub
-
绝对路径:这种方式以根目录“/”作为起点,如“/boot/grub”表示根目录下 boot子目录中的 grub 目录
[root@CentOS7-4 ~]# cd /boot/grub
-
相对路径:这种方式一般以当前工作目录作为起点,在开头不使用“/”符号,因此使用的时候更加简短、便捷
[root@CentOS7-4 lhost apache-tomcat-8.5.16]# cd conf/ [root@CentOS7-4 conf]# pwd /usr/local/apache-tomcat-8.5.16/conf
[root@CentOS7-4 conf]# cd ../../ [root@CentOS7-4 local]# pwd /usr/local
[root@CentOS7-4 ~]# cd ~zhangsan [root@CentOS7-4 zhangsan]# pwd /home/zhangsan
-
ls 命令(列表(List)显示目录内容)
-l 选项:以长格式(Long)显示文件和目录的列表,包括权限、大小、最后更新时间等详细信息 -a 选项:显示所有(All)子目录和文件的信息,包括名称以点号“.”开头的隐藏目录和隐藏文件 -A 选项:与-a 选项的作用基本类似,但有两个特殊隐藏目录不会显示,即表示当前目录的“.”和表示父目录的“..” -d 选项:显示目录(Directory)本身的属性,而不是显示目录中的内容 -h 选项:以更人性化(Human)的方式显示目录或文件的大小,默认的大小单位为字节(B),使用-h 选项后将显示为 KB、MB 等单位。此选项需要结合-l 选项一起使用 -R 选项:以递归(Recursive)的方式显示指定目录及其子目录中的所有内容
[root@CentOS7-4 ~]# ls anaconda-ks.cfg lsman.txt
[root@CentOS7-4 ~]# ls -lh /etc/yum.conf /boot/vmlinuz-3.10.0-1062.el7.x86_64 -rwxr-xr-x. 1 root root 6.5M 8月 8 2019 /boot/vmlinuz-3.10.0-1062.el7.x86_64 -rw-r--r--. 1 root root 970 8月 8 2019 /etc/yum.conf
[root@CentOS7-4 ~]# ls -lh /etc/ns*.conf -rw-r--r--. 1 root root 2.0K 12月 20 04:33 /etc/nsswitch.conf
-
du 命令(统计目录及文件的空间占用情况(Disk Usage))
-a:统计磁盘空间占用时包括所有的文件,而不仅仅只统计目录 -h:以更人性化的方式(默认以 KB 计数,但不显示单位)显示出统计结果,使用-h选项后将显示出 KB、MB 等单位 -s:只统计每个参数所占用空间总的(Summary)大小,而不是统计每个子目录、文件的大小
[root@CentOS7-4 ~]# du -sh /var/log 3.3M /var/log
[root@CentOS7-4 ~]# du -ah /root/ 4.0K /root/.bash_logout 4.0K /root/.bash_profile 4.0K /root/.bashrc 4.0K /root/.cshrc 4.0K /root/.tcshrc 4.0K /root/anaconda-ks.cfg 9.0M /root/apache-tomcat-8.5.16.tar.gz 4.0K /root/.bash_history 8.0K /root/.viminfo 9.1M /root/
2.创建目录及文件
-
mkdir 命令(创建新的目录(Make Directory))
[root@CentOS7-4 ~]# mkdir public_html [root@CentOS7-4 ~]# ls -d public_html/ public_html/
#一次创建多级目录使用 -p 选项 [root@CentOS7-4 ~]# mkdir -p /1/2/3/4/5 [root@CentOS7-4 ~]# ls -R /1/ /1/: 2 /1/2: 3 /1/2/3: 4 /1/2/3/4: 5 /1/2/3/4/5:
-
touch命令(创建空文件)
[root@CentOS7-4 ~]# touch 1.txt [root@CentOS7-4 ~]# ls -lh 1.txt -rw-r--r-- 1 root root 0 3月 28 21:25 1.txt
-
ln 命令(创建链接(Link)文件)
-
软链接:又称为符号链接,删除原始文件后,软链接将失效,软链接适用于文件或目录,软链接与原始文件可以位于不同的文件系统中,使用 “ -s ”选项
-
硬链接:默认创建硬链接,删除原始文件后,硬链接仍然可用,硬链接只可用于文件,硬链接必须与原始文件在同一个文件系统(如一个Linux 分区)
[root@CentOS7-4 ~]# ln -s 1.txt /opt/1.txt [root@CentOS7-4 ~]# ls -lhd /opt/1.txt lrwxrwxrwx 1 root root 5 3月 28 21:26 /opt/1.txt -> 1.txt
3.复制、删除、移动目录和文件
-
cp 命令(复制(Copy)文件或目录)
-f:覆盖目标同名文件或目录时不进行提醒,而直接强制(Force)复制 -i:覆盖目标同名文件或目录时提醒用户确认(Interactive,交互式) -p:复制时保持(Preserve)源文件的权限、属主及时间标记等属性不变 -r:复制目录时必须使用此选项,表示递归复制所有文件及子目录
[root@CentOS7-4 ~]# cp /bin/touch /mytouch
root@CentOS7-4 ~]# cp -r /boot/grub/ /etc/host.conf /huos/
[root@CentOS7-4 ~]# ls -ld /home/zhangsan/zhangsan.txt -rw-rw-r--. 1 zhangsan zhangsan 0 12月 21 00:04 /home/zhangsan/zhangsan.txt [root@CentOS7-4 ~]# cp -p /home/zhangsan/zhangsan.txt /root/ [root@CentOS7-4 ~]# ls -ld zhangsan.txt -rw-rw-r--. 1 zhangsan zhangsan 0 12月 21 00:04 zhangsan.txt
-
rm 命令(删除(Remove)文件或目录)
-f:删除文件或目录时不进行提醒,而直接强制删除 -i:删除文件或目录时提醒用户确认 -r:删除目录时必须使用此选项,表示递归删除整个目录树(应谨慎使用)
[root@CentOS7-4 ~]# rm -rf public_html/ [root@CentOS7-4 ~]# rm -rf /* #删除整个根目录(慎用)
-
mv 命令(移动(Move)文件或目录)
[root@CentOS7-4 ~]# mv /1.txt /2.txt
[root@CentOS7-4 ~]# mv /1.txt /root/ [root@CentOS7-4 ~]# ls -ld /root/1.txt -rw-r--r--. 1 root root 16 12月 20 17:20 /root/1.txt
4.查找目录和文件
-
which 命令(查找用户所执行的命令文件存放的目录)
[root@CentOS7-4 ~]# which ls alias ls='ls --color=auto' /usr/bin/ls [root@CentOS7-4 ~]# which vi /usr/bin/vi
#当查找内部命令时就会出现无法搜索到命令文件目录 [root@CentOS7-4 ~]# which history #history命令显示最近使用的历史命令 /usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
-
find 命令(查找文件或目录)
-
按名称查找:关键字为“-name”,根据目标文件的名称进行查找,允许使用 “ * ”及 “ ? ”通配符。
-
按文件大小查找:关键字为“-size”,根据目标文件的大小进行查找,一般使用“+”和“-”号设置超过或小于指定的大小作为查找条件。常用的容量单位包括 kB(注意 k 是小写)、MB、GB。
-
按文件属主查找:关键字为“-user”,根据文件是否属于目标用户进行查找。
-
按文件类型查找:关键字为“-type”,根据文件的类型进行查找,这里的类型指的是普通文件(f)、目录(d)、块设备文件(b)、字符设备文件(c)等。块设备指的是成块读取数据的设备(如硬盘、内存等),而字符设备指的是按单个字符读取数据的设备(如键盘、鼠标等)
[root@CentOS7-4 ~]# find /etc –name "resol*.conf" /etc/resolv.conf
[root@CentOS7-4 ~]# find /boot/ -type d /boot/ /boot/efi /boot/efi/EFI /boot/efi/EFI/centos /boot/grub2 /boot/grub2/i386-pc /boot/grub2/locale /boot/grub2/fonts /boot/grub
三、Linux的关机与重启
1.关机
[root@CentOS7-4 ~]# poweroff
[root@CentOS7-4 ~]# shutdown -h now
[root@CentOS7-4 ~]# shutdown -h +10
[root@CentOS7-4 ~]# shutdown -r 23:30
[root@CentOS7-4 ~]# init 0
2.重启
[root@CentOS7-4 ~]# reboot #立刻重启
[root@CentOS7-4 ~]# shutdown -r now #立刻重启
[root@CentOS7-4 ~]# shutdown -r +10 #10分钟之后重启
[root@CentOS7-4 ~]# shutdown -r 23:30 #23:30分之后重启
[root@CentOS7-4 ~]# init 6
3.取消定时关机或重启
[root@CentOS7-4 ~]# shutdown -c
Broadcast message from root@CentOS7-4.localdomain (Tue 2021-12-21 00:35:36 CST):
The system shutdown has been cancelled at Tue 2021-12-21 00:36:36 CST!
4.init命令
-
语法
init(选项)(参数)
-
选项
-b:不执行相关脚本而直接进入单用户模式; -s:切换到单用户模式
-
参数
运行等级:指定Linux系统要切换到的运行等级
-
实例
几个常用的命令
查看系统进程命令:
ps -ef | head
查看init的配置文件:more /etc/inittab
查看系统当前运行的级别:runlevel
-
运行级别
到底什么是运行级呢?简单的说,运行级就是操作系统当前正在运行的功能级别。这个级别从0到6 ,具有不同的功能。你也可以在
/etc/inittab
中查看它的英文介绍
#0 停机(千万不能把initdefault 设置为0)
#1 单用户模式
#2 多用户,没有 NFS(和级别3相似,会停止部分服务)
#3 完全多用户模式
#4 没有用到
#5 x11(Xwindow)
#6 重新启动(千万不要把initdefault 设置为6)