• Outils en Ligne
  • - Calculatrices
    • Compteur de Caractères
  • - Téléchargement
    • Téléchargement TikTok
    • Téléchargement Douyin
  • - Outils Web
    • BASE64
    • Base64 vers image
    • Image vers Base64
    • Encodage URL
    • JavaScript
    • Timestamp
    • Convertisseur Unicode
    • Formatage JSON
    • Modifier l’Extension
    • Créer une Liste
    • Optimiseur CSS
  • - Outils de Chiffrement
    • Chiffrement MD5
    • Générateur Aléatoire
  • - Outils d’Image
    • Compression d’Images
    • Générateur de QR Code
    • Lecteur de QR Code
    • Prévisualisation de Fond
  • - Fiches d'information
    • Hérédité du Groupe Sanguin
    • Tailles Vêtements
  • [email protected]
DopuBox
  • English
  • Español
  • Français
  • 日本語
  • 한국어
  • 简体中文
  • 繁體中文
全部 ニュース Meta Code 文化・アート
Linux findコマンドにおける-execパラメータの役割の紹介
2021-12-12

Linuxコマンドに異なるパラメータを加えると効果も異なることはよく知られていますが、Linux fingコマンドの-execパラメータについて詳しく説明します。

exec解釈:

-execパラメータの後にcommandコマンドが付いています。その終了は次のとおりです。終了フラグのため、このコマンドの後ろのセミコロンは不可欠であり、各システムでセミコロンが異なる意味を持つことを考慮して、前にスラッシュを付けます。

{}カッコは、前のfindで検索されたファイル名を表します。

findを使うときは、欲しい操作を1つのファイルに書けば、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

  [root@localhost test]#

説明:

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ユーザーが存在するかどうかを確認します。

2 合計3ページ インスタンス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

合計0root@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

合計0root@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:「./test 3/log 2014.log」および「test 3/log 2014.log」は同じファイル

cp:「./test 3/log 2013.log」および「test 3/log 2013.log」は同じファイル

cp:「./test 3/log 2012.log」および「test 3/log 2012.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]#

Linux findコマンドの-execパラメータの使い方を説明しましたが、findコマンドのパラメータはまだたくさんあります。他のパラメータの使用を知りたい場合は、Linux findコマンドの-path-pruneパラメータの役割を参照してください。


ソース元URL:https://dopubox.com/article/p/d357da49c8a16c5e

Autres Outils
  • Compteur de Caractères Téléchargement TikTok Téléchargement Douyin BASE64 Base64 vers image Image vers Base64 Encodage URL JavaScript Timestamp Convertisseur Unicode Formatage JSON Modifier l’Extension Créer une Liste Optimiseur CSS Chiffrement MD5 Générateur Aléatoire Compression d’Images Générateur de QR Code Lecteur de QR Code Prévisualisation de Fond Hérédité du Groupe Sanguin Tailles Vêtements
  • 歯科医師の技術で愛好家も納得するサウナを細部まで再現するミニチュア作家
    2024-05-30

    藤井聡太八冠、瀬戸際の戦い 将棋叡王戦、31日に第4局
    2024-05-29

    将棋叡王戦、藤井が勝ち最終局へ
    2024-05-30

    【6月1日~2日】週末必見の“アート&デザイン”まとめ!
    2024-06-01

    『マッドマックス:フュリオサ』に登場する「ある絵画」は何を意味しているのか? ギリシア神話と「ファム・ファタル」から考察する
    2024-06-01

    鎌倉幕府の地頭から海の武士団・松浦党に…古文書群「青方文書」からみる中世の漁業や製塩事情
    2024-06-01

    <今週の本棚・次回の予定>6月8日の毎日新聞書評欄は『新装版 ペルーからきた私の娘』ほか
    2024-06-03

    「工芸的美しさの行方―うつわ・包み・装飾」が東京と京都で開催。第1弾はBONDED GALLERYにて7月から
    2024-06-03

    現代美術作家の杉本博司がYouTubeチャンネル開設 素顔に迫る映像を公開
    2024-06-04

    「君たちはどう生きるか」展 第二部 レイアウト編(三鷹の森ジブリ美術館)レポート。絵を描くことの営為、そして苦悩も見せる
    2024-06-04

    建築家・白井晟一設計の個人住宅「桂花の舎」が江之浦測候所に移築へ
    2024-06-04

    「TRIO パリ・東京・大阪 モダンアート・コレクション」(東京国立近代美術館)開幕レポート。トリオで再発見する3館のコレクション
    2024-06-05

    太宰府天満宮で「Fukuoka Art Book Fair」が開催へ
    2024-06-04

    国際博物館の日、今年のテーマは「学びと研究のための博物館」
    2024-06-04

    横山奈美の個展「広い空に / Big Sky Mind」がN&A Art SITEで開催へ
    2024-06-05

    夜の美術館を楽しめる「サマーナイトミュージアム2024」が開催。東京都現代美術館、東京都庭園美術館などで入場料割引も
    2024-06-04

    105歳で死去した画家弥勒祐徳さん おごらず、黙々と、ひたすらに神楽など描く
    2024-06-05

    3日間限定発売! 名作パントンチェアが新たな魅力を纏った限定カラーで登場。
    2024-06-05

    「江戸期より後の遺構」 世界遺産目指す佐渡の金山、一部除外勧告
    2024-06-06

    アキナの山名さん骨折 毎日放送、ロケ中に転倒
    2024-06-06

    ©  Dopu Box
    💛