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/p_w_picpaths/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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章