inserting modified date into filename – 2 cases Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of inserting modified date into filename – 2 cases without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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 and remembered about allowing its execution by chmod 755, but still I came up with nothing…


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"
We are here to answer your question about inserting modified date into filename – 2 cases - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji