1.进入某个目录下使用cd,可使用绝对路径和相对路径

1)绝对路径
cd /data
2)相对路径,进入到往上两层的目录下
cd ../../
3)相对路径,进入到当前目录的test子目录下
cd ./test

2.ls

#-l 以list模式显示出来
#-r 反向排序
#-s 显示文件的大小
#-t 按修改时间排序显示
#-R 递归显示目录下所有文件和目录
#-a显示隐藏文件和上层目录(.,..)
#-F 如果是文件在后面显示斜杠

3.mkdir

#创建一个文件夹
mkdir test
#递归创建,如果已经存在了直接跳过
mkdir -p /test1/test2/test3

4.find命令

1)查找系统根目录下的某个文件
find / -name test.txt 
2)按类型查找,使用-type,type后面可以跟文件,跟目录使用d
find / -name test.txt -type f
find / -name test.txt -type d
3)按修改时间查询
#当日
find / -name *.txt -mtime -1
#20天
find / -name *.txt -mtime +20
4)按大小查找
#单位为K
find / -name *.txt -mtime +20k
#单位为M
find / -name *.txt -mtime +20M
5)按权限查找,系统权限有10位,第一位代表是目录还是文件(d表示目录,-表示文件),第2到第4位是当前用户的权限,第5位到第7位是用户组的权限,第8位到第10位是当前用户之外用户的权限,用rwx分别表示读写执行的权限,r编码为数字4,w编码为数字2,x编码为1,例:drwxrw-rw-,表示一个766权限的目录
#查找根目录下权限为755的txt文件
find / -name *.txt -perm 755
6)查找到根目录下所有文件修改权限,-exec可以把find找到的内容作为后面{}里面的参数处理。

find / -type f -exec chmod -R 755 {} \;

5.grep命令

#找文件里的内容
grep 'root' /etc/passwd
#加颜色,--color
grep --color 'root' /etc/passwd
#加行号,-n
grep -n --color 'root' /etc/passwd
#以什么开头,^
grep -n --color '^root' /etc/passwd
#以什么结尾,$
grep -n --color 'root$' /etc/passwd
#取反,-v
grep -v grep test.txt
#去除空行,
grep -v "^$" test.txt
#正则匹配
1)匹配一个数字
grep "[0-9]" test.txt
2)匹配连续两个数字
grep "[0-9][0-9]" test.txt
3)匹配多次,比如匹配一个出现1到3次的数字后面加一个小数点的行
egrep "[0-9]{1,3}" test.txt
#[0-9]表示匹配一个数字,{1,3}表示匹配1到3次,反斜杠表示转译,中括号表示匹配的范围,[^1-9]里面的尖符号表示不匹配1-9开头
egrep "([0-9]{1,3}\.){3}" test.txt
4)统计,-c
##统计以“test”字符开头的行数为多少
grep -c 'test' test.txt

6.awk,主要用来统计

1)打印列,
#打印第一列
awk "{print $1}" /etc/passwd
#打印最后一列,使用-F指定分隔符
awk -F: "{print $NF}" /etc/passwd

7.sed

-n :只打印模式匹配的行
#只打印第2行
sed -n '2p' test.txt
#默认打印所有行,匹配到指定的行后再打印一遍,所以匹配到的行会打印两遍
sed '2p' test.txt
#打印指定行
sed -n '1,3p' test.txt
#打印匹配到字符的行
sed -n '/test/p' test.txt
#正则匹配
sed'5 q'/etc/passwd#打印前5行  
sed -n '/r*t/p'/etc/passwd#打印匹配r有0个或者多个,后接一个t字符的行  
sed -n '/.r.*/p'/etc/passwd#打印匹配有r的行并且r后面跟任意字符  
sed -n '/o*/p'/etc/passwd#打印o字符重复任意次  
sed -n '/o\{1,\}/p'/etc/passwd#打印o字重复出现一次以上  
sed -n '/o\{1,3\}/p'/etc/passwd#打印o字重复出现一次到三次之间以上
#增加一行打印,默认情况下sed使用的-e,并不会修改文件内容,只是将修改后的内容打印出来而已
sed '1a best' test.txt
#字符替换
sed 's/要替换的字符串/新的字符串/g' test.txt

对每行匹配到的第一个字符串进行替换
sed -i '1s/原字符串/新字符串/' test.txt

对全局匹配上的所有字符串进行替换
sed -i 's/原字符串/新字符串/g' test.txt 

删除所有匹配到字符串的行
sed -i '/匹配字符串/d'  test.txt 

特定字符串的行后插入新行
sed -i '/特定字符串/a 新行字符串' test.txt 

特定字符串的行前插入新行
sed -i '/特定字符串/i 新行字符串' test.txt

把匹配行中的某个字符串替换为目标字符串
sed -i '/匹配字符串/s/源字符串/目标字符串/g' test.txt

在文件ab.txt中的末行之后,添加bye
sed -i '$a bye' test.txt   

对于文件第3行,把匹配上的所有字符串进行替换
sed -i '3s/原字符串/新字符串/g' test.txt 
#同时执行两个替换规则,用引号包括所有规则,不同规则使用分号进行分隔
sed 's/^/添加的头部&/g;s/$/&添加的尾部/g' test.txt
#删除指定行,只是打印,并不会真的删除,加上-i就真的删除了
sed '2d' test.txt

8.cut使用方法

1)指定域的分隔符用-d 
#指定使用逗号做分隔符,取第2个域和第3到第5个域
cut -d"," -f 2,3-5 test.txt
2)按字节截取
#-b,一个汉字按2个字符截取
#-c,一个汉字按一个字符截取
[root@host130 ~]# cut -b 1-4 test.txt 
test
我
[root@host130 ~]# cut -c 1-2 test.txt 
te
我

9.软硬连接

1)创建软连接
#ln -s test.txt test1.txt
[root@host130 ~]# ls -lrt test*
-rw-r--r-- 1 root root 66 May 24 14:32 test.txt
lrwxrwxrwx 1 root root  8 May 24 14:43 test1.txt -> test.txt
2)创建硬连接
#ln test.txt test2.txt
[root@host130 ~]# ls -lrt test*        
-rw-r--r-- 2 root root 66 May 24 14:32 test.txt
-rw-r--r-- 2 root root 66 May 24 14:32 test2.txt
lrwxrwxrwx 1 root root  8 May 24 14:43 test1.txt -> test.tx
3)修改源文件
[root@host130 ~]# echo "whoami" > test.txt 
[root@host130 ~]# cat test.txt 
whoami
[root@host130 ~]# cat test1.txt 
whoami
[root@host130 ~]# cat test2.txt  
whoami
4)删除源文件
[root@host130 ~]# rm test.txt 
rm: remove regular file ‘test.txt’? y
[root@host130 ~]# ls -lrt test*
-rw-r--r-- 1 root root 7 May 24 14:44 test2.txt
lrwxrwxrwx 1 root root 8 May 24 14:51 test1.txt -> test.txt
[root@host130 ~]# cat test2.txt 
whoami
[root@host130 ~]# cat test1.txt 
cat: test1.txt: No such file or directory

树枝挂个大皮球
7 声望0 粉丝