Print name of current/Working Directory
显示当前所在(工作)的目录
[root@VM-12-4-centos network]# pwd
/root/network
Change the current Directory
更改文件夹,即进入某个文件夹
# 进入当前文件夹的config目录(用相对路径)
[root@VM-12-4-centos network]# cd config/
[root@VM-12-4-centos config]#
# 进入家目录的network2文件夹(用绝对路径)
[root@VM-12-4-centos config]# cd ~/network2/
[root@VM-12-4-centos network2]#
# 返回当前用户家目录
[root@VM-12-4-centos network2]# cd
[root@VM-12-4-centos ~]#
MaKe DIRectories,创建目录
# 创建config2目录
[root@VM-12-4-centos network]# mkdir config2
[root@VM-12-4-centos network]# ls
1.txt config config2
# 创建config3/template目录(config3不存在的情况)
# 直接mkdir创建报错,报错原因为config3不存在,所以无法创建template
[root@VM-12-4-centos network]# mkdir config3/template
mkdir: cannot create directory ‘config3/template’: No such file or directory
# 使用-p参数,作用为递归创建,即先创建config3,再在config3下创建template
[root@VM-12-4-centos network]# mkdir -p config3/template
[root@VM-12-4-centos network]# ls config3
template
对于不存在的文件进行创建,对于已存在的文件修改时间戳(创建时间、访问时间或更新时间等)
# 创建一个普通文件
[root@VM-12-4-centos network]# touch 2.txt
[root@VM-12-4-centos network]# ll
-rw-r--r-- 1 root root 0 Jan 20 09:13 2.txt
# 将已有文件1.txt的创建时间更新
# 仅会更新1.txt的时间,不会变更文件内容
[root@VM-12-4-centos network]# ll
-rw-r--r-- 1 root root 6 Jan 13 09:52 1.txt
[root@VM-12-4-centos network]# touch 1.txt
[root@VM-12-4-centos network]# ll
-rw-r--r-- 1 root root 6 Jan 20 09:15 1.txt
# 使用-a、-m参数可以更改文件的访问时间、修改时间,用得不多,不作介绍
LiSt
用于列出文件夹下的内容
#列出当前文件夹下的内容
[root@VM-12-4-centos network]# ls
1.txt config
#以竖向的格式列出,注 ls -l == ll,直接用ll命令也可实现
[root@VM-12-4-centos network]# ls -l
total 8
-rw-r--r-- 1 root root 6 Jan 13 09:30 1.txt
drwxr-xr-x 2 root root 4096 Jan 13 09:30 config
#列出所有文件(含隐藏文件)
[root@VM-12-4-centos network]# ls -la
total 16
drwxr-xr-x 3 root root 4096 Jan 13 09:32 .
dr-xr-x---. 13 root root 4096 Jan 13 09:29 ..
-rw-r--r-- 1 root root 6 Jan 13 09:30 1.txt
-rw-r--r-- 1 root root 0 Jan 13 09:32 .2.txt
drwxr-xr-x 2 root root 4096 Jan 13 09:30 config
#列出config下所有文件,使文件的大小带上单位
[root@VM-12-4-centos network]# ls -lha config/
total 148M
drwxr-xr-x 2 root root 4.0K Jan 13 09:30 .
drwxr-xr-x 3 root root 4.0K Jan 13 09:32 ..
-rw-r--r-- 1 root root 148M Jan 13 09:30 jdk-7u80-linux-i586.tar.gz
#列出文件夹所有内容,并按创建时间排序,创建时间从最新到最老
[root@VM-12-4-centos network]# ll -at
total 16
drwxr-xr-x 3 root root 4096 Jan 13 09:32 .
-rw-r--r-- 1 root root 0 Jan 13 09:32 .2.txt
drwxr-xr-x 2 root root 4096 Jan 13 09:30 config
-rw-r--r-- 1 root root 6 Jan 13 09:30 1.txt
dr-xr-x---. 13 root root 4096 Jan 13 09:29 ..
#列出文件夹所有内容,并按创建时间排序,创建时间从最老到最新,参数r为取反
[root@VM-12-4-centos network]# ll -atr
total 16
dr-xr-x---. 13 root root 4096 Jan 13 09:29 ..
-rw-r--r-- 1 root root 6 Jan 13 09:30 1.txt
drwxr-xr-x 2 root root 4096 Jan 13 09:30 config
-rw-r--r-- 1 root root 0 Jan 13 09:32 .2.txt
drwxr-xr-x 3 root root 4096 Jan 13 09:32 .
#列出当前文件夹属性
[root@VM-12-4-centos network]# ls -ld
drwxr-xr-x 3 root root 4096 Jan 13 09:32 .
Concatenate files And print on the standard outpuT
浏览文件内容并打印
# 浏览文件内容
[root@VM-12-4-centos network]# cat 1.txt
it is line 1
it is line 3
# 带行号输出文件内容
[root@VM-12-4-centos network]# cat -n 1.txt
1 it is line 1
2
3 it is line 3
# 带行号输出文件内容(不计算空行的行号)
[root@VM-12-4-centos network]# cat -b 1.txt
1 it is line 1
2 it is line 3
Sort lines of text files
排序文件行,sort的排序原理是从每行首字符向后按ASCII值比较,简单理解a-z,A-Z,0-9排序,0-9在a-z前,a-z在A-Z前
# 查看文件当前内容
[root@VM-12-4-centos network]# cat 3.txt
13
2
55
115
abc
bc
abd
# 按行排序文件,从小到大
# 0-9在a-z前,故数字在字母前
# 尤其要注意的,sort默认不是按照数字大小排序,如115排在了13前
# 115与13的比较:先比较第一位,都是1,再比较第二位,1与3,显然1的ASCII值小,故115排在13前面
[root@VM-12-4-centos network]# sort 3.txt
115
13
2
55
abc
abd
bc
# 按行排序文件,其中数字行按实际数字大小从小到大排序
[root@VM-12-4-centos network]# sort -n 3.txt
abc
abd
bc
2
13
55
115
# 按行排序文件,从大到小
[root@VM-12-4-centos network]# sort -r 3.txt
bc
abd
abc
55
2
13
115
# 按行排序文件,先去重,再从小到大输出
[root@VM-12-4-centos network]# cat 4.txt
111
111
2222
1111
22222
[root@VM-12-4-centos network]# sort -u 4.txt
111
1111
2222
22222
# 按每行的第三列排序
# 需确保每一行都有第三列(即有一个特征符号能划分列)
# 例如下列文件,每行分列的特征符号为"."
[root@VM-12-4-centos network]# cat 5.txt
1.2.3.4
1.5.4
1.1.8
[root@VM-12-4-centos network]# sort -t '.' -k 3 5.txt
1.2.3.4
1.5.4
1.1.8
# -t '.' 通过.划分列
# -k 3 按第三列排序行