I have a variable which has the directory path, along with the file name. I want to extract the filename alone from the Unix directory path and store it in a variable.
fspec="/exp/home1/abc.txt"
I have a variable which has the directory path, along with the file name. I want to extract the filename alone from the Unix directory path and store it in a variable.
fspec="/exp/home1/abc.txt"
Use the basename command to extract the filename from the path:
Using bash "here string":
The benefit of the "here string" is that it avoids the need/overhead of running an
echo
command. In other words, the "here string" is internal to the shell. That is:as opposed to:
bash:
You can simply do:
bash to get file name
other ways
awk
sed
using IFS
dirname "/usr/home/theconjuring/music/song.mp3"
will yield/usr/home/theconjuring/music
.