I have a list of a few hundred files. They are currently as below:


etc., all the way up to

filename.New_<current date>.dat

I have a process that currently looks to pick files up from a landing directory, but in a totally different file name format.

What i want to do is rename the files but retain the date. For example, if i was to change one file it would be like:

$ mv filename.New_20160902.dat FILE_NAME_NEW.20160902.dat

Easily done for one file, but how can i do this for a list of files. I want to extract the date part of the old filename, store it as a variable and mv that old file to a new file with the new filename, keeping the date part the same.



3 Answers 3



rename -n s/filename.New_/FILE_NAME_NEW./ filename.New*

If you are OK with proposed renaming, run without -n


Try something like this

for x in *; do
    mv "${x}" FILE_NAME_NEW."${x#filename.New_}"

Use echo in front of mv first, so you can see what the effect is before renaming the files. "${x}" is the variable with your filename (e.g. filename.New_20160901.dat) and "${x#filename.New_}" is the filename with filename.New_ removed if it is at the beginning (e.g. 20160901.dat).


To match a file that contains the time stamp (and not other files) use:


for x in filename.New_[0-9]*.dat; do
    echo "file --> $x"

    echo    mv "${x}" "${x/filename.New_/FILE_NAME_NEW.}"

Remove the echo in front of mv to actually perform the move (after you are happy with the files selected with the script).

You must log in to answer this question.

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