LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2006, 06:35 AM   #1
nitroid
Member
 
Registered: Jul 2004
Location: Finland
Distribution: Arch and Maemo (N900)
Posts: 32

Rep: Reputation: 15
Question 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
 
Old 04-13-2006, 06:58 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Using tail:

Code:
ls -tc /whatever/path                           | \
   tail -n1                                     | \
   awk '{print "rm \"/whatever/path/" $1 "\""}' | \
   bash
 
Old 04-13-2006, 07:01 AM   #3
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
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".
 
Old 04-13-2006, 07:08 AM   #4
nitroid
Member
 
Registered: Jul 2004
Location: Finland
Distribution: Arch and Maemo (N900)
Posts: 32

Original Poster
Rep: Reputation: 15
Smile

Thank you, that helped a lot.
 
  


Reply



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
Bash scripting question flagg0204 Programming 1 11-19-2005 03:31 PM
Bash Scripting Question fiod Linux - Newbie 4 11-19-2005 05:09 AM
bash scripting question bmfan Linux - Software 6 11-04-2005 08:54 PM
Bash Scripting Question jsimmonds Programming 2 06-16-2005 09:07 AM
BASH Scripting question flagg0204 Programming 4 12-23-2004 07:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 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
Open Source Consulting | Domain Registration