Create Bash script to change modification time of files

scriptName=""; echo -e '#!/bin/sh\n' > "${scriptName}"; cat files.txt | while read file; do echo "touch ${file}; sleep 0.5;" >> "${scriptName}"; done; chmod +x "${scriptName}";
Create a bash script to change the modification time for each file in 'files.txt' such that they are in the same order as in 'files.txt' File name for bash script specified by variable, 'scriptName'. It is made an executable once writing into it has been completed.
Sample Output


touch 01_intro.compressed.pdf; sleep 0.5;
touch 02_points.compressed.pdf; sleep 0.5;
touch 03_points.pdf; sleep 0.5;

These Might Interest You

  • This loop will finish if a file hasn't changed in the last 10 seconds. . It checks the file's modification timestamp against the clock. If 10 seconds have elapsed without any change to the file, then the loop ends. . This script will give a false positive if there's a 10 second delay between updates, e.g. due to network congestion . How does it work? 'date +%s' gives the current time in seconds 'stat -c %Y' gives the file's last modification time in seconds '$(( ))' is bash's way of doing maths '[ X -lt 10 ]' tests the result is Less Than 10 otherwise sleep for 1 second and repeat . Note: Clever as this script is, inotify is smarter. Show Sample Output

    while [ $(( $(date +%s) - $(stat -c %Y FILENAME) )) -lt 10 ]; do sleep 1; done; echo DONE
    flatcap · 2015-05-09 12:30:13 0
  • -a for access time, -m for modification time, -c do not create any files, -t timestamp

    touch -amct [[CC]YY]MMDDhhmm[.ss] FILE
    sharfah · 2009-05-27 14:33:22 0
  • find and normal files and list them sorting with modification time without group l: with detailed information t: sort with modification time r: reverse order h: show file's size in human-readable format, such as K(kilobytes), M(megabyes) etc. g: do not show group Show Sample Output

    find . -type f | xargs ls -ltrhg
    emacs · 2010-05-28 01:23:53 1
  • remove files with access time older than a given date. If you want to remove files with a given modification time replace %A@ with %T@. Use %C@ for the modification time. The time is expressed in epoc but is easy to use any other format. Show Sample Output

    find <dir> -printf '%p : %A@\n' | awk '{FS=" : " ; if($2 < <time in epoc> ) print $1 ;}' | xargs rm --verbose -fr ;
    angleto · 2009-11-20 16:31:58 6

What Others Think

Looks like another job for sed. Also, I'd use a sub-shell to tidy the redirection, e.g. (echo hello; echo world) > output.txt . script=""; (echo -e '#!/bin/sh\n'; sed 's/.*/touch \0; sleep 0.5;/') > $script; chmod +x $script . Note, you only need to surround variables with {} when there's risk of misinterpretation, e.g. ${hello}world != $helloworld
flatcap · 109 weeks and 5 days ago
for F in $(<files.txt) ; do touch $F ; done xargs -a files.txt -n 1 touch
pdxdoughnut · 109 weeks and 4 days ago
for file in $(cat files.txt); do touch ${file}; sleep .5; done ought to be a fast one-liner to do this. faster if you don't care about cpu or perfection: for file in $(cat files.txt); do ( touch ${file} ) &; done
sixsicsix · 109 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: