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;

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 · 294 weeks and 1 day ago
for F in $(<files.txt) ; do touch $F ; done xargs -a files.txt -n 1 touch
pdxdoughnut · 294 weeks 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 · 293 weeks and 5 days ago
took me time to read the forum but its actually great. People should know about this more.
Killersmile · 36 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: