Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solaris and ls command

Status
Not open for further replies.

podsds

MIS
Nov 8, 2002
20
US
On a Solaris 8 machine, and maybe other versions too ...

the command '# ls /export/home/root/steve' returns the following output '/export/home/root/steve', but is I cd to the directory and do 'ls steve' the command returns 'steve'

my question ... is there a way to omit the s command from returning the full path when a file is fully qualified?
 
Sounds to me like you may have an alias set for ls in your profile, have you tried /usr/bin/ls /export/home/root/steve

Or done a find on your server for ls ? What shell are you using ?
 
ls will return the full path if you have it in your command line. For example:

if you were in the directory /export/home/root/ and did a #ls /export/home/root/steve, it will return the full path.

As far as I know there is no option to use with ls to knock off the path.

If you are looking to use the output of the ls then I suggest using an awk or grep.
 
I am looking to extract from the output of the ls command just the file name, but haven't found a way to do it yet. I looked at things line cut or awk. Not knowing a better way ... I would count the number of slashes in the path and then cut or awk the last field. I think sed might be a better tool, but I have to research it.

Thanks
 
Have a look at the basename command - this should do what you want, e.g.

file=`basename /u/root/scripts/show_free`
echo $file
show_free
 
If you are writing this into a ksh script you can also

FILE=/a/b/c/d
echo ${F##*/}

will just output "d"

N.
 
ls /export/home/root/steve | awk -F/ '{print $NF}'

this shud solve ur problem.

 
ls /export/home/root/steve | sed 's;.*/;;'

It is all horses-for-courses

N.
 
Thanks all,

I now remember using basename in a shell script long ago, its what I was looking for, but I appreciate all other examples.
 
Because I know where the file is, just not the full name as the program that creates it tabs a date and time stamp on it. So ls command with wild card is used to extract some stats on the file and then i wish to use just the basename, no directory when reporting it to the user.

hope this helps ... but I am all set! the command basename has worked for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top