In Linux, one folder is created every morning and 5 files are created in that folder.

At the end of the day (Midnight), the last two files in that folder have to be deleted. How can I accomplish this?

  • love to help give us more information. Is it a new folder each day if so what is the filename pattern. Same question for the files that need to be deleted. What have you tried so far? – rob Jun 5 '14 at 7:32
  • 5
    With folder do you mean directory? What determines what the 'last two' files are? The order of creation? Please give the name and setup of some real life software that needs this (so we can give better feedback, and so we don't get the impression of doing your homework assignment for you). – Anthon Jun 5 '14 at 8:00
  • use the rm command. – ChuckCottrill Jun 6 '14 at 2:04

Without knowing more information you could use this command to select the last 2 files in that directory and delete them. This assumes you want the last 2 files that were modified, deleted.

$ ls -t | head -n 2 | xargs rm -f


Say I have these files.

$ seq 5 | xargs -n 1 touch
$ ls -ltr
total 0
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 1
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 2
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 3
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 4
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 5

Using ls -t | head -n 2 would give me the last 2 modified files.

$ ls -t | head -n 2

And I can pass those to xargs rm -f to delete them.

$ ls -t | head -n 2 | xargs rm -f
$ ls -tr
total 0
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 1
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 2
-rw-rw-r--. 1 saml saml 0 Jun  5 04:01 3
  • 1
    Why would you reverse the list (-r) and use tail instead of using head? Note that it assumes file names don't contain space, tab, newline, single quote, double quote or backslash characters and don't start with -. Also note that -1 is superfluous as ls only outputs on multiple columns if its output goes to a terminal. The tail -2 syntax is deprecated. Use tail -n 2 instead. Also note that ls -t sorts by last modification time, not creation time and omits hidden files. – Stéphane Chazelas Jun 5 '14 at 11:29
  • @StéphaneChazelas - I reversed it so that the solution corresponded w/ how the OP envisioned solving the problem, i.e. removing the last 2 most recently added/modified files. I've fixed the tail -n 2 and ls -1 I've fixed. Changed text to say "modified" instead of "created". W/o more info from OP this was an approximation of a solution. – slm Jun 5 '14 at 12:18
  • 1
    What I meant is that ls -t | head -n 2 is the same as ls -rt | tail -n 2 – Stéphane Chazelas Jun 5 '14 at 12:29
  • @StéphaneChazelas - OK, changed those too. – slm Jun 5 '14 at 12:34

With zsh

rm -f -- *(D.om[1,2])

would delete (up to) the two newest (in terms of last modification time) regular files in the current directory.

With GNU tools:

eval "files=($(ls -At --quoting-style=shell-always))"
for f in "${files[@]}"; do
  if [[ -f $f && ! -L $f ]]; then
    rm -f -- "$f"
    ((--n)) || break
  • is zsh going to be default shell in upcoming server OS ? – Rahul Patil Jun 5 '14 at 12:43
  • 1
    Yes, one day, zsh will rule the world. For now though, no major Unices beside OS/X (it even used to be the system shell there) include it in their default base installation. That's probably the only reason why you might want to use any other shell ;-) – Stéphane Chazelas Jun 5 '14 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.