0

I have a folder with thousands of files in this format

  • filenameA - 1.ext
  • filenameA - 2.ext
  • filenameB - 1.ext

and sometimes

  • filenameB - 02.ext

I need a script that will read the files and create a single folder with each distinct filename before the " - #.ext" and then add all files with that same name into the folder.

From the example above I would have two folders 1) filenameA 2) filenameB - each with two files in them. The actual data has thousands of files and each group can have 1 to 50ish files, with sequential numbers added to the end.

0

If all your files follow the convention of dirname - the_rest_and_ext (have the - in the middle) you can use something like:

for f in *; do
   dir_name=`echo "$f" | sed 's/ -.*//'`
   mkdir -p "$dir_name"
   mv "$f" "$dir_name"
done

If all the files have the same extension, you can change the for to something like:

for f in *.ext; do

NOTE: make sure you know what you are doing.

  • This works perfectly per my request but you were correct that I do have some filenames with " - " in the title. But none that have " - #" other than number at the end of the file. What do I add to ignore " - " (space-dash-space) when it is not followed by a number? – KeithS Mar 13 '18 at 17:42

You must log in to answer this question.

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