LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting question (https://www.linuxquestions.org/questions/programming-9/bash-scripting-question-434749/)

nitroid 04-13-2006 06:35 AM

Bash scripting question
 
I'm making a bash script that has to (amongst other things I figured out myself) delete the oldest file in a directory. I have an idea how to do it, but there's something I can't translate from common sense into a bash script.

I can list the files sorted by date with 'ls -c /whatever/path' and get the number of files with 'ls /whatever/path | wc -w'. Now all I have to do is set a variable with its value of whatever is the last word 'ls -c' outputs, or word number [number of files]. How exactly can I do that?

Thanks in advance, if I made enough sense to get an answer :)

macemoneta 04-13-2006 06:58 AM

Using tail:

Code:

ls -tc /whatever/path                          | \
  tail -n1                                    | \
  awk '{print "rm \"/whatever/path/" $1 "\""}' | \
  bash


addy86 04-13-2006 07:01 AM

The following code will set the value of f to the name of the oldest file in /whatever/path.
Code:

f=`ls -1c /whatever/path | tail -n1`
Note: the option for ls is a "one", not an "ell".

nitroid 04-13-2006 07:08 AM

Thank you, that helped a lot.


All times are GMT -5. The time now is 11:36 PM.