简体   繁体   中英

How to append the last motify date to a FileName suffix when cp a File

I want to append the last motify date to a FileName which will be copyed I just know how to append the current date to a File,like:

 find -name *.log  -exec cp {} {}__$(date "+%F")__bak \;

and test.log --> test.log__2012.12.24__bak

I want test.log --> test.log__{last_mofigy_date}__bak

But how can I append the last modify date to the fileName ,it is best if it can be done in one statement

One way:

$ for file in $(find .  -name *.log )
> do
>  x=$(stat -c %y $file)
>  echo cp $file ${file}_${x%% *}_bak
> done

This snippet will display the set of cp commands formed. If it is proper, remove the echo and run it to get the files copied.

Updated: Another way using just find and exec :

 find . -name *.log -exec bash -c 'x={} ; y=$(stat -c %y $x); echo cp $x ${x}_${y%% *}_bak ' \;

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