- 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
- Bash Get Current Directory
- Requirements
- PWD (Print working directory)
- CD (Change current working directory)
- Display or list all directories
- Conclusion
- About the author
- Aqsa Yasin
- Get current directory or folder name (without the full path)
- 24 Answers 24
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.
Bash Get Current Directory
In Linux, all tasks done through the command line require users to access adequate directories. There are different types of directories in a computer system with Linux or Ubuntu OS. Users can access each directory through the terminal, and interact with them. There are multiple options, and each time users interact with the command prompt of the current directory they are working.
The Linux system responds by providing information against each input request. The achieved output is standard and printed to the shell prompt. In this tutorial, we will dig deep into the ways of accessing the current working directory and how users can switch from one directory or location to another, followed by relevant examples. The command used for accessing the current working directory will help them access any location in their system anytime, as per their requirements.
Requirements
Following system requirements are mandatory to run the commands in the bash to get directory:
Recommended OS: Linux Mint 20 or Ubuntu 20.04
User account: A user account with sudo rights
The tutorial assumes that users already have the latest Linux Mint OS on their computer systems. For bash, get the current directory in Linux Mint 20, open up the Terminal from the main menu on the bottom left of your screen, and then select the Terminal option.
To interact with the terminal, type bash and then press enter.
It will display a prompt, which shows that Bash is waiting for the value of the input.
Note: It all depends on the user’s computer system that they might get a different prompted character (The current location in the file structure of the computer system including the working directory that is currently running on the system). While entering the commands, don’t type $ or any other character before the command. Also, notice that in the examples mentioned in this tutorial, the lines that have a prompt in them, and don’t begin with $ character, are the outputs of each command.
PWD (Print working directory)
The current working directory is the directory where all of the commands are being executed. You need to get the name of the current working directory printed. Type PWD command and then click enter. It will show the complete directory in the output, as shown below:
The above output shows that we are currently in the user’s directory, i.e., /home/aqsa. The command used here is PWD, a print working directory, and once typed, the Linux Mint 20 system is requested to display the current location. The default directory is the home directory that will appear when the users start a new Bash session.
Note: To exit the directory by one level, type cd .. and then click enter. You will be returned in one directory.
Whereas, if you want to exit all directories, simply type cd, then click enter. You will reach the default directory.
CD (Change current working directory)
Sometimes users want to switch from one directory to another to access the relevant locations and files in another directory. For this, they need to use the CD command, then followed by a location or a directory, e.g., Documents, Home, etc.
Simply type the CD directory name and then click enter. You can print your directory to check this new path. The working directory can be changed to the existing one, and the current working directory will be updated, as shown in the example below. Here, we have reached the home directory.
You can also move further in any directory by typing the CD Directory Name and then hit enter. This will further take you to the location, which is looking-for. Users can try entering the whole path as well in one go, e.g., cd /home/documents/test.docx; this will save them from trying multiple steps and will help them in reaching the location in one go.
Note: You can also see the list of all files present in the location in which you are currently present. It can be completed by simply typing ls, then, you can press enter to see the output.
Display or list all directories
Knowing the list of all directories is one important thing while working on Linux systems. The users can check out different options based on the directories they are currently working in and would want to switch between them, so they can make use of these locations.
To display all directories from a particular location, try the command as below:
Here, in the example below, the user is in its home directory, so it will display the relevant directory, which is named as “aqsa listed” and “currently in use”.
Note: You can also use a combination of ls and grep commands that will list down the directory names. For this, users can use the find command. Following are a few commands that can also be used in place of the command mentioned above:
Conclusion
In this tutorial, we explored different options to get the current directory using Bash in Linux Mint 20. In this way, users can access the current directory in Linux or Ubuntu based on the system they are using. The various command-line options are discussed to let users know how to get the current directory they are working in. The current working directory is the directory from which users invoke different kinds of commands from their terminal or console line. They can access different locations by simply typing these easy commands in one go and then perform relevant actions in the locations they tend to work in.
About the author
Aqsa Yasin
I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.
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