Shell
File management
cp -R path/source path/target
Copy directories.
find . -name '.DS_Store' -type f -delete
Delete all .DS_Store files recursively.
File management, batching
https://stackoverflow.com/a/24265787
find
Search for files in a directory.
find <dir> <pattern> <action>
For example:
find . -name \*.jpg -type f -print0
.searches current directory (recursively)-namecase sensitive search-inamecase insensitive\*.jpgpattern (why\?)-print0print filename, for e.g. to use with pipe|-exec <command> {};-<command> command to run, e.g.cp-{}gets replaced with current file -;` terminates command
find . \( -name \*.jpg -o -name \*.jpeg \) -print0
-oforOr
find . -name "*.jpg" -delete
-
Dedicated
findflag for deleting files. -
The pattern is quoted here, another alternative is to escape the asterisk as in a previous example
-
-type fselects files only (alsodfor directory andlfor symlink) -
-print0print filename -
-exec <command> {};-<command> command to run, e.g.cp-{}gets replaced with current file -;` terminates command
xargs
Execute commands from standard input. Use | to pipe commands to xargs.
find . -name \*.jpg -print0 | xargs -I{} -0 cp -v -f {} /home/dir
-I{}replacement string-0separate items with a null charactercpoptions:-vfor Verbose,-ffor Force`
Practical examples
Find all video files in a tree
- Just a quick list of video file extensions, not exhaustive.
- Probably not a safe copy, since the original tree may have duplicate file names in different directories, but
cpwill overwrite them and only keep the last one in the flattened target directory.
find . \( -name \*.webm -o -name \*.mkv -o -name \*.flv -o -name \*.vob -o -name \*.ogv -o -name \*.avi -o -name \*.mov -o -name \*.wmv -o -name \*.asf -o -name \*.mp4 -o -name \*.m4p -o -name \*.m4v -o -name \*.mpeg -o -name \*.mpv -o -name \*.mp2 -o -name \*.3gp -o -name \*.swf \) -print0 | xargs -I{} -0 cp -v -f {} /home/dir