简体   繁体   中英

Append date to a filename in linux

I want add the date next to a filename ("somefile.txt"). For example: somefile_DDMMYYYYHHMMSS.txt

Maybe a script will do or some command in the terminal window. I'm using Linux.

Thanks in advance.

Something like can do the work:

a=somefile.txt
bname="${a%.*}"
ename="${a##*.}"
date=$(date +%d%m%Y%H%M%S)
echo ${bname}_${date}.${ename}

You can also try this.

This code will list all files in the current directory and renames all the files to specified format.

#!/bin/bash -e
TDATE=$(date +%d%m%Y%H%M%S)
for FULLFILE in `ls`
do
FILENAME=`echo $FULLFILE | cut -d '.' -f1`
EXTN=`echo $FULLFILE | cut -d '.' -f2`
mv ${FULLFILE} ${FILENAME}_${TDATE}.${EXTN}
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM