Remove the latest modified file

7,261

Solution 1

The simple and reliable way to delete the latest file in the current directory is

zsh -c 'rm ./*(om[1])'

Of course, if you're running zsh, that's just

rm ./*(om[1])

Unlike other solutions proposed in this thread such as parsing the output of ls or of stat, this works no matter what strange characters the filename contains, even unprintable characters, even newlines.

The characters in parentheses after * are glob qualifiers. om means to sort by modification time (most recent first), and [1] means to select only the first file (after sorting).

If what you want is to get rid of a particular file, relying on the modification time is a complicated approach. Instead, type rm Space Tab and navigate through the completions that your shell offers until you hit the right file.

Solution 2

stat is the main ingredient in this recipe: remove the echo if you're satisfied it's working

echo rm "$(stat -c "%Y:%n" * | sort -t: -n | tail -1 | cut -d: -f2-)"

You don't specify your platform: this is Linux and GNU tools.

Note that this doesn't work if the file name contains a newline.

Solution 3

If you wish to remove the latest file in a directory named dir, and the file name doesn't contain a newline character, do this:

rm -i -- "$(LC_CTYPE=C ls -t dir | head -1)"

Beware that if the file name contains unprintable characters, this may not work because ls may mangle the unprintable characters.

If the latest file in the directory is another directory you'll get an error such as rm: cannot remove ‘dir2’: Is a directory.

Share:
7,261

Related videos on Youtube

teja
Author by

teja

Updated on September 18, 2022

Comments

  • teja
    teja almost 2 years

    I have created two files: sample.txt and sample.txt  (the second file contains some hidden characters, e.g. a space). How can I remove the latest modified file? I'm using Linux.

    • Admin
      Admin over 8 years
      rm "sample.txt "
    • teja
      teja over 8 years
      thanks for your response but i just want to delete latest file without the help of filename
    • Admin
      Admin over 8 years
      What do you mean by "latest"? Most recently created?
    • sinclair
      sinclair over 8 years
      two files with the same name in one directory? oO
    • teja
      teja over 8 years
      i have created two files manually with the same file name but the second file contains some hidden characters. i just want the command to remove the recently created file(i.e., second file)
    • teja
      teja over 8 years
      yeah these two files are in one directory
    • sinclair
      sinclair over 8 years
      rm "sample.txt "
    • teja
      teja over 8 years
      without the help of filename i have to delete/remove
    • Jeff Schaller
      Jeff Schaller over 8 years
    • Admin
      Admin over 8 years
      Suddenly, I realize this looks like a homework question.
    • Jeff Schaller
      Jeff Schaller about 7 years
      If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you!
  • Marius
    Marius over 8 years
    OP already said the filename contains special characters, which won't work with ls.
  • dave_thompson_085
    dave_thompson_085 over 8 years
    @ThomasDickey anything but newline (which wasn't mentioned) is fine for ls; the problem for space etc is unquoted command substitution, which can be fixed by quoting it. Also note this selects the last modified file, which is not necessarily the last created. And on older filesystems, it's not reliable if the files were modified/created within the same second.
  • dave_thompson_085
    dave_thompson_085 over 8 years
    xargs discards space (as mentioned by the OP) and also quotes and backslash (not mentioned) from the arguments it parses and passes. With GNU xargs and one value per line (as here) this can be fixed with -d'\n'