8

I have lots of individual files:

/File 01.txt
/Picture 02.png

Is there a terminal command that would do this so I end up with this:

/File 01/File 01.txt
/Picture 02/Picture 02.png/

I'm on a Mac on OS X 10.11.3

It sounds very simple, but I'm fairly new to Mac's and have no real knowledge of Terminal having moved from Windows.

0

3 Answers 3

13

Before you start, I really recommend you make a backup of the folder you are trying to do this on.

cd into the folder you trying to operate on and then:

for file in *; do
  if [[ -f "$file" ]]; then
    mkdir "${file%.*}"
    mv "$file" "${file%.*}"
  fi
done
  1. Loop over all (*) the files in the current folder.
  2. create a folder (mkdir) from the file without its extension ${file%.*}
  3. move (mv) the file into that folder.

Note that you have to use quotation because some files might have spaces in their names.

You can either type this up in the terminal or creating a script file.

4
  • AMAZING! Thanks sooo much. Is it possible to get the loop to only loop over files and ignore any existing directories? Mar 5, 2016 at 22:07
  • I should add it works with this, but causes an error when it processes each directory. Mar 5, 2016 at 22:45
  • 2
    That's easy! You just need an if statement that tests if $file is a folder or a file. I edited my answer to reflect that. Mar 6, 2016 at 11:57
  • Superb script. This just saved me hours of time. I was able to cd into my folder in Terminal and just copy/paste the script and hit return for it to work.
    – GForce
    Jul 27, 2019 at 16:20
3

I don't have enough reputation to add this as a comment to @Ammar Alammar's awesome answer above, but in case anyone wants to just paste this directly at a terminal prompt rather than saving it as a script, this one-liner should do the trick:

for file in *; do if [[ -f "$file" ]]; then mkdir "${file%.*}"; mv "$file" "${file%.*}"; fi; done
1
  • Nice. Keep posting.
    – gnicko
    Sep 23, 2020 at 13:18
0

but I was wondering if have many files on a folder for instance that have same name followed by a number, for example fotolux-1.jpg, fotolux-2.jpg... and Fotoone-1.jpg, fotoone-2.jpg...etc... and would like to create a folders called fotolux, fotoone, etc etc to have all files inside theur respective name folder for example fotolux, fotoone, etc, desregarding what changes should I do to make this work... Thank you

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 21 at 20:13
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review Feb 21 at 20:59

You must log in to answer this question.

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