Remove number in file name

Hi ,

i have a file name with date and time append like

test_SEC_AES_V1_T03_2016031404306

i want to remove the date and time after _ and rename to current date and time,can any one please let me know as i tried some options but din't help:(

Thanks,

Please show your attempts.

And, please explain your date/time format 2016031404306 :
2016 - year
03 - month
14 - day
04 - hour?
30 - minute?
6 - ?

the above code is not working

---------- Post updated at 07:54 AM ---------- Previous update was at 07:43 AM ----------

yes 2016 - year
03 - month
14 - day
04 - hour
30 - minute
6 - sec

Hello caba_jones,

Welcome to forums. While seeing your code I think you need to rename all txt files into your present directory. If this is the case then following may help you in same.

for file in *.txt; do echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"; done

Above code will write the move command, so if you are ok with this then you could remove echo in bold above code and could change the .txt files to as per your need.
EDIT: Adding a non-one liner form of solution above too.

for file in *.txt
do
   echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"
done

Thanks,
R. Singh

will this code rename files in same folder?
when i execute its not renaming in the same folder.can you please let me know.

Hello caba_jones,

Yes, it will rename the files in current folder where you are running the script or command as mentioned above. In case we want it to take care of a folder where we are not currently in then you should mention the complete path of those .txt files as follows.

for file in /tmp/test/*.txt       ######Basically mention complete path of .txt files in here.
do
   echo mv $file ${file%_*}$(date +%Y%m%d%H%m)".txt"
done

As mentioned before you could remove echo from your code in case you are happy with the printed commands by echo .

Thanks,
R. Singh

1 Like
for file in ${DPATH}/*.txt.pgp; do  mv ${test}/${FN} ${FN%_*}$(date +%Y%m%d%H%m)"test.txt.pgp" ; done  
mv: cannot access (the above path)

i am not able to move the files even though i have 777 permission,can you please let me know any other reasons not able to move the file.

What be the contents of the variables DPATH , test , and FN ? Where are those variables defined?

Hello caba_jones,

Please use code tags as per forum rules into your posts. I think above error shows because command is not able to find files.
So could you please try following, seems you haven't used correct variable names.

DPATH=/your/actual/path/where_files_are_present
for file in ${DPATH}/*.txt.pgp; do  mv ${file}  ${file%_*}$(date +%Y%m%d%H%m)"test.txt.pgp" ; done 

I haven't tested above but you should make sure you are using correct variable names and value of variable named DPATH is set as per your system.

Thanks,
R. Singh

The permissions to rename a file are required on the directory. The file exists as just a collection of data blocks on disk somewhere and it is the directory that holds the file name, references when to find the data blocks, file permissions etc. To rename the file, you are actually editing the directory.

If you are in the appropriate directory/directories, can you run the following:-

id -G
ls -ld .

This will show some of your account details and the directory details.

Robin