ls - Do not list full path only filename
Hi,
I'm doing the following command which gives me all the matching entries,but i'd like to see only the filename,not the full path,so i'm doing this.. Code:
[root@accountingdb scripts]# ls --format single-column /var/www/html/CSA20June/B_Berg/*20080620* IS there some way in ls to throw away the full path, and only list the filename? I went through the man pages, but i could not find anything. I've tried some pipe commands to grep as well, but my grep skills is not nearly good enough it seems. Any suggestions how can i achieve this? Thanks |
First redirect the whole output to a file say result.txt
Then count the number of / comes in the file path(I hope all the files are from same folder as per your above query) and run this command: cut -d / -f# /path/to/result.txt Where # is the number of slashes+1 i.e, for the path /var/www/html/CSA20June/B_Berg/ipB_Berg200806200001.cgi, the # should be 7. I am not a scripting master, I had got this solution for a similar problem earlier and it works! --Dipu ---------------------------- Windows? reboot. Linux? Be root!! http://www.smartdipu.info |
You can prefix it with the basename command.
Code:
basename `ls /some/long/path/*extension` edit: apparently basename takes only one arg. To workaround that, xargs seems to do fine: Code:
ls /some/path/*txt | xargs -n1 basename |
Quote:
Thanks |
If you actually cd to the folder first and then do ls, you'll get the filename like you want it. But when you pass the path to ls, it displays that path followed by the filename.
|
Another way to list files using ls using only ls
ls -m1
|
looks like ls -m1 is the weiner...
but if you need a loop to process the files.. id go like this Code:
for f in /var/www/html/CSA20June/B_Berg/*20080620* |
Not quite
In ls -m1 the -m is overkill
ls -1 is what you're actually doing (-1 overrides the other parameter) |
Quote:
Code:
$ ls /home/user/comps/srv1/chassis/* |
Quote:
Example Code:
find /path/to/directory/ -type f <options> -printf "%f\n" |
http://mywiki.wooledge.org/ParsingLs as in indeed one shouldn't use 'ls' but find.
|
Well, yes, you shouldn't really parse the output of 'ls', because it can, potentially, be quite variable over releases and with different set-ups, but just this once...
i) Does 'ls -D -1' do what you are asking for? I can't say that I know all that much about 'dired' mode, but superficially the output looks quite similar to what you are asking for, once it has been put into single column mode. Obviously, all the warnings of imminent doom, bad karma and possibly even garden gnomes and stone cladding, from the wooledge link apply. ii) If you were going to parse the 'path...filename' output to just give 'filename', you would want to use 'basename' to strip, errr, the basename from the full path string (while busily noting, but ignoring for this application, its partner 'dirname'). I don't see any case whatever for doing this, but you should probably note the existence of basename/dirname in case that you have need of them, in future. |
I agree with suicidaleggroll, none of the ls switches work for what the OP asked, (inc -D -1).
Use xargs, or param expansion (post #7) or (better) find. |
All times are GMT -5. The time now is 09:13 PM. |