LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2013, 07:35 AM   #1
iruhdam
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Rep: Reputation: Disabled
How to cut a string from a variable


Hi,

Say suppose "directory/files/file.h" path is saved in a variable.

How can i get the file name i.e "file.h" from the above path

Want the file name separetly.

Please help me...
 
Old 04-18-2013, 07:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You can use bash internals to do just that:
Code:
$ fname="directory/files/file.h"
$ echo ${fname##*/}
file.h
Have a look at the bash manual page (Parameter Expansion section).

EDIT: If bash cannot be used, try using the basename command:
Code:
$ basename $fname
file.h

Last edited by druuna; 04-18-2013 at 07:44 AM.
 
1 members found this post helpful.
Old 04-18-2013, 08:17 AM   #3
iruhdam
LQ Newbie
 
Registered: Apr 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up

basename works!!

Thanks

Is there any way i can get the path of that file "file.h"

Thanks in advance.
 
Old 04-18-2013, 08:19 AM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
basename - Strip directory and suffix from filename
dirname - strip last component from filename

Code:
dirname $fname
Code:
info bash
for a lot of other great information.

Last edited by towheedm; 04-18-2013 at 08:21 AM.
 
Old 04-18-2013, 12:54 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Or the equivalent of # being %:
Code:
$ fname="directory/files/file.h"
$ echo ${fname%/*}
directory/files
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 3 03-21-2012 02:47 PM
[SOLVED] Cut string with command tquang Linux - General 2 03-13-2012 01:55 AM
Help to cut a string deepudixit Programming 5 11-28-2009 09:21 AM
how to cut first four digit of a string kkpal Linux - Newbie 3 07-22-2008 03:32 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM


All times are GMT -5. The time now is 05:10 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration