Extracting file name from path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting file name from path
# 1  
Old 02-02-2010
Extracting file name from path

if I have a variable value "filetoprocess" equal to "/aaa/bbb/ccc"

How can I simply extract the file name of "ccc"
# 2  
Old 02-02-2010
Code:
filename=${filetoprocess##*/}

# 3  
Old 02-02-2010
Hi Franklin,

could you please explain the solution?

thanks in advance.
# 4  
Old 02-02-2010
Quote:
Originally Posted by dragon.1431
Hi Franklin,

could you please explain the solution?

thanks in advance.
I can't explain it better then this:

Learning the Korn Shell, 2nd Edition: Chapter 4: Basic Shell Programming Pattern-Matching Operators

Smilie
# 5  
Old 02-02-2010
You could also use basename.

Code:
basename "/aaa/bbb/ccc"

or to put it in a variable
Code:
filename=`basename "/aaa/bbb/ccc"`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

4. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 Replies

5. Shell Programming and Scripting

Extracting Directory From Path

Hi guys. I'm doing some bash scripting and have run into a snag. Say I have the path: /home/one/two/three/ All I need is the 'three' while making a filename. Is there an easy way to do this? I've tried using grep (because I'm that smart.) cut (as I'm unable to tell how many fields there... (3 Replies)
Discussion started by: Drayol
3 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. UNIX for Dummies Questions & Answers

Extracting parts from an absolute path

Hi, How can I extract parts from an absolute path? For example : The absolute path is /dir1/dir2/dir3/dir4/dir5.I need the relative path starting with directory given as parameter : for instance if the parameter is dir3 then the result should be dir3/dir4/dir5 I need generic solution... (9 Replies)
Discussion started by: mortanon
9 Replies

8. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies

9. Shell Programming and Scripting

extracting a field from directory path ??????

Hi I'll be getting a directory path as the input to the script. E.g. 1 abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad e.g. 2 sadfg/sadgjhgds/sd/dtuc/cghcx/dtyue/dfghsdd/XXX/qytq This input will be stored in a variable. My query is how to extract the field in a variable VAR which occurs... (15 Replies)
Discussion started by: skyineyes
15 Replies

10. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

Hi, I have a file with about 60 lines of path: app-defaults/boxXYZ....... I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command. Is there any other quick way to do this? Thanks ;) (2 Replies)
Discussion started by: Yinzer955i
2 Replies
Login or Register to Ask a Question