- Get current directory or folder name (without the full path)
- 24 Answers 24
- How can I get the current working directory? [duplicate]
- 5 Answers 5
- How to show the full path of a file or directory in the terminal?
- 3 Answers 3
- How to Get the current directory in Linux
- Get current directory with pwd command in Linux
- Directory Structure in Linux
- Understanding directories . (dot) and .. (dot dot)
- List files under the directory using ls command in Linux
- change directory with cd command in Linux
- Conclusion
Get current directory or folder name (without the full path)
How could I retrieve the current working directory/folder name in a bash script, or even better, just a terminal command. pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin .
24 Answers 24
No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:
result=$ # to assign to a variable result=$ # to correct for the case where PWD=/ printf '%s\n' "$" # to print to stdout # . more robust than echo for unusual names # (consider a directory named -e or -n) printf '%q\n' "$" # to print to stdout, quoted for use as shell input # . useful to make hidden characters readable.
Note that if you’re applying this technique in other circumstances (not PWD , but some other variable holding a directory name), you might need to trim any trailing slashes. The below uses bash’s extglob support to work even with multiple trailing slashes:
dirname=/path/to/somewhere// shopt -s extglob # enable +(. ) glob syntax result=$ # trim however many trailing slashes exist result=$ # remove everything before the last / that still remains result=$ # correct for dirname=/ case printf '%s\n' "$result"
Alternatively, without extglob :
dirname="/path/to/somewhere//" result="$">" # extglob-free multi-trailing-/ trim result="$" # remove everything before the last / result=$ # correct for dirname=/ case
How can I get the current working directory? [duplicate]
I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd . It just prints pwd how do I get the current working directory into a variable?
This is not a duplicate of the question for which it is currently marked as one. The two questions should be compared, at least, based on their titles (as well as their answers). That the answer to this question is already covered by another is, or should be, irrelevant.
@KennyEvitt actually, one of the main reasons we close is precisely because an answer has been given elsewhere. And, in fact, the main question here is actually how to assign the output of a command to a variable, which is covered by the dupe. I have also given the answer to this specific case, so all bases are covered. There would be no benefit in opening this again.
@terdon As a resource available, and intended, for the entire population of Unix & Linux users, this is a valuable question, even if the original asker really just needed an answer already covered elsewhere. If anything, I think this question should be edited to more closely match its title and it should be re-opened, not to allow further activity, but to not imply that this question is ‘bad’.
@KennyEvitt closing as a duplicate in no way implies that the question is bad! This question will remain here, answered, for ever. If you really want to know how to get the current working directory, you will find your answer here. If you just want to know how to save the output of a command in a variable, you will also find the answer here by following the link to the dupe. In any case, this isn’t really something I should do alone, if you feel strongly that it should be reopened, please open a discussion on Unix & Linux Meta where such things should be resolved.
5 Answers 5
There’s no need to do that, it’s already in a variable:
The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:
Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.
For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):
Of the two, the $() is preferred since it is easier to build complex commands like:
command0 "$(command1 "$(command2 "$(command3)")")"
Whose backtick equivalent would look like:
command0 "`command1 \"\`command2 \\\"\\\`command3\\\`\\\"\`\"`"
How to show the full path of a file or directory in the terminal?
I need to know how the directory name in order to type it out in the terminal. How do I access the names of directories? Windows Explorer used to have a title bar with the full path. Can someone please help me figure out how to see the full path of a certain file?
If you know the path and need to type it out quickly, nothing is better than Tab completion, especially with zsh .
I figured out a way to delete files with BleachBit, thanks for your answer. But for the other query I still need to know how to see the FULL path of any certain file or folder.
3 Answers 3
If you are using nautilus to browse your files, you can toggle the navigation bar by pressing Ctrl + L .
If you are using the terminal, just use pwd to know the absolute path of your current location.
And don’t forget that space characters need to be escaped within the terminal. If you want to access /path/to/the force then you need to do cd /path/to/the\ force .
To display the full path of a file in the terminal just drag the file’s icon into the terminal, and the full path of the file will be displayed enclosed by two apostrophes (single quotation mark characters). It’s that simple.
In Ubuntu 20.04 and later drag and drop of files or directories doesn’t work from the desktop, but does work in other locations including dragging from the desktop in Files file manager.
find can do this quite handily from the terminal. Here’s an example in which I’m looking for the full path of the file Taxes-2013.pdf:
sudo find / -name Taxes-2013.pdf
/home/me/Documents/Taxes-2013.pdf
I’m using sudo so that I can avoid all the permission denied output that I would otherwise get with find when searching from the root of the tree.
If you just want the pathname and want the filename stripped off you can use
sudo find / -name Taxes-2013.pdf | xargs -n1 dirname
Note: If you are in the habit of putting spaces in names this is relevant to you.
How to Get the current directory in Linux
In this blog post, we will be discussing how to check the current directory in Linux. There are a few different ways to do this, but we will be focusing on terminal commands. Knowing how to navigate your file system is an essential skill for any Linux user, so please follow along as we walk you through it!
Get current directory with pwd command in Linux
To get the current directory in Linux, we can use pwd command. This command stands for “print working directory”. It will print out the full path of the current directory you are in. For example, if we are currently in the /home/user/directory, it will print out that exact path.
example:
$ pwd
/home/user/directory
The pwd command is fairly straightforward – it simply prints out the current working directory. However, there is one thing you should know about it.
The pwd command always prints out the full path of the current directory. This means that it will print out the complete path, starting from the root of the file system.
Directory Structure in Linux
There are a few important directories that every Linux user should be familiar with.
The / directory is the root of the Linux file system. This is the top directory, and everything else is contained within it. The / directory can be thought of as the “backbone” of your file system, and all other directories are its children.
The /etc directory is one of the most important directories on a Linux system. This is where all of the system’s configuration files are stored. If you ever need to change a setting or configure your system, you will likely find the file you need in /etc.
The /var directory is another important directory in Linux. This holds variable data such as logfiles and spools. If your system ever runs out of space, you can often free up some extra room by deleting files from /var.
The /home directory, for example, is where your personal files and folders are stored.
Some other important directories that are nested within the / directory include:
- /bin – This contains essential command-line programs that are needed for your system to run.
- /boot – This stores files necessary for booting your system.
- /dev – This contains device files that represent physical devices on your system.
- /etc – This contains configuration files for your system.
- /home – This contains user home directories.
- /lib – This stores libraries needed by programs on your system.
- /mnt – This is used to mount (attach) filesystems to the file hierarchy.
- /opt – This contains optional software packages that may be installed on your system.
- /proc – This contains information about the running processes on your system.
Understanding these essential Linux directories is a key part of becoming a successful Linux user. With a little practice, you’ll be navigating your file system like a pro!
Understanding directories . (dot) and .. (dot dot)
The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the directory one level above the current working directory, often referred to as the parent directory.
List files under the directory using ls command in Linux
To list the files under the current directory in Linux, we can use ls command. This stands for “list” and will list all of the files and folders in the current directory.
If we run the ls command with no arguments, it will simply print out a list of all of the files in the current directory.
However, we can also use the -l argument to get a more detailed listing. This will print out additional information such as the file size, owner, and file permissions.
change directory with cd command in Linux
In order to change directories in Linux, you can use the cd command. For example, if you want to change to the /home/user/directory, you would simply run the cd /home/user/directory command.
If you want to go one level up in the directory tree, you can use the .. (dot dot) directory. For example, if you are in the /home/user/directory and you want to go to the parent directory, you would run the cd .. command.
If you want to go to user’s home directory in Linux, you can use cd ~username.
Conclusion
In this blog post, we have discussed how to check the current directory in Linux and how to change directories using terminal commands. We have also introduced some of the most important directories in Linux, and provided examples of how to navigate to them.
By following these steps, you should be able to easily navigate your way around the file system. That’s all there is to it! With these skills, you should be able to get around your system with ease. As always, if you have any questions or need help, feel free to reach out to us.
David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.
howtouselinux.com is dedicated to providing comprehensive information on using Linux.
We hope you find our site helpful and informative.