I need to add a character to the middle of a file name, I need to do this for about 8000 files all in the same directory. I'm looking for a command-line option:
Example (all files that need renaming are five digits, they are in a directory that contains other six-digit filenames that do not need to be renamed):
01011
02022
12193
To:
010101
020202
121903
I've tried a couple of things: rename, mv, etc. Similar to this (Bash - Adding 0's in the middle of a file name) but not exactly
Another way to deal with spaces and special characters in a file name is to escape the characters. You put a backslash ( \ ) in front of the special character or space.
Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified. The quotation marks must be used.
Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters. Characters that are NOT supported include, but are not limited to: @ $ % & \ / : * ?
You can write:
for file in * ; do
mv ./"$file" "${file:0:4}0${file:4}"
done
(See the explanation of ${parameter:offset}
and ${parameter:offset:length}
in §3.5.3 "Shell Parameter Expansion" of the Bash Reference Manual.)
Edited to add: If you only want to capture a specific subset of files, you can change *
to a more-specific pattern such as [0-9][0-9][0-9][0-9][0-9]
(which matches filenames consisting of five digits).
Incidentally, you can format the whole thing on one line:
for file in [0-9][0-9][0-9][0-9][0-9] ; do mv "$file" "${file:0:4}0${file:4}" ; done
Try using below code.
rename -v -n "s/(S01S0[0-2][0-9])/\$1-/" *
-n
to check the command output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With