Ubuntu: Renaming date-stamped files to the previous day



Question:

I want to rename the files in a directory to sequential numbers, based on this rule :
filename-[some date] => filename-[previous day's date]

For example, file20170329 file20170328 and file20170327 are to be renamed to file20170328 file20170327 and file20170326.

I have like 100 file, so I need a better trick than renaming each file individually.


Solution:1

The following script:

  • for each file
  • If filename is greater than 8 (i.e. is able to hold a date)
  • basefilename hold the filename without the last 8 chars
  • TODAY - holds the last 8 chars (the date)
  • YESTERDAY - holds one day before $TODAY as 8 chars
  • Instead of using mv I created a /tmp/storage folder, and the script will copy the files to that location. This way, renaming one file won't corrupt another file if by any chance the basefilename for both files is the same, but the date is differ in one day

Note: In order to allow the script to execute the cp one should remove the echo from the echo cp ... line

mkdir -p /tmp/storage  for f in *  do    if [ "${#f}" -gt 8 ]; then      filebasename=${f::-8}      TODAY=${f: -8}      YESTERDAY=`date '+%C%y%m%d' -d "$TODAY-1 days"`      echo cp $f /tmp/storage/${f//$TODAY}$YESTERDAY;     fi  done  

example:

$ ls *20*  aaa2hhh20170329  aaahhh20170329  xajds20170222  xajsxs20170224  

script result:

cp aaa2hhh20170329 /tmp/storage/aaa2hhh20170328  cp aaahhh20170329 /tmp/storage/aaahhh20170328  cp xajds20170222 /tmp/storage/xajds20170221  cp xajsxs20170224 /tmp/storage/xajsxs20170223  


Solution:2

If you have 100 files (and have access to perl rename, generally the case in Ubuntu) you could use:

for n in {100..1}; do rename -n "s/$(date --date="$n days ago" +"%Y%m%d")/$(date --date="$(($n+1)) days ago" +"%Y%m%d")/" *; done  

Now, when you use -n for testing, it complains like this:

rename(file20170217, file20170216)  file20170218 not renamed: file20170217 already exists  file20170219 not renamed: file20170218 already exists  

But when you run it without -n the file no longer exists, so the renaming is successful:

for n in {100..1}; do rename "s/$(date --date="$n days ago" +"%Y%m%d")/$(date --date="$(($n+1)) days ago" +"%Y%m%d")/" *; done  

If you have more than 100 files, you need to increase the range to {150..1} or whatever.

This is very slow. It will take maybe 2 minutes or more to rename 100 files. But it works.


Solution:3

or if you prefer GUI approach, give krename a try, it has a lot of modules and you can write custom scripts in it, "date system function", "date time plugin" could work for youKrename GUI for plugins

http://www.krename.net/home/

sudo apt-get install krename  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »