I am writing a simple method to mass move files and have attempted two approaches:
#(1)
find . -name '*.pdf' | xargs -I{} mkdir pdfs; mv {} pdfs
#(2)
find . -name '*.pdf' -exec mv {} pdfs \+
The first approach surprisingly worked 'sometimes', however, after deleting the folder with the pdfs a few times and returning the pdfs to the parent directory, it suddenly stopped working.
It produces the following error:
mv: rename {} to pdfs: No such file or directory
xargs: unterminated quote
Whereas the second approach gives the following error:
find: -exec: no terminating ";" or "+"
Update:
I got it working with:
find . -name '*.pdf' -exec mv "{}" pdfs \;
However, If I wanted to create the directory and move files in one line, this wont work, for example:
find . -name '*.csv' -exec mkdir -p csvs && mv "{}" csvs \;
find: -exec: no terminating ";" or "+"
How to implement directory creation and move?
man 1 find
and read the entries on-exec
closely. Hint: the position of{}
matters.find -exec <command> \;
always executescommand
once for each of its matches. Why not:mkdir foo; find . -name bar -exec mv -t foo {} \;
?-exec mv -t csvW {} +
is completely robust. You are needlessly complicating things and breaking support for arbitrary file names. See also mywiki.wooledge.org/BashFAQ/020-t
you can't do that; you are saying to movecsvW
and all the other files onto the last found file. Try insteadfind ... -exec sh -c 'mv "$@" "$0"' csvW {} +