0

I want to touch the files in a directory so that the first file in the directory (by alphabetical ordering) was touched last.

I am having trouble even referencing the current directory in the function. I've tried:

for f in $PWD ; touch f; done

and this returns an error. But I think this would, even if it worked, be touching the files in order, and I want to do this in reverse.

  • Possible duplicate of [How to reverse a for loop in a bash script](https://stackoverflow.com/questions/47675910/how-to-reverse-a-for-loop-in-a-bash-script) – Hamms Sep 12 '19 at 21:33

1 Answers1

0

That fragment for f in $PWD can't work, because $PWD gives you the name of a directory, not a list of the files in the directory.

I would try something like

ls | sort -r | while read filename; do touch "$filename"; sleep 1; done

ls gives you a list of files. sort -r sorts it into reverse order. And then the while read filename; do ... done loop does something for each filename. I put a sleep call in the loop to ensure the files will actually end up with different timestamps.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103