I want to batch re-name a number of files in a directory so that the preceding number and hypen are stripped from the file name.
Old file name: 2904495-XXX_01_xxxx_20130730235001_00000000.NEW
New file name: XXX_01_xxxx_20130730235001_00000000.NEW
How can I do this with a linux command?
You can use this tool: rnm
Code to do what you want:
-fo
is for file only mode-dp
is the depth of directory (-1
means unlimited depth).-rs
is replace string.\d+-
regex is being replaced with empty string.-ss
is search string, it searches for files with^\d+-
regex. (It could be omitted though, some harmless error messages would be printed on screen).I really like something as "rename *.mp3 *.mp4".
But none of other answers give me that. So I wrote a haskell program to do it.
https://hackage.haskell.org/package/batch-rename
With this you can do: batch_rename "DCIM*.jpg" "*.png"
There is also a handy GUI tool
pyRenamer
https://wiki.ubuntuusers.de/pyRenamer/This might look a little complex, but it's pretty effective and works well on both *nix and OSX systems. It also acts recursively, renaming files in the current directory as well as any subdirectories:
Here's a breakdown of what just happened:
The first line says to find (
find
) all files, starting with those in the current directory (.
), whose name matches the pattern (-regex
) of "7 numbers, followed by a dash, followed by 0 or more characters" ('.*/[0-9]\{7\}[-].*'
), and write those file names and their respective paths (-print
) to a file called temp1 (> temp1
). Note that the-print
directive is probably not necessary in most cases but it shouldn't hurt anything.Then, copy (
cp
) the contents of temp1 to a file called temp2.Next, open the file temp2 using the vi text editor and give vi two commands (using
-c
to signify each new command)::g
) for the same pattern we searched for above, except this time group the results using parentheses (\([0-9]\{7\}[-]\)\(.*\)
).\2
).:x
).The result of which being this:
Now, concatenate the lines from temp1 with those of temp2 (
paste
) and write each newly combined line to a file called temp3 (> temp3
).Next, run vi again, doing the same steps as above except this time search for the beginning of each line (
^
) inside the file temp3 and add mv and one space right after it (mv
).Then, execute the contents of temp3 (
./temp3
) as a shell script (sh
).Finally, remove (
rm
) each of the temporary files we created during the whole process.vimv lets you rename multiple files using Vim's text editing capabilities.
Entering vimv opens a Vim window which lists down all files and you can do pattern matching, visual select, etc to edit the names. After you exit Vim, the files will be renamed.
The screencast in the README file shows how it solves original poster's question.
[Disclaimer: I'm the author of the tool]
This should make it:
It gets from the beginning the block
[0-9]
(that is, numbers) many times, then the hyphen-
and deletes it from the file name.If
rename
is not in your machine, you can use a loop andmv
:Test
Or: