实验目的:熟练掌握获取命令帮助的常用方法,以便于及时了解与服务器管理、维护相关和其他命令的用法

实验需求:

查看及切换目录:pwd、cd、ls、du

创建目录和文件:mkdir、touch、ln

复制、删除、移动目录和文件:cp、rm、mv

查找目录和文件:which、find


1、熟练掌握获取命令帮助的常用方法

使用help命令

[root@localhost ~]#  help pwd
pwd: pwd [-LP]
打印当前工作目录的名字。
选项:
-L 打印 $PWD 变量的值,如果它命名了当前的
工作目录
-P 打印当前的物理路径,不带有任何的符号链接
默认情况下,`pwd' 的行为和带 `-L' 选项一致
退出状态:
除非使用了无效选项或者当前目录不可读,否则
返回状态为0。

使用“--help”选项

[root@localhost ~]# 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 严重问题 (例如:无法使用命令行参数)

使用man手册页

[root@localhost ~]# man file
FILE(1) General Commands Manual FILE(1)

NAME
file - 确定文件类型

总览
file [ -bcnsvzL ] [ -f 命名文件 ] [ -m 幻数文件 ] file ...

描述
本手册页说明了3.27版本 file 命令的使用. File 命令试图检查每个参数以判定文件的类型.
检查共有三组,按如下顺序进行:文件系统检查,幻数检查,以及语言检查. 文件系统
检查成功则输出文件类型.

输出的类型一般会包含以下的词中的一个: text (文件中仅有 ASCII 字符,可以用 ASCII
终端读此文件,以保证内容的可靠性), executable (文件中保存的是程序编译后的结果,一些UNIX
内核或其它内核能理解这类文件), 或者 data 表示所有其它类型文件(data
一般为二进制文件或者不可打印的).
但是有的常用的文件格式(如core文件、tar包),虽然也包含二进制数据, 却不属于这一类
如果要修改 /usr/share/magic 或者程序本身, preserve these keywords . 当文件为``text''
类型时,认为此文件为可读文件. 不要象在Berkeley环境中那样做 - 要把``shell commands
text''改为``shell script''.

使用man工具查看man本身的手册页信息,并保存为man.txt文本文件

[root@localhost ~]# man man | col -b > man.txt



2、查看及切换目录:pwd、cd、ls、du

查看当前的工作目录---pwd

[root@localhost ~]# pwd
/root

切换工作目录---cd

[root@localhost ~]# cd /boot/grub
[root@localhost grub]# pwd
/boot/grub
[root@localhost grub]# pwd
/boot/grub
[root@localhost grub]# cd ~
[root@localhost ~]# cd -
/boot/grub
[root@localhost grub]#

列表显示目录内容---ls

[root@localhost grub]# ls
splash.xpm.gz
[root@localhost grub]# ls -ld
drwxr-xr-x. 2 root root 27 6月 24 16:18 .
[root@localhost grub]# ls -lh /etc/yum.conf /boot/vmlinuz-3.10.0-957.el7.x86_64 
-rwxr-xr-x. 1 root root 6.4M 11月 9 2018 /boot/vmlinuz-3.10.0-957.el7.x86_64
-rw-r--r--. 1 root root 970 11月 5 2018 /etc/yum.conf
[root@localhost grub]# ls -lh /etc/ns*.conf
-rw-r--r--. 1 root root 1.8K 6月 24 16:22 /etc/nsswitch.conf

统计目录及文件的空间占用情况---du


[root@localhost ~]# du -sh /var/log
6.1M /var/log
[root@localhost ~]# du -ah /boot/
4.0K /boot/efi/EFI/centos/BOOT.CSV
4.0K /boot/efi/EFI/centos/BOOTX64.CSV
1.2M /boot/efi/EFI/centos/MokManager.efi
1.2M /boot/efi/EFI/centos/mmx64.efi
1.2M /boot/efi/EFI/centos/shim.efi
1.2M /boot/efi/EFI/centos/shimx64-centos.efi
//下略

3、创建目录和文件:mkdir、touch、ln

创建新的目录---mkdir

创建单个目录

[root@localhost ~]# mkdir public_html
[root@localhost ~]# ls -d public_html
public_html

创建多个目录(-p)

[root@localhost ~]# mkdir -p /multimedia/movie/cartoon
[root@localhost ~]# ls -R /multimedia
/multimedia:
movie

/multimedia/movie:
cartoon

/multimedia/movie/cartoon:

创建空文件---touch

[root@localhost ~]# cd /multimedia/movie/cartoon
[root@localhost cartoon]# touch HuaMulan.rmvv NeZhaNaoHai.mp4
[root@localhost cartoon]# ls -lh
总用量 0
-rw-r--r-- 1 root root 0 6月 27 16:13 HuaMulan.rmvv
-rw-r--r-- 1 root root 0 6月 27 16:13 NeZhaNaoHai.mp4

创建链接文件---ln

基本使用格式:ln 【-s】 源文件或目录    链接文件或目标位置

[root@localhost ~]# mkdir /bin/wx
[root@localhost ~]# ln -s /bin/bzip2 /bin/wx
[root@localhost ~]# ls -lh /bin/wx/bzip2
lrwxrwxrwx 1 root root 10 6月 27 16:16 /bin/wx/bzip2 -> /bin/bzip2

4、复制、删除、移动目录和文件:cp、rm、mv

复制文件或目录---cp

基本格式:cp 【选项】   源文件或目录   目标文件或目录

将/bin/touch 命令程序复制到当下目录下,并命名为mytouch,另外将/etc/yum.conf 文件复制一份作为备份,添加.bak扩展名,仍存放在/etc目录中

[root@localhost ~]# cp /bin/touch ./mytouch
[root@localhost ~]# cp /etc/yum.conf /etc/yum.conf.bak
[root@localhost ~]# cp -r /boot/grub/ /etc/host.conf  public_html/
[root@localhost ~]# ls public_html/
grub host.conf

删除文件或目录---rm

基本格式:rm 【选项】   源文件或目录   目标文件或目录

[root@localhost ~]# rm -rf public_html/grub/
[root@localhost ~]# ls public_html/
host.conf
[root@localhost ~]# rm -i public_html/host.conf 
rm:是否删除普通文件 "public_html/host.conf"?y
[root@localhost ~]# ls public_html/
[root@localhost ~]#

移动文件或目录---mv

基本格式:mv 【选项】   源文件或目录   目标文件或目录

[root@localhost ~]# mv mytouch mkfile
[root@localhost ~]# ls -lh mytouch mkfile
ls: 无法访问mytouch: 没有那个文件或目录
-rwxr-xr-x 1 root root 62K 6月 27 16:19 mkfile
[root@localhost ~]# mv mkfile public_html/
[root@localhost ~]# ls -l public_html/mkfile
-rwxr-xr-x 1 root root 62568 6月 27 16:19 public_html/mkfile

5、查找目录和文件:which、find

[root@localhost ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which history
/usr/bin/which: no history in (/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin)

查找文件或目录---find

基本格式:find 【查找范围】 【查找条件表达式】

[root@localhost ~]# find /etc -name "resol*.conf"
/etc/resolv.conf
[root@localhost ~]# find /boot -type d
/boot
/boot/efi
/boot/efi/EFI
/boot/efi/EFI/centos
/boot/efi/EFI/centos/fw
/boot/efi/EFI/BOOT
/boot/grub2
/boot/grub2/i386-pc
/boot/grub2/locale
/boot/grub2/fonts
/boot/grub
[root@localhost ~]# find /boot -size +1024k -a -name "vmlinuz*"
/boot/vmlinuz-3.10.0-957.el7.x86_64
/boot/vmlinuz-0-rescue-483dc869e1474b3b8fae4787f9a3fa7e
[root@localhost ~]# find /boot -size +1024k -o -name "vmlinuz*"
/boot/efi/EFI/centos/MokManager.efi
/boot/efi/EFI/centos/mmx64.efi
/boot/efi/EFI/centos/shim.efi
/boot/efi/EFI/centos/shimx64-centos.efi
/boot/efi/EFI/centos/shimx64.efi
/boot/efi/EFI/BOOT/BOOTX64.EFI
/boot/grub2/fonts/unicode.pf2
/boot/System.map-3.10.0-957.el7.x86_64
/boot/vmlinuz-3.10.0-957.el7.x86_64
/boot/initramfs-0-rescue-483dc869e1474b3b8fae4787f9a3fa7e.img
/boot/vmlinuz-0-rescue-483dc869e1474b3b8fae4787f9a3fa7e
/boot/initramfs-3.10.0-957.el7.x86_64.img