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.

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

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

Awesome thanks, but I think its blowing up on the use of the wildcard. I only want the TXT files. Any thoughts?

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`

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.

The backslash prevents the shell to expand the *. This should work, assuming the variable has a slash as last character:

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

Regards