views:

2483

answers:

4

if I have a string like some/unknown/amount/of/sub/folder/file.txt how can I get only the the file.txt sub string, remove the front part, while the length is unknown.

thank you

EDIT: file name could be any length, and sub folders could be any levels.

+2  A: 

Use basename command:

orig_path="some/unknown/amount/of/sub/folder/file.txt"
last_comp=$(basename $orig_path)
echo $last_comp
Matthew Flaschen
thank you, that really helped
derrdji
+1  A: 
$ basename "some/unknown/amount/of/sub/folder/file.txt"
file.txt

To generically extract a substring, you can use this syntax

$ hello="abcdef"
$ echo ${hello:1:3}
bcd
Stefano Borini
+3  A: 

basename some/unknown/amount/of/sub/folder/file.txt

gonzo
thank you for replying so fast
derrdji
+1  A: 

While I agree that the correct answer is to invoke basename, in bash you can also use ## to delete the longest occurrence of a string from the start of a variable.

bash-3.2$ t=/this/is/a/path
bash-3.2$ echo ${t##*/}
path
William Pursell