Setting Variable to oldest file in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting Variable to oldest file in a directory
# 1  
Old 12-26-2008
Setting Variable to oldest file in a directory

I am using a bash script to perform some automated maintenance on files in a directory. When I run the script using [user@server /usr/utilities]$sh -x script.sh <directory> the script works fine. It sets the variable to the oldest file, and continues on. However when I run the script like this [user@server /usr/utilities]$./script.sh <directory>, it never sets the variable which blows up my script. Here is how I am setting the variable.

cd $1
getoldestfile=`ls -lat *.TXT | grep ^- | tail -1 | head -1`
file=`echo $getoldestfile |cut -d " " -f 9`

Any help would greatly be appreciated.
# 2  
Old 12-26-2008
Hammer & Screwdriver Simplify you setting command

No need for the head after the tail; also perhaps place the paramater for the grep in " " to keep it clean.

Code:
getoldestfile=`ls -lat | grep "^-" | tail -1`

# 3  
Old 12-26-2008
Awesome thanks, but I think its blowing up on the use of the wildcard. I only want the TXT files. Any thoughts?
# 4  
Old 12-26-2008
Hammer & Screwdriver

Are you passing the directory as $1?
Are you only trying to look at that directory for *.TXT files?

If so, you might augment your script with something like:

mydir=$1
...
getoldestfile=`ls -lat ${mydir}\*.TXT | grep "^-" | tail -1`
# 5  
Old 12-26-2008
I gave it a try, unfortunately here is the result:
ls: /usr/tmp/*.TXT: No such file or directory
bash is looking at the * as literal so its literally trying to list *.TXT which will never be there.
# 6  
Old 12-26-2008
The backslash prevents the shell to expand the *. This should work, assuming the variable has a slash as last character:

Code:
getoldestfile=`ls -lat ${mydir}*.TXT | grep "^-" | tail -1`

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. If there are 3folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Bash to select oldest folder in directory automatically and log process

The `bash` below uses the oldest folder in the specified directory and logs it. The goes though an analysis process and creates a log. My problem is that if there are 3 folders in the directory folder1,folder2,folder3, the bash is using folder2 for the analysis eventhough folder1 is the oldest... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Deleting the oldest file in a directory

Hey! I have found similar posts both here and on other sites regarding this, but I cannot seem to get my script to work. I want to delete the oldest file in a test directory if there are more than two files. My script is currently: #!/bin/bash MEPATH=/usr/local/bin/test FILECOUNT=`ls... (4 Replies)
Discussion started by: Immolation
4 Replies

4. UNIX and Linux Applications

Finding the oldest file in a directory without ls

I am trying to determine the oldest and most recent files in a huge directory. I am using an ls -tr statement outside my find statement. The directory is too big and I am getting an "arg list too long" error. Is there something I can put in my find statement that doesn't create a list to... (2 Replies)
Discussion started by: hiyofjord
2 Replies

5. Shell Programming and Scripting

setting directory variable?

I have the following script and would like to know how to set the variable correctly. FTP downloads the .rpm to the current directory, so the RPM command needs to know the directory it is in. I could use ./test-application-1.0.i386.rpm to execute the command, but is there a way to set the... (6 Replies)
Discussion started by: nolamiami
6 Replies

6. Shell Programming and Scripting

Finding the oldest file in a particular directory

Hi all, I am a newbie to scripting and I need your help regarding finding the oldest file in a particular directory. My intention is to remove that oldest file. Are there any options available with the "find" command to do this.. Thanks in advance for your help Pavan (4 Replies)
Discussion started by: pavan_movva
4 Replies

7. Shell Programming and Scripting

Find Oldest file in a directory tree

This might just be one command. Any1 having the solution? Thanks, Rahul. (25 Replies)
Discussion started by: rahulrathod
25 Replies

8. Shell Programming and Scripting

how to grep the oldest file in a directory

Hi, Please help me out I want to grep the oldest file in a directory, could I use "ls" command? and how? thanx in advance (1 Reply)
Discussion started by: ericaworld
1 Replies

9. Shell Programming and Scripting

Removing the oldest file in a directory

Hi all, I need your assistance in removing the oldest file in a directory. I posted the same thread 3 days back and I got the following answer ls -1 -t | tail -1 | xargs rm which is not covering the case when there are directories older than the oldest file. So, could you please... (2 Replies)
Discussion started by: pavan_movva
2 Replies

10. UNIX for Dummies Questions & Answers

Oldest File In A Directory

I'm writing a script to find the oldest file in a directory. I know this can be done by using ls -rt | tail -1 but these are rather large directories and that can be somewhat slow since the script will be running constantly. Are there any other ways to do this that would be faster? I looked to... (2 Replies)
Discussion started by: bergerj3
2 Replies
Login or Register to Ask a Question