LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to cut a string from a variable (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-cut-a-string-from-a-variable-4175458615/)

iruhdam 04-18-2013 07:35 AM

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...

druuna 04-18-2013 07:41 AM

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


iruhdam 04-18-2013 08:17 AM

basename works!!

Thanks :)

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

Thanks in advance.

towheedm 04-18-2013 08:19 AM

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.

grail 04-18-2013 12:54 PM

Or the equivalent of # being %:
Code:

$ fname="directory/files/file.h"
$ echo ${fname%/*}
directory/files



All times are GMT -5. The time now is 02:57 AM.