If you type pwd you get something like:
/home/username/Desctop/myfolder/
How to take the last part? The myfolder
path.
This must be simple but I couldn't find easy solution in shell. I know how to take care of this in java but not in shell.
thanks
You can use
basename
for that, provided the last part is indeed a directory component (not a file):Using
basename $(pwd)
are two useless and expensive forks.should do the trick completely in the shell without expensive forks (snag: for the root directory this is the empty string).
To extract the last part of a path, try using
basename
...You're right--it's a quick command:
In Linux, there are a pair of commands,
dirname
andbasename
.dirname
extracts all but the last part of a path, andbasename
extracts just the last part of a path.In this case, using
basename
will do what you want:basename $(pwd)