Thread: Bash: remove oldest file

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Bash: remove oldest file

    Hi,

    I'm trying to remove the oldest file (whenever there's 14 or more):

    Code:
    		
    		FILECOUNT=`ls | wc -l`;
    		if [ $FILECOUNT > 14 ]
    		then
    			cd "$PATH" && ls -tr | head -n 1 | xargx rm -f;
    			echo "Removed oldest file ...";
                    else
    			echo "No 14 files in the filesystem yet, nothing to remove";
    		fi
    I keep getting the following error though:

    ./mysqlbackup: line 1: ls: command not found
    ./mysqlbackup: line 1: wc: command not found
    No 14 files in the filesystem yet, nothing to remove
    Can anyone tell me what I'm doing wrong here ?

    Thnx in advance !
    Last edited by ReggieBE; 05-29-2007 at 09:14 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    PATH is a special variable. Apparently your script is overwriting its value and breaking path search. Never use a variable called PATH.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    Yes, that was the problem, thnx

    But it's not removing the file though .. is there any way to do this without xargx ? Cause it's not installed :\

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    cd "$PATH" && ls -tr | head -n 1 | read file &&  rm -f $file

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ReggieBE View Post
    Yes, that was the problem, thnx

    But it's not removing the file though .. is there any way to do this without xargx ? Cause it's not installed :\
    It's not xargx, it's xargs

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    lol ok thanks brewbuck and mcnamara, it's fixed !

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ReggieBE View Post
    lol ok thanks brewbuck and mcnamara, it's fixed !
    You can do it without xargs OR read:

    Code:
    cd "$MY_PATH" && rm -f `ls -tr | head -n 1`
    Those are backward single quotes, known as backticks. It takes the output of the pipeline within the backticks and substitutes it lexically into the command line. Very useful.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    cd "$MY_PATH" && rm -f `ls -tr | head -n 1`
    Those are backward single quotes, known as backticks. It takes the output of the pipeline within the backticks and substitutes it lexically into the command line. Very useful.
    That's the old-style syntax. For POSIX compatibility, one should be using $() instead of ``:
    Code:
    cd "$MY_PATH" && rm -f $(ls -tr | head -n 1)
    Besides portability (between newer systems, anyway), $() has several advantages. For example, you can nest them, unlike backquotes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    That's the old-style syntax. For POSIX compatibility, one should be using $() instead of ``:
    I don't really buy the portability argument. I don't know of any Bourne-style shell that does not understand the back ticks. However, I know of at least 4 systems within 100 feet of me that do NOT understand $().

    So in practice, I use back ticks because it's more portable.

    Besides portability (between newer systems, anyway), $() has several advantages. For example, you can nest them, unlike backquotes.
    I'd call the inability to nest back ticks a feature, not a disadvantage. Nesting substitutions leads to an unreadable mess.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't really buy the portability argument.
    How about portability between keyboards? Getting backticks on the console can be a real pain with German keyboards until the system is completely set up and tuned.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Backquotes are more portable now, simply because they have been around for a longer period of time. But POSIX-style shell substitution is part of the POSIX standard, and they make more sense anyway: the characters are easier to type, the characters are easy to distinguish (` can look like '), and they can be nested.

    I would use the POSIX style unless the shell doesn't support it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. file i/o and remove something
    By bnkslo in forum C Programming
    Replies: 22
    Last Post: 06-24-2008, 01:38 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM