Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-26-2008
Registered User
 

Join Date: Dec 2008
Posts: 3
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.
Sponsored Links
  #2 (permalink)  
Old 12-26-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,460
Wink 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 (permalink)  
Old 12-26-2008
Registered User
 

Join Date: Dec 2008
Posts: 3
Awesome thanks, but I think its blowing up on the use of the wildcard. I only want the TXT files. Any thoughts?
  #4 (permalink)  
Old 12-26-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,460
Wink

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 (permalink)  
Old 12-26-2008
Registered User
 

Join Date: Dec 2008
Posts: 3
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 (permalink)  
Old 12-26-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 4,962
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
Sponsored Links
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding the oldest file in a particular directory pavan_movva Shell Programming and Scripting 4 04-08-2009 08:24 AM
Find Oldest file in a directory tree rahulrathod Shell Programming and Scripting 25 01-12-2009 12:45 PM
how to grep the oldest file in a directory ericaworld Shell Programming and Scripting 1 05-29-2007 02:24 PM
Removing the oldest file in a directory pavan_movva Shell Programming and Scripting 2 10-10-2006 12:38 PM
Oldest File In A Directory bergerj3 UNIX for Dummies Questions & Answers 2 02-27-2002 03:31 PM



All times are GMT -4. The time now is 05:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0