文件和目录操作命令
Linux核心命令
命令在线查询网站http://linux.51yip.com/search/
1.文件和目录操作
ls
ls -hl # 列出详细信息并以可读⼤⼩显示⽂件⼤⼩
ls --human-readable --size -1 -S --classify # 按⽂件⼤⼩排序
du -sh * | sort -h # 按⽂件⼤⼩排序
ll -i # 可以显示文件node号
[root@VM-4-3-centos work]# ll -i
total 36
795607 -rw-r--r-- 1 root root 4528 Aug 11 13:13 !
786840 drwxr-xr-x 7 root root 4096 Aug 11 11:53 back-end
795575 -rw-r--r-- 1 root root 339 Aug 11 11:40 default.conf
795611 -rw-r--r-- 1 root root 1711 Aug 11 13:01 docker-compose.yml
786888 drwxr-xr-x 7 root root 4096 Aug 11 11:52 front-end
786952 drwxr-xr-x 10 root root 4096 Sep 3 11:54 KeepHealthy
787047 drwxr-xr-x 4 root root 4096 Aug 3 23:44 mysql
787033 -rw-r--r-- 1 root root 515 Aug 3 15:00 README.md
[root@VM-4-3-centos work]# find ./ -inum 795607| xarg rm -rf {}
-bash: xarg: command not found
[root@VM-4-3-centos work]# find ./ -inum 795607| xargs rm -rf {}
find
用的多的 -mtime<24⼩时数>:查找在指定时间曾被更改过的⽂件或⽬录,单位以24⼩时计算;
-mmin<分钟>:查找在指定时间曾被更改过的⽂件或⽬录,单位以分钟计算;
-amin<分钟>:查找在指定时间曾被存取过的⽂件或⽬录,单位以分钟计算
-atime<24⼩时数>:查找在指定时间曾被存取过的⽂件或⽬录,单位以24⼩时计算;
-cmin<分钟>:查找在指定时间之时被更改过的⽂件或⽬录;
-ctime<24⼩时数>:查找在指定时间之时被更改的⽂件或⽬录,单位以24⼩时计算;
-depth:从指定⽬录下最深层的⼦⽬录开始查找;
-empty:寻找⽂件⼤⼩为0 Byte的⽂件,或⽬录下没有任何⼦⽬录或⽂件的空⽬录;
-exec<执⾏指令>:假设find指令的回传值为True,就执⾏该指令;
-maxdepth<⽬录层级>:设置最⼤⽬录层级;
-perm<权限数值>:查找符合指定的权限数值的⽂件或⽬录;
-size<⽂件⼤⼩>:查找符合指定的⽂件⼤⼩的⽂件;
-type<⽂件类型>:只寻找符合指定的⽂件类型的⽂件;
touch
touch test_{1..100}
## 批量创建文件名
[root@VM-4-3-centos ~]# cat filename | xargs touch
[root@VM-4-3-centos ~]# ls
back-end core.7 default.conf docker-compose.yml filename front-end KeepHealthy mysql README.md
file
[root@VM-4-3-centos file]# file *
1: ASCII text, with very long lines
1.sh: Bourne-Again shell script, ASCII text executable
tree
[root@VM-4-3-centos ~]# tree -L 1
.
├── back-end
├── core.7
├── default.conf
├── docker-compose.yml
├── file
├── filename
├── front-end
├── KeepHealthy
├── mysql
└── README.md
1 directory, 9 files
[root@VM-4-3-centos ~]#
[root@VM-4-3-centos ~]# tree -L 2
.
├── back-end
├── core.7
├── default.conf
├── docker-compose.yml
├── file
│ ├── 1
│ └── 1.sh
├── filename
├── front-end
├── KeepHealthy
├── mysql
└── README.md
chattr
## 给脚本加一个不能修改的权限
[root@VM-4-3-centos file]# chattr +i 1.sh
#让某个⽂件只能往⾥⾯追加内容,不能删除,⼀些⽇志⽂件适⽤于这种操作:
chattr +a /data1/user_act.log
md5sum
[root@VM-4-3-centos file]# md5sum 1.sh > 1.sh-md5.txt
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]# md5sum 1.sh -c 1.sh-md5.txt
md5sum: 1.sh: no properly formatted MD5 checksum lines found
1.sh: OK
cat
## 配合重定向使用
[root@VM-4-3-centos file]# cat 1.sh
#!/bin/bash
echo shell
[root@VM-4-3-centos file]# cat > 1.sh <<EOF
> 1
> 2
> 3
> EOF
-bash: 1.sh: Permission denied
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]#
[root@VM-4-3-centos file]# chattr -i 1.sh
[root@VM-4-3-centos file]# cat > 1.sh <<EOF
1
2
3
EOF
[root@VM-4-3-centos file]# cat 1.sh
1
2
3
head
## 显示文件头几行
[root@VM-4-3-centos file]# cat /etc/passwd |head -n 4 |tail -n 1
adm:x:3:4:adm:/var/adm:/sbin/nologin
sort
## 文件排序
[root@VM-4-3-centos file]# seq 10 > 3.txt
[root@VM-4-3-centos file]# cat 3.txt
1
2
3
4
5
6
7
8
9
10
[root@VM-4-3-centos file]# cat 3.txt | sort -nr
10
9
8
7
6
5
4
3
2
1
uniq
## 去重
[root@180-143 test]# cat test
1 2 3
1 2 3
[root@180-143 test]# uniq test
1 2 3
正则表达式
^ # 锚定⾏的开始 如:'^grep'匹配所有以grep开头的⾏。
$ # 锚定⾏的结束 如:'grep$' 匹配所有以grep结尾的⾏。
. # 匹配⼀个⾮换⾏符的字符 如:'gr.p'匹配gr后接⼀个任意字符,然后是p。
* # 匹配零个或多个先前字符 如:'*grep'匹配所有⼀个或多个空格后紧跟grep的⾏。
.* # ⼀起⽤代表任意字符。
[] # 匹配⼀个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
[^] # 匹配⼀个不在指定范围内的字符,如:'[^A-Z]rep' 匹配不包含 A-Z 中的字⺟开头,紧跟 rep 的⾏
\(..\) # 标记匹配字符,如'\(love\)',love被标记为1。
\< # 锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的⾏。
\> # 锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的⾏。
x\{m\} # 重复字符x,m次,如:'0\{5\}'匹配包含5个o的⾏。
x\{m,\} # 重复字符x,⾄少m次,如:'o\{5,\}'匹配⾄少有5个o的⾏。
x\{m,n\} # 重复字符x,⾄少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的⾏。
\w # 匹配⽂字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个⽂字或数字字符,然后
是p。
\W # \w的反置形式,匹配⼀个或多个⾮单词字符,如点号句号等。
\b # 单词锁定符,如: '\bgrep\b'只匹配grep。
grep
[root@VM-4-3-centos log]# netstat -antp | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1717/sshd
tcp 0 52 10.0.4.3:22 171.41.83.164:63787 ESTABLISHED 4711/sshd: root@pts
## -v 参数排除
tar
tar -czvf ar_test.tar.gz ar_test #将ar_test文件打包并且压缩
tar zxvf ar_test.tar.gz #解压
free
[root@VM-4-3-centos file]# free -h
total used free shared buff/cache available
Mem: 1.8G 266M 75M 528K 1.5G 1.3G
Swap: 0B 0B 0B