I believe I'll ask two separate questions, but the context is the same, so I hope it's alright.

I have an output of few processes as presented below (which I can't modify).


What I'm trying to achieve with this input is the 'simple operation' of inserting the date into the filename or substitute that filename. Desired date usually corresponds to last modified date, so it's doable with for f in *.csv; do mv -- "$f" "$f-$(date +%Y%m%d -r "${f}")"; done and renaming oneliner. Sometimes the last modified date is a one day away from what I look for, so I'm wondering if there is an easy way (oneliner?) to decrease that numeric value (e.g. to transform 20160506 into 20160505)?


The second case. Last modified date is not useful, since it's completely incorrect. But the number in the filename increases along with expected date. Can I insert some numeric value into the filename and have the same value increase by one for each subsequent file? You see what I'm trying to accomplish? I just don't know how ;)...

EDIT I've found an answer for my question here - kind of... Maybe I just can't use it?

for i in *; do
  let cnt=cnt+1
  mv "$i" "$(echo "${i}_${cnt}" | awk -F_ '{print $1"_"$3}')"

I know how to modify it for my needs, but in the end the output is the same for all files, like the 'incrementation is not working'. I've saved code from above as rename.sh and remembered about allowing its execution by chmod 755 rename.sh, but still I came up with nothing...

  • 1
    In which situations is the "Sometimes last modified date is a one day away from what I look for" ? Otherwise, we can't hope to provide the "right" answer. The second case may be easier to accomplish; if so, would that solve your problem, or are these two independent scenarios?
    – Jeff Schaller
    Commented May 18, 2016 at 18:10
  • the date utility varies quite a bit between particular implementations, but with date (GNU coreutils) 8.23, one can add a variety of date modifiers to the "-d" switch: date +%Y%m%d -d '-1day' Commented May 18, 2016 at 18:25
  • It happens, when the process is somehow delayed and starts few hours later (next day), which results in 'not desired' last modified date. I believe the answer for only second scenario would be enough for me. Shame on my 8.21 GNU coreutils ;(.
    – Je.dno
    Commented May 18, 2016 at 19:32
  • What timezone are you in? It might be possible to adjust TZ by a few hours to get the dates you need Commented May 19, 2016 at 7:18
  • It's an outside process, so the timestamp is written on the server. I only get the access to the output like I've showed.
    – Je.dno
    Commented May 19, 2016 at 8:30

1 Answer 1


I hope it won't be a crime, to answer my own question. I've found partial solution at AskUbuntu - works for me, at least in the first case.

touch -d "$(date -R -r filename) - 2 hours" filename

And for modification of all files in subfolder, type:

find DIRECTORY -print | while read filename; do
    # do whatever you want with the file
    touch -d "$(date -R -r "$filename") - 2 hours" "$filename"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .