日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下find命令之-exec ll -sh {} \;

發布時間:2023/12/31 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下find命令之-exec ll -sh {} \; 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

exec解釋:

-exec??參數后面跟的是command命令,它的終止是以;為結束標志的,所以這句命令后面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜杠。

{}???花括號代表前面find查找出來的文件名。

使用find時,只要把想要的操作寫在一個文件里,就可以用exec來配合find查找,很方便的在有些操作系統中只允許-exec選項執行諸如l?s或ls?-l這樣的命令。大多數用戶使用這一選項是為了查找舊文件并刪除它們。建議在真正執行rm命令刪除文件之前,最好先用ls命令看一下,確認它們是所要刪除的文件。?exec選項后面跟隨著所要執行的命令或腳本,然后是一對兒{?},一個空格和一個\,最后是一個分號。為了使用exec選項,必須要同時使用print選項。如果驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。

實例1:ls?-l命令放在find命令的-exec選項中?

命令:

find?.?-type?f?-exec?ls?-l?{}?\;

輸出:?

[root@localhost?test]#?find?.?-type?f?-exec?ls?-l?{}?\;?

-rw-r--r--?1?root?root?127?10-28?16:51?./log2014.log

-rw-r--r--?1?root?root?0?10-28?14:47?./test4/log3-2.log

-rw-r--r--?1?root?root?0?10-28?14:47?./test4/log3-3.log

-rw-r--r--?1?root?root?0?10-28?14:47?./test4/log3-1.log

-rw-r--r--?1?root?root?33?10-28?16:54?./log2013.log

-rw-r--r--?1?root?root?302108?11-03?06:19?./log2012.log

-rw-r--r--?1?root?root?25?10-28?17:02?./log.log

-rw-r--r--?1?root?root?37?10-28?17:07?./log.txt

-rw-r--r--?1?root?root?0?10-28?14:47?./test3/log3-2.log

-rw-r--r--?1?root?root?0?10-28?14:47?./test3/log3-3.log

-rw-r--r--?1?root?root?0?10-28?14:47?./test3/log3-1.log

[root@localhost?test]#

說明:?

上面的例子中,find命令匹配到了當前目錄下的所有普通文件,并在-exec選項中使用ls?-l命令將它們列出。

實例2:在目錄中查找更改時間在n日以前的文件并刪除它們

命令:

find?.?-type?f?-mtime?+14?-exec?rm?{}?\;?

輸出:

[root@localhost?test]#?ll

總計?328

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

-rw-r--r--?1?root?root?????33?10-28?16:54?log2013.log

-rw-r--r--?1?root?root????127?10-28?16:51?log2014.log

lrwxrwxrwx?1?root?root??????7?10-28?15:18?log_link.log?->?log.log

-rw-r--r--?1?root?root?????25?10-28?17:02?log.log

-rw-r--r--?1?root?root?????37?10-28?17:07?log.txt

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxrwx?2?root?root???4096?10-28?14:47?test3

drwxrwxrwx?2?root?root???4096?10-28?14:47?test4

[root@localhost?test]#?find?.?-type?f?-mtime?+14?-exec?rm?{}?\;

[root@localhost?test]#?ll

總計?312

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

lrwxrwxrwx?1?root?root??????7?10-28?15:18?log_link.log?->?log.log

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxrwx?2?root?root???4096?11-12?19:32?test3

drwxrwxrwx?2?root?root???4096?11-12?19:32?test4


說明

在shell中用任何方式刪除文件之前,應當先查看相應的文件,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的文件進行操作之前提示你。?

實例3:在目錄中查找更改時間在n日以前的文件并刪除它們在刪除之前先給出提示

命令:

find?.?-name?"*.log"?-mtime?+5?-ok?rm?{}?\;

輸出:

[root@localhost?test]#?ll

總計?312

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

lrwxrwxrwx?1?root?root??????7?10-28?15:18?log_link.log?->?log.log

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxrwx?2?root?root???4096?11-12?19:32?test3

drwxrwxrwx?2?root?root???4096?11-12?19:32?test4

[root@localhost?test]#?find?.?-name?"*.log"?-mtime?+5?-ok?rm?{}?\;

<?rm?...?./log_link.log?>???y

<?rm?...?./log2012.log?>???n

[root@localhost?test]#?ll

總計?312

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxrwx?2?root?root???4096?11-12?19:32?test3

drwxrwxrwx?2?root?root???4096?11-12?19:32?test4

[root@localhost?test]#

說明

面的例子中,?find命令在當前目錄中查找所有文件名以.log結尾、更改時間在5日以上的文件,并刪除它們,只不過在刪除之前先給出提示。?按y鍵刪除文件,按n鍵不刪除。?

?

實例4:-exec使用grep命令

命令:

find?/etc?-name?"passwd*"?-exec?grep?"root"?{}?\;

輸出:

[root@localhost?test]#?find?/etc?-name?"passwd*"?-exec?grep?"root"?{}?\;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@localhost?test]#

說明:

任何形式的命令都可以在-exec選項中使用。??在面的例子中我們使用grep命令。find命令首先匹配所有文件名為“?passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執行grep命令看看在這些文件中是否存在一個root用戶。


實例5:查找文件移動到指定目錄??

命令:

find?.?-name?"*.log"?-exec?mv?{}?..?\;

輸出:

[root@localhost?test]#?ll

總計?12drwxr-xr-x?6?root?root?4096?10-27?01:58?scf

drwxrwxr-x?2?root?root?4096?11-12?22:49?test3

drwxrwxr-x?2?root?root?4096?11-12?19:32?test4

[root@localhost?test]#?cd?test3/

[root@localhost?test3]#?ll

總計?304

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

-rw-r--r--?1?root?root?????61?11-12?22:44?log2013.log

-rw-r--r--?1?root?root??????0?11-12?22:25?log2014.log

[root@localhost?test3]#?find?.?-name?"*.log"?-exec?mv?{}?..?\;

[root@localhost?test3]#?ll

總計?0[root@localhost?test3]#?cd?..

[root@localhost?test]#?ll

總計?316

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

-rw-r--r--?1?root?root?????61?11-12?22:44?log2013.log

-rw-r--r--?1?root?root??????0?11-12?22:25?log2014.log

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxr-x?2?root?root???4096?11-12?22:50?test3

drwxrwxr-x?2?root?root???4096?11-12?19:32?test4

[root@localhost?test]#

實例6:用exec選項執行cp命令??

命令:

find?.?-name?"*.log"?-exec?cp?{}?test3?\;

輸出:

[root@localhost?test3]#?ll

總計?0[root@localhost?test3]#?cd?..

[root@localhost?test]#?ll

總計?316

-rw-r--r--?1?root?root?302108?11-03?06:19?log2012.log

-rw-r--r--?1?root?root?????61?11-12?22:44?log2013.log

-rw-r--r--?1?root?root??????0?11-12?22:25?log2014.log

drwxr-xr-x?6?root?root???4096?10-27?01:58?scf

drwxrwxr-x?2?root?root???4096?11-12?22:50?test3

drwxrwxr-x?2?root?root???4096?11-12?19:32?test4

[root@localhost?test]#?find?.?-name?"*.log"?-exec?cp?{}?test3?\;

cp:?“./test3/log2014.log”?及?“test3/log2014.log”?為同一文件

cp:?“./test3/log2013.log”?及?“test3/log2013.log”?為同一文件

cp:?“./test3/log2012.log”?及?“test3/log2012.log”?為同一文件

[root@localhost?test]#?cd?test3

[root@localhost?test3]#?ll

總計?304

-rw-r--r--?1?root?root?302108?11-12?22:54?log2012.log

-rw-r--r--?1?root?root?????61?11-12?22:54?log2013.log

-rw-r--r--?1?root?root??????0?11-12?22:54?log2014.log



轉載于:https://blog.51cto.com/925370765/1763404

總結

以上是生活随笔為你收集整理的linux下find命令之-exec ll -sh {} \;的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。