LoginSignup
0
1

More than 5 years have passed since last update.

find で検索結果を検索対象パスからの相対パスで取得する

Posted at

したいこと:

$ find hoge/fuga -name '*.txt'
hoge/fuga/000.txt
hoge/fuga/001.txt
hoge/fuga/subdir0/000.txt
hoge/fuga/subdir0/001.txt
hoge/fuga/subdir1/000.txt
hoge/fuga/subdir1/001.txt

なときに

000.txt
001.txt
subdir0/000.txt
subdir0/001.txt
subdir1/000.txt
subdir1/001.txt

がほしい.

汎用的なやりかたは,sed などで不要な部分を消す.末尾のバックスラッシュの有無に注意.

$ find hoge/fuga -name '*.txt' | sed 's|^hoge/fuga/||'

GNU 版 find の場合,-printf オプションを用いて,ヒットしたファイルパスから検索対象パスを除いた部分 (%P) を出力すればよい.

$ find hoge/fuga -name '*.txt' -printf '%P\n'

-printf オプションの書式指定子の詳細は Man page of FIND に挙げられている.ヒットしたファイルからいろんな情報を取得でき,偏執的便利そう.

0
1
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
1