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}.log
123.log










本文转自 小罗ge11 51CTO博客,原文链接:http://blog.51cto.com/xiaoluoge/1752869,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
Linux
`grep`命令搜索当前目录及其子目录下的所有文件
`grep`命令搜索当前目录及其子目录下的所有文件
243 1
|
4月前
|
Linux Shell 数据库
linux(十九)查找文件的四个命令whereis、which、find、locate~
linux(十九)查找文件的四个命令whereis、which、find、locate~
45 0
|
安全 Shell Linux
linux命令之find查找文件
find 用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则 find 命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
240 0
|
存储 Linux Shell
厉害了!除了find命令,还有这么多文件查找命令,高手必备!
云栖号资讯:【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 在系统里查找文件,是所有工程师都必备的技能(不管你用的是 Windows 、Linux、还是 MacOS 系统)。
厉害了!除了find命令,还有这么多文件查找命令,高手必备!
|
Shell 关系型数据库 Oracle
[20180302]使用find命令小错误.txt
[20180302]使用find命令小错误.txt --//上午一台机器磁盘空间爆满,腾出磁盘空间顺便清理adump目录文件,再次遇到小问题.做一个记录. --//我使用find遇到的问题还不少,链接:http://blog.
1096 0