ファイル・フォルダの基本操作

検索

検索には、find, fd 等のコマンドを使用する:

fd . -e png

--unrestricted -u.gitignoreの尊重を解除する:

fd  -u . --type f

--base-directoryで、従来のfindのように使う:

fd --base-directory=docs . --max-depth 1

-gで、regexの代わりにglobパターンを適用する:

fd '**/*.md' -g docs

--full-pathで、経由ディレクトリを制約とする:

fd '**/docs/**/*.txt' -g --full-path --max-depth 2

並列実行

繰り返し処理にはwhileforなどを用いる。parallelxargsを使って並列に処理を行うこともできる:

set items (fd . -e png)
for i in $items; do
  cp $(basename {}) .
done
fd . -e png -x parallel -j 4 rsync -auvz $(basename {}) .

並べ替え

自然順序でソートするには:

fd . -e png | sort -V

より多彩な並べ方をする場合、ls系コマンドを使うことも考えられる:

fd -e png -X eza -s modified

使用可能なキーワードは以下の通り:

eza -s
eza: Flag -s needs a value (choices: name, Name, size, extension, Extension, modified, changed, accessed, created, inode, type, none)

さらにリッチに出力するために以下のコマンドを使用する:

fd -e png -X eza -l --group-directories-first --no-user -@ --time-style +%Y%m%dT%H%M%S -s modified

選択

fzfや、skim による高速検索を活用する:

fd | fzf -m +s --reverse

Ref.:

活用例

チェックサム

データの真正性 (Intigrity)をたしかめるために、チェックサムを活用する:

fd -e png -x md5sum > file_checksums.txt
# for get sha256sum:
# brew install coreutils
 
find . -iname 'model*' -regex ".*\.\(jpg\|gif\|png\|jpeg\)" -exec sha256sum {} \;
ls . | xargs -i sha256sum "{}"

スナップGIFをつくる:

magick \
-delay 600 \
-gravity center (fd -e png) \
-resize 1920x880\> \
-background black \
-extent 1920x1080 \
-alpha remove \
-layers OptimizePlus \
-fill white -font 'MS-Mincho' \
-pointsize 40 \
-annotate +0-500 '%t' \
merged.gif

FAQ

find, fdの相違点は?

あるフォルダを$itemsとして指定したとしましょう。

ls $items
# 1.png 2.png videos/

異なる結果が出る理由を確認する:

find $items -type f -name "*.png"
fd $items --full-path -t f -e png

主な違いは、標準でパターンがどのように解釈されるかです。前者はGlobを使い、後者は正規表現を使います。

フォルダ名が「face」の.pngファイルのみをキャプチャするには:

fd --fixed-strings 'face/' --full-path -e png