Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get sub string in shell script

Tags:

string

shell

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.

like image 244
derrdji Avatar asked Nov 29 '22 05:11

derrdji


1 Answers

$ 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
like image 80
Stefano Borini Avatar answered Dec 29 '22 04:12

Stefano Borini