LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2013, 11:02 PM   #1
Shimdidly
LQ Newbie
 
Registered: Mar 2012
Posts: 21

Rep: Reputation: Disabled
Trying to remove oldest file in directory


Code:
ls /media/flashdrive/ | head -1
produces:
backup123.sql

Why doesn't this work?
Code:
ls /media/flashdrive/ | head -1 | rm -i
It says: rm: missing operand

Last edited by Shimdidly; 07-10-2013 at 11:03 PM.
 
Old 07-10-2013, 11:07 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 3,880
Blog Entries: 1

Rep: Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998Reputation: 1998
Quote:
Originally Posted by Shimdidly View Post
Code:
ls /media/flashdrive/ | head -1
produces:
backup123.sql

Why doesn't this work?
Code:
ls /media/flashdrive/ | head -1 | rm -i
It says: rm: missing operand
Because there is no file name visible to rm...

try ...|xargs rm -i
 
Old 07-10-2013, 11:11 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
ls sorts in alphebetical order, if you want to sort by modification time you need to use the "-t" flag.

Code:
ls -rt | tail -1
Will give you the name of the oldest file, you can use xargs to pipe it to rm for removal

Code:
ls -rt | tail -1 | xargs rm -i
Just beware of files with spaces in them, this approach won't handle them properly. You'd need find with the "-print0" flag and the "-0" flag to xargs to handle files with spaces in them properly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a script that removes the oldest files on /tmp directory danndp Linux - Newbie 6 11-18-2010 10:47 AM
[ncftp] how to delete oldest directory and file using ncftp commands booyeeka Linux - Newbie 3 08-17-2010 12:23 PM
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 05:58 AM
GLIB - two versions. how to remove oldest? zivs Linux - Newbie 4 06-18-2006 02:09 PM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 04:44 PM


All times are GMT -5. The time now is 12:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration