I have a folder that has several hundreds of thousands of files named like "ABC 123456". I need to add a dash after between the two blocks of text like this "ABC – 123456". Leaving the trailing space.
Automator does not offer a method for counting 4 characters in and inserting an new dash and space.
I have this terminal command and have been tinkering with it.
for file in *.txt
do
echo mv "$file" **"$file" count 3 "- "**
done
Anyone know of an Applescript or can help with the terminal command that will help me rename the files?
Best Answer
The question itself isn't exact too, because the OP talks about:
ABC 123456
(without extension), but in the example code using*.txt
extensionABC - 123456
, but in the next taking aboutcounting to 4
andcount 3
, e.g. not exacltly clear what the script should do for example if findABCD 12
orAB 345
The problem is dividable to 3 separate parts:
Ad "selecting the right files". Selecting the files, can be done by some external utilities, for example by the
find
command. The main benefits of thefind
are:man find
.)Ad "prepare the new name". This can be done by external programs, like
sed
orawk
or any program what can manipulate text in shell scripts, or it can be done (in some simple cases such this) withing thebash
itself - and is possible to save one expensive command execution. (for hunderts of thousand files it makes a difference).The following:
substitutes (replaces) one space (the
/ /
part) with the dash (/-
). So it is possible to writeand save the
sed
execution.So one of the alternative solutions could be, for example the following:
The abobe is for the DRY RUN - it only will show, what will be done, for the real execution remove the
echo
.Decomposition:
selecting the right files for the rename: the
find . -maxdepth 1 -type f -iname "* *.jpg" -print0
will find all-maxdepth 1
)-type f
)-print0
)the cycle
while IFS= read -r -d $'\0' filename; do ... done < <( )
find
command-d $'\0'
)-r
) /advanced topic - not needed to explained here/IFS=
prevents trimming of leading and trailing whitespace from the filename (also, a bit advanced topic)preparing the new filename
newfilename="${filename/ /-}"
bash
internally (without executing an external command).${filename/ /- }
the actual renaming is done by the
mv
command (seeman mv
from the terminal), where-i
what will ask the user if here are already a file with the new filename (don't override it)and the
mv
is executed only when thenewfilename
is different from thefilename
[[ "$filename" != "$newfilename" ]]
The above solution is mostly error-prone, but it still isn't very effective, because for hundreds of thousand files will execute hundreds of thousand times the
mv
command. The solution can be: using some utility, what can read the filenames and do the renaming without executing themv
command N-times. For example, the "big gun" of system admins the "perl", as:what will rename all files what outputs the
find
in one execution - e.g. much faster as thousands ofmv
executions.Tor testing (DRY RUN) use the following:
Ps: the perl is powerful enough to handle everything itself, eg. the find command too, but it is more advanced topic...
Ps2: my English is much worse as my
bash
, so someone could kindly edit this post for adding/correcting things.. ;)