Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

shell - Linux - How to replace the last few characters of a file name with a string

I have list of files and each file name ends with (+1) and I want to replace that (+1) of the filename with date

Ex: ABC.DEF(+1)

Looking for a simple solution which replaces the file name as below:

ABC.DEF.20200127

question from:https://stackoverflow.com/questions/65929663/linux-how-to-replace-the-last-few-characters-of-a-file-name-with-a-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am not sure about the extension of your file, so I just considered .txt

for file in *.txt;   
do   
    mv "$file" "${file%????.txt}."`date +"%y%m%d"`""  
done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...