Moving files of specific type/extension from one directory to another?
Have a large folder filled with a large collection of different files.Ideally, I want to shift all the different files into their respective folders; so jpg files into the jpg folder etc.
Original folder : unsorted_files destination folder: jpgfolder I tried
mv /home/tony/Desktop/unsorted_files/*.jpg /home/tony/Desktop/jpgfolder
But got an error "jpgfolder" is not a directory"
Taking in account that 1) Q said "large collection ... of files" - the list of files might not all fit in one command line buffer (2,084,684 bytes on MY system); and 2) Filenames might contain funny characters ("My Stuff.jpg
"); mv
is not the best way. Using find
, xargs
, and the sure knowledge that filenames must NOT contain NUL bytes (or slashes):
find /home/tony/Desktop/unsorted_files/ -maxdepth 1 -type f -iname '*.jpg' -print0 | \
xargs -0 --no-run-if-empty echo mv --target-directory=/home/tony/Desktop/jpgfolder
Once the echo
d commands look right, remove the echo
, and let mv
do the work.
If you have already done mv /home/tony/Desktop/unsorted_files/*.jpg /home/tony/Desktop/jpgfolder
BEFORE you created the /home/tony/Desktop/jpgfolder
, nothing has been lost, but you must, as other answers have said, mkdir -p /home/tony/Desktop/jpgfolder
first.