TIL: Clearing directories and appending filenames in bulk
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.
#file-management #tips #bash #find #find-command #cli #shell-scripting #shell #linux
Reply to this post by email blZake@proZbableodyssey.blog (remove Z characters) ↪
Comments
Leave a comment
Markdown is supported. Your email is private and only used if you'd like a reply.