Issue
I have a directory which contains multiple sub-directories with mov and jpg files.
/dir/
/subdir-a/ # contains a-1.jpg, a-2.jpg, a-1.mov
/subdir-b/ # contains b-1.mov
/subdir-c/ # contains c-1.jpg
/subdir-d/ # contains d-1.mov
... # more directories with the same pattern
I need to find a way using command-line tools (on Mac OSX, ideally) to move all the mov files to a new location. However, one requirement is to keep directory structure i.e.:
/dir/
/subdir-a/ # contains a-1.mov
/subdir-b/ # contains b-1.mov
# NOTE: subdir-c isn't copied because it doesn't have mov files
/subdir-d/ # contains d-1.mov
...
I am familiar with find
, grep
, and xargs
but wasn't sure how to solve this issue. Thank you very much beforehand!
Solution
It depends slightly on your O/S and, more particularly, on the facilities in your version of tar
and whether you have the command cpio
. It also depends a bit on whether you have newlines (in particular) in your file names; most people don't.
Option #1
cd /old-dir
find . -name '*.mov' -print | cpio -pvdumB /new-dir
Option #2
find . -name '*.mov' -print | tar -c -f - -T - |
(cd /new-dir; tar -xf -)
The cpio
command has a pass-through (copy) mode which does exactly what you want given a list of file names, one per line, on its standard input.
Some versions of the tar
command have an option to read the list of file names, one per line, from standard input; on MacOS X, that option is -T -
(where the lone -
means 'standard input'). For the first tar
command, the option -f -
means (in the context of writing an archive with -c
, write to standard output); in the second tar
command, the -x
option means that the -f -
means 'read from standard input'.
There may be other options; look at the manual page or help output of tar
rather carefully.
This process copies the files rather than moving them. The second half of the operation would be:
find . -name '*.mov' -exec rm -f {} +
Answered By - Jonathan Leffler