Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-05-2009
Registered User
 

Join Date: Dec 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How do I get only the file name from a listing?

Hi,
I am trying to get a file name only. Could anyone help me on the same.
Meaning I have a file say list.out which holds below output
./xyz/abc.txt
./xyz/hij.txt
I want an output as below
abc.txt
hij.txt
i.e I want to delete everything before abc.txt
Please help me out if any one has the ans.
Sponsored Links
  #2  
Old 12-05-2009
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 6,729
Thanks: 0
Thanked 53 Times in 50 Posts
Please use a more useful title next time. This cannot possibly be that urgent - i.e., your system will not crash if you don't know how to do this.

The command you want is basename

Code:
>echo basename /path/to/file/file.txt
file.txt
>

  #3  
Old 12-05-2009
scottn's Avatar
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 2,593
Thanks: 13
Thanked 91 Times in 88 Posts

Code:
xargs -n1 < list.out basename
abc.txt
hij.txt

  #4  
Old 12-05-2009
Registered User
 

Join Date: May 2008
Location: Chennai
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
cat list.out | awk -F "/" '{print $3}'
  #5  
Old 12-05-2009
scottn's Avatar
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 2,593
Thanks: 13
Thanked 91 Times in 88 Posts
Awk is incredibly much faster (more noticeable for larger list.out files) - and handles filenames with spaces, but I'd change it slightly


Code:
awk -F/ '{print $NF}' list.out

0.05 seconds for 8000+ files, versus 6.3 seconds using xargs.

(not that I'm a fan of the UUOC club - it took the same time with or without cat!)

It's even quicker than sed (0.09 seconds)

Code:
sed "s+.*/++" list.out


Last edited by scottn; 12-05-2009 at 12:08 PM..
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Rename multiple file from file listing yshahiac Shell Programming and Scripting 5 02-09-2009 03:42 PM
file listing ... thepurple Solaris 3 12-28-2007 08:39 AM
listing file with other format happyv Shell Programming and Scripting 3 06-26-2007 06:17 AM
Recursive directory listing without listing files psingh UNIX for Dummies Questions & Answers 4 05-10-2002 10:52 AM
File listing Astudent UNIX for Dummies Questions & Answers 5 10-25-2000 05:36 PM



All times are GMT -4. The time now is 07:39 AM.