|
|
|
|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|||
|
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 | ||
|
|
|
|||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| shell script, shell scripting, unix scripting, unix scripting basics |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |