0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

find と xargs でファイルを検索

Posted at

概要

find と xargs でファイルを検索する方法です。

実行

以下を実行すると、カレントディレクトリのファイルのみを対象として、ls コマンドを実行してくれます。

$ find . -type f -print0 | xargs -0 ls

結果

$ find . -type f -print0 | xargs -0 ls
./.firebase/hosting.YnVpbGQ.cache
./.firebaserc
./node_modules/@jest/core/LICENSE
./node_modules/@jest/core/README.md
./node_modules/@jest/core/build/FailedTestsCache.d.ts
./node_modules/@jest/core/build/FailedTestsCache.js
./node_modules/@jest/core/build/ReporterDispatcher.d.ts
./node_modules/@jest/core/build/ReporterDispatcher.js
./node_modules/@jest/core/build/SearchSource.d.ts
...

解説

find . -type f -print0

上記のコマンドの意味は以下の通りです。

command 意味
. カレントディレクトリを指定
-type f ファイルのみを指定
-print0 区切り文字をスペースから\0(null 文字)に変更される
xargs -0 ls

上記のコマンドの意味は以下の通りです。

command 意味
xargs 読み込んだ文字列(find でとってきたやつ)を引数で指定されたコマンド(ls)の引数にして、引数で指定されたコマンド(ls)を実行する
-0 \0 を区切り文字(デミリタ)として扱う

おまけ

改行コード一括変換

find . -type f -print0 | xargs -0 nkf --overwrite -w -Lu
command 意味
nkf 文字エンコードしたり、改行コード変換したり(Network Kanji Filter)
-w UTF-8 変換
-Lu 改行を LF にする
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?