欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux find命令之exec簡單概述

 更新時間:2017年02月11日 11:06:02   作者:優(yōu)雅的程序yuan  
這篇文章主要為大家詳細介紹了linux find命令之exec的簡單使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

find是我們很常用的一個Linux命令,但是我們一般查找出來的并不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現(xiàn)出來了。

exec解釋:

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

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

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

實例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
[root@localhost test]# 

說明:

在shell中用任何方式刪除文件之前,應(yīng)當先查看相應(yīng)的文件,一定要小心!當使用諸如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結(jié)尾、更改時間在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,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個root用戶。

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

命令:

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

輸出:

[root@localhost test]# ll
總計 12
drwxr-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選項執(zhí)行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
[root@localhost test3]#

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vim 編輯器操作匯總

    Vim 編輯器操作匯總

    本文是小編給大家收藏整理的關(guān)于vim編輯器操作方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-05-05
  • Linux Shell腳本中獲取本機ip地址方法

    Linux Shell腳本中獲取本機ip地址方法

    這篇文章主要介紹了Linux Shell腳本中獲取本機ip地址方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • shell隨機定時修改密碼change_passwd.sh

    shell隨機定時修改密碼change_passwd.sh

    這篇文章主要介紹了shell隨機定時修改密碼change_passwd.sh,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • Git 創(chuàng)建分支提交遠程分支詳解

    Git 創(chuàng)建分支提交遠程分支詳解

    這篇文章主要介紹了Git 創(chuàng)建分支提交遠程分支詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • shell腳本中使用return和exit的方法

    shell腳本中使用return和exit的方法

    return和exit各有用途,合理使用可以使shell編程更規(guī)范可控,return是一個關(guān)鍵字, exit是一個函數(shù),這篇文章主要介紹了shell腳本之如使用return和exit,需要的朋友可以參考下
    2023-08-08
  • Linux下的自動化構(gòu)建工具之make/makefile的用法詳解

    Linux下的自動化構(gòu)建工具之make/makefile的用法詳解

    這篇文章主要為大家詳細介紹了Linux下的自動化構(gòu)建工具之make/makefile的區(qū)別與使用,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2022-10-10
  • 查看Linux系統(tǒng)重啟的四種基本命令詳解

    查看Linux系統(tǒng)重啟的四種基本命令詳解

    Linux 重啟命令是指在Linux系統(tǒng)中,通過指定命令來啟動或重啟系統(tǒng),在日常使用中,我們經(jīng)常需要對系統(tǒng)進行重啟操作,如更新軟件、修改系統(tǒng)配置等,因此,熟練掌握Linux重啟命令是非常必要的,所以本文給大家介紹了查看Linux系統(tǒng)重啟的四種基本命令,需要的朋友可以參考下
    2024-04-04
  • Linux命令行查看cpu(lm_sensors)和顯卡溫度(nvidia-smi)的操作方法

    Linux命令行查看cpu(lm_sensors)和顯卡溫度(nvidia-smi)的操作方法

    lm_sensors,是一款基于linux系統(tǒng)的硬件監(jiān)控的軟件??梢员O(jiān)控主板,CPU的工作電壓,溫度等數(shù)據(jù),這篇文章主要介紹了Linux命令行如何查看cpu(lm_sensors)和顯卡溫度(nvidia-smi),需要的朋友可以參考下
    2022-12-12
  • CentOS下mysql定時備份Shell腳本分享

    CentOS下mysql定時備份Shell腳本分享

    這篇文章主要介紹了CentOS下mysql定時備份Shell腳本分享,本文使用的是最簡單的方法,需要的朋友可以參考下
    2014-12-12
  • linux基礎(chǔ)之Shell Script入門介紹

    linux基礎(chǔ)之Shell Script入門介紹

    本文介紹下,學習shell script編程的入門知識,通過幾個入門實例,帶領(lǐng)大家走進shell script的神圣殿堂,呵呵,有需要的朋友參考下
    2013-11-11

最新評論