ProbableOdyssey | Blake Cook

TIL: Clearing directories and appending filenames in bulk

· 1 min read · 93 words

Yet another set of commands I keep forgetting!

# Clear directory
find . -type d -empty -delete

# Append `foo-` to all files in directory
find . -type f -exec sh -c 'mv "$1" "foo-${1#./}"' _ {} \;

Note, the underscore is a placeholder for $0 (the script name). When using sh -c, the first argument after the command string is assigned to $0, the next to $1, etc. Without the underscore, {} would become $0 instead of $1, breaking the reference "$1". So it ensures the filename from find is passed as $1.

Reply to this post by email blZake@proZbableodyssey.blog (remove Z characters) ↪