60歳からITエンジニアを目指す無謀なブログ

60歳で定年し、職種を変更してIT技術者を目指すブログです。

/dev/null の活用

Linuxのコマンド実行していると膨大なエラーログメッセージが出ることがあり、

その中に真のメッセージが紛れていることがあります。

 

例) find コマンドであるファイルを探すときにシステム管理でアクセス不可の

フォルダも探しに行くため、大量のエラーが表示されます

 >find ~ -name .zhrc

find: /Users/myaccount/Pictures/Photos Library.photoslibrary: Operation not permitted

find: /Users/myaccount/Library/Application Support/CallHistoryTransactions: Operation not permitted

find: /Users/myaccount/Library/Application Support/CloudDocs/session/db: Operation not permitted

find: /Users/myaccount/Library/Application Support/com.apple.sharedfilelist: Operation not permitted

find: /Users/myaccount/Library/Application Support/Knowledge: Operation not permitted

find: /Users/myaccount/Library/Application Support/com.apple.TCC: Operation not permitted

 このような同様のメッセージが長々と続きます

注)上記で便宜上、私の $HOME の名前はmyaccountに置き換えてありあます

 

これを以下のように標準エラー出力を/dev/nullに捨てるようにすると

正しく見つけた標準出力メッセージのみが出力されるようになります。

find ~ -name .zshrc 2> /dev/null

/Users/myaccount/.zshrc   <ーーーヒットしたファイルの場所は標準出力なので

                  これだけ出力される

 

/dev/nullの使い方を知らなかったので調べて為になりました。