Remove the latest modified file
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
.
Related videos on Youtube
teja
Updated on September 18, 2022Comments
-
teja almost 2 years
I have created two files:
sample.txt
andsample.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 over 8 yearsrm "sample.txt "
-
teja over 8 yearsthanks for your response but i just want to delete latest file without the help of filename
-
Admin over 8 yearsWhat do you mean by "latest"? Most recently created?
-
sinclair over 8 yearstwo files with the same name in one directory? oO
-
teja over 8 yearsi 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 over 8 yearsyeah these two files are in one directory
-
sinclair over 8 years
rm "sample.txt "
-
teja over 8 yearswithout the help of filename i have to delete/remove
-
Jeff Schaller over 8 years
-
Admin over 8 yearsSuddenly, I realize this looks like a homework question.
-
Jeff Schaller about 7 yearsIf any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you!
-
-
Marius over 8 yearsOP already said the filename contains special characters, which won't work with
ls
. -
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 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 GNUxargs
and one value per line (as here) this can be fixed with-d'\n'