当前位置:首页> 正文

ls和find命令查找的一些小技巧

ls和find命令查找的一些小技巧

看到老男孩老师的博客有一篇是要写用三种方法查找修改文件;想来想去后面回去看一下ls和find命令的使用技巧,非常实用这里总结一下、省得每次用都百度:

ls命令总结:

-t 可以查看相关修改的时间

-l 每行显示一个条目

-h 可以结合显示文件的GB,MB等;

-R 递归显示

-n 显示组id和gid


1、查看我最新修改的文件是什么:

[root@xiaoluo test]# ls -lt
-rw-r--r-- 2 root root 12 Mar 19 14:17 3.txt
-rw-r--r-- 2 root root 12 Mar 19 14:17 4.txt
-rwxr-xr-x 1 root root 226 Mar 19 13:52 test.sh

2、以单位显示文件大小:

[root@xiaoluo ~]# ls -lh
total 384M
-rw-------. 1 root root 1.2K Dec 23 22:46 anaconda-ks.cfg
-rw-r--r-- 1 root root 383M Apr 30 2015 CentOS-6.6-x86_64-minimal.iso

3、递归显示文件:

[root@xiaoluo test]# ls -R /test/
/test/:
3.txt 4.txt test.sh

4、查看文件的组uid合gid:

[root@xiaoluo test]# ls -n /test/
-rw-r--r-- 2 0 0 12 Mar 19 14:17 3.txt
-rw-r--r-- 2 0 0 12 Mar 19 14:17 4.txt
-rwxr-xr-x 1 0 0 226 Mar 19 13:52 test.sh


实际有效应用:

查找系统中的最大文件:

[root@xiaoluo ~]# ls -sh | sort -nr | head -5
384M CentOS-6.6-x86_64-minimal.iso
92K index.html
12K install.log
4.0K Videos
4.0K Templates

与find命令结合删除系统里面最大的5个文件:

find . -type f -exec ls -s {} ; | sort -n -r | head -5


find命令小结:

1、忽略文件大小查找:

[root@xiaoluo test]# ls
3.txt 4.txt test.sh XIAOLUO

2、查找用户权限是rwx的(当然也可以按组找或者别的):

[root@xiaoluo test]# find . -perm -u=rwx -type f -exec ls -l {} ;
-rwxr-xr-x 1 root root 226 Mar 19 13:52 ./test.sh
[root@xiaoluo test]# ll
-rw-r--r-- 2 root root 12 Mar 19 14:17 3.txt
-rw-r--r-- 2 root root 12 Mar 19 14:17 4.txt
-rwxr-xr-x 1 root root 226 Mar 19 13:52 test.sh

3、查找空字节的文件:

[root@xiaoluo test]# find ~ -empty
/root/Desktop
/root/.elinks/bookmarks
/root/.local/share/.converted-launchers

4、查找大于30M的文件(小于用 -30M):

[root@xiaoluo test]# find / -size +30M
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/kvm/images/centos.qcow2


5、找出3天“以前”被修改过的文档
# find /root/ -mtime +3 -type f -print

7、找出3天“内”被修改过的文档
# find /root/ -mtime -3 -type f -print


7、文件状态判断:
-mtime: 指定时间文件内容被修改过
-ctime: 指定时间文件权限被修改过
-atime: 指定时间文件被读取过


强大的粘合剂paste:

[root@xiaoluo test]# cat 3.txt
xiaoluoge 3
[root@xiaoluo test]# cat 4.txt
xiaoluoge 4
[root@xiaoluo test]# paste 3.txt 4.txt
xiaoluoge 4 xiaoluoge 4


小字符拼接(当然个人认为用python的join是相当强悍但是复杂):

[root@xiaoluo test]# xiaoluo=123 [root@xiaoluo test]# echo ${xiaoluo}.log123.log

展开全文阅读

相关内容