0

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"

3
  • 2
    you probably move the fist file to "/home/tony/Desktop/jpgfolder" move it back. Then makedir /home/tony/Desktop/jpgfolder and run mv /home/tony/Desktop/unsorted_files/*.jpg /home/tony/Desktop/jpgfolder/
    – Panther
    Nov 9, 2017 at 15:10
  • mv does not create folders. 1. mkdir -p /home/tony/Desktop/jpgfolder; 2. mv ...
    – pLumo
    Nov 9, 2017 at 15:11
  • I know it doesn't, I already have created the destination folder.
    – Jack White
    Nov 9, 2017 at 15:12

2 Answers 2

2

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 echod 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.

7
  • Read man find. Read man xargs. Try to state your confusion as a question.
    – waltinator
    Nov 9, 2017 at 19:14
  • @IronUhlan And add that question here instead of posting a new question with the same content!
    – dessert
    Nov 9, 2017 at 19:49
  • Which I've already done.I did that because of the coding stuff.
    – Jack White
    Nov 9, 2017 at 19:51
  • find ... -exec mv -t ... -- {} + would be slightly more elegant than the detour through xargs, imho. Otherwise +1. Nov 10, 2017 at 23:54
  • @david-foerster How does find ... -exec mv -t ... -- {} + handle too many filenames? man find says The command line is built in much the same way that xargs builds its command lines., but that's unclear.
    – waltinator
    Nov 15, 2017 at 5:48
-1

As far as I know - corrections are welcome - mv does not actually move files, it renames them. So you are trying to rename all jpg files to a folder name.

I would try:

rsync /home/tony/Desktop/unsorted_files/*.jpg /home/tony/Desktop/jpgfolder
2
  • while staying on the same filesystem, moving and renaming a file is exactly the same thing. mv is smart enough to know if the file should be moved to another filesystem or just renamed into another directory.
    – pim
    Nov 9, 2017 at 15:16
  • pastebin.com/rQg59xu7 @waltinator, I have tried to implement your solution, but still the files are not being transferred
    – Jack White
    Nov 9, 2017 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .