LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-24-2008, 03:52 AM   #1
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Rep: Reputation: 30
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*
/var/www/html/CSA20June/B_Berg/ipB_Berg200806200001.cgi
but i only want the filename(ipB_Berg200806200001.cgi) returned.

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
 
Old 07-24-2008, 04:09 AM   #2
dipuasks
Member
 
Registered: Oct 2005
Location: India
Distribution: Redhat 7-9,Fedora Core 3 - 9, RHEL 4 -5, CentOS 4 - 5, Ubuntu 7.10 - 12.10, Mandirva 2008 -2009
Posts: 133

Rep: Reputation: 16
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
 
Old 07-24-2008, 04:10 AM   #3
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
You can prefix it with the basename command.
Code:
basename `ls /some/long/path/*extension`
will give you only the filename.

edit: apparently basename takes only one arg. To workaround that, xargs seems to do fine:
Code:
ls /some/path/*txt | xargs -n1 basename

Last edited by indeliblestamp; 07-24-2008 at 04:17 AM. Reason: added better version
 
1 members found this post helpful.
Old 07-24-2008, 04:36 AM   #4
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by arungoodboy View Post
edit: apparently basename takes only one arg. To workaround that, xargs seems to do fine:
Code:
ls /some/path/*txt | xargs -n1 basename
This works perfectly thanks. I find it a bit strange that one can't do this directly from ls,but i guess as long as there is a way do do it, all is well.

Thanks
 
Old 07-24-2008, 04:58 AM   #5
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 40
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.
 
Old 03-06-2010, 05:29 PM   #6
toto10
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Another way to list files using ls using only ls

ls -m1
 
Old 03-06-2010, 05:47 PM   #7
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
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*
do
  echo ${f##/*/}
done
you could also test if $f is a direcotyr or not if you have directories you dont want to see....
 
Old 06-14-2013, 02:08 PM   #8
Bryan H
LQ Newbie
 
Registered: Jun 2013
Posts: 1

Rep: Reputation: Disabled
Not quite

In ls -m1 the -m is overkill

ls -1 is what you're actually doing (-1 overrides the other parameter)
 
Old 06-14-2013, 04:00 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by Bryan H View Post
In ls -m1 the -m is overkill

ls -1 is what you're actually doing (-1 overrides the other parameter)
Neither of those work on my machine...

Code:
$ ls /home/user/comps/srv1/chassis/*
/home/user/comps/srv1/chassis/SC846.pdf
$ ls -1 /home/user/comps/srv1/chassis/*
/home/user/comps/srv1/chassis/SC846.pdf
$ ls -m1 /home/user/comps/srv1/chassis/*
/home/user/comps/srv1/chassis/SC846.pdf
$ ls /home/user/comps/srv1/chassis/* | xargs -n1 basename
SC846.pdf
The man page for -1 says "list one file per line" and for -m says "fill width with a comma separated list of entries", neither of which claim to do what the OP is asking, and neither of which actually do what the OP is asking either (at least not on my machine).

Last edited by suicidaleggroll; 06-14-2013 at 04:02 PM.
 
1 members found this post helpful.
Old 06-14-2013, 04:05 PM   #10
linuxCode
Member
 
Registered: Jun 2013
Location: New York City
Distribution: Arch
Posts: 33

Rep: Reputation: 44
Quote:
Originally Posted by arungoodboy View Post
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.
The find command can list filenames recursively and omit the full paths by using printf

Example
Code:
find /path/to/directory/ -type f <options> -printf "%f\n"
This will go through the directory you listed including subs and show only the filenames without the full paths.

Last edited by linuxCode; 06-14-2013 at 04:40 PM.
 
1 members found this post helpful.
Old 06-14-2013, 05:53 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
http://mywiki.wooledge.org/ParsingLs as in indeed one shouldn't use 'ls' but find.
 
Old 06-16-2013, 04:51 AM   #12
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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.
 
Old 06-17-2013, 12:21 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
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.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell: tr can only be used when given full path (despite being in $PATH) chochem Programming 2 06-27-2008 08:37 AM
using commands to output path, filename and info to a txt file bob_man_uk Linux - General 3 05-11-2006 02:31 PM
how to seperate filename from path in perl mengle Programming 2 08-28-2005 01:35 AM
how do i get the path of the filename linuxzouk Programming 25 06-10-2004 08:00 AM
path and filename of compressed kernel ? ohernandez Linux - General 3 05-29-2002 06:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration