Results 1 to 3 of 3

Thread: Remove last file in a directory with the command line

  1. #1
    Join Date
    Oct 2013
    Beans
    1

    Smile Remove last file in a directory with the command line

    Hello,

    I need to delete the last file in a list within a directory. How do I do that using the command line?

    rm "file_name" is not enough, as I have a huge list and I don't know the names of the files.

    Thanks in advance

  2. #2
    Join Date
    Mar 2010
    Location
    Metro-ATL
    Beans
    Hidden!
    Distro
    Lubuntu 12.04 Precise Pangolin

    Re: Remove last file in a directory with the command line

    I didn't script this, but
    \ls -t1 - will order the output by time in 1 column.
    tail -n 1 or 2 should grab the last 1 or 2 lines in the output.
    \ls -t1|tail -n 1

    Oops, that gets the oldest 1 file - you want the newest? - use head -n 1

    Then probably just pipe that into xargs.
    Does that make sense?

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Remove last file in a directory with the command line

    Hi, Mimo_Camussi and welcome to the forums!

    The output of ls is formated for humans and will cause bugs in scripts. Please check out a http://mywiki.wooledge.org/ParsingLs and BashFAQ 003 (link in my signature).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •