Get file name from full path in Unix

I'm trying to get the name of a file from the full path of a parameter for a function in my bashrc. I don't know any regular expressions, so I'm kind of lost. I know I want to get the last forward slash and use everything after that, but I'm not quite sure how to go about doing that. Then I want to store it in a variable in my function, which I'll use later.

Any help is appreciated!

Answers


The fastest way is to use variable substitution like

f1=/my/big/long/path/to/a/little/bitty/file
f2=./my/big/long/path/to/a/little/bitty/file
f3=my/big/long/path/to/a/little/bitty/file
f4=file

echo ${f1##*/}
echo ${f2##*/}
echo ${f3##*/}
echo ${f4##*/}

# This is how you get value out
filename=${f1##*/}

OUTPUT;

file
file
file
file

You can use basename but that will be slow as it will launch a new child process.


Need Your Help

Access the 'value' in JS Object using D3.JS

javascript object d3.js

I'm working with D3.js and trying to sort my bars in ascending/descending order when a button is clicked.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.