Create Bash script to change modification time of files

scriptName="reorder_files.sh"; 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
Input:
01_intro.compressed.pdf
02_points.compressed.pdf
03_points.pdf

Output:
#!/bin/sh

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="reorder_files.sh"; (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 · 126 weeks ago
for F in $(<files.txt) ; do touch $F ; done xargs -a files.txt -n 1 touch
pdxdoughnut · 125 weeks and 5 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 · 125 weeks and 3 days 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: