简体   繁体   中英

Delete oldest file for each directory in a tree

I have a directory tree like the following:

Games
-- Game1
  -- file1
  -- file2
  -- file3
-- Game2
  -- file1
  -- file2
-- GameN
  -- filen

And I would like to delete the oldest file for each Game subdirectory.

Tried searching for the various questions already posted but didn't find a solution.

Thanks

If the files not containing special characters, like \n (no problem with files with spaces):

for dir in Games/Game*/; do
    (
        cd "$dir"
        echo rm "$(\ls -tr | sed q)"
    )
done

This is the only one case where I parse ls

Drop echo command if your attempts are satisfactory.

  • ls -tr sort files by date of modification in reverse order.
  • sed q take the first line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM