1

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

filename.New_20160901.dat
filename.New_20160902.dat
...

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.

Thanks

1

try

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

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

0

Try something like this

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

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).

0

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

#!/bin/bash 

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.