Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take the last part of the folder path in shell

Tags:

linux

bash

shell

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

like image 402
Lukap Avatar asked May 18 '12 14:05

Lukap


People also ask

How do I get the shell directory path?

2.1. To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.

How do I get the last directory name in Unix?

How do I get directory name from its path on a Linux or Unix-like system? [/donotprint]In other words, you can extract the directory name using dirname command.

How do I go back to the last directory?

To navigate into the root directory, use "cd /" To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

How do I find the path of a file in bash?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.


1 Answers

You're right--it's a quick command:

basename "$PWD" 
like image 92
andyras Avatar answered Sep 19 '22 10:09

andyras