- 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
- How to Change Current Directory Using cd, pwd in Bash Script?
- How to Change the Current Directory Using cd, pwd in Bash Script?
- Use of “source” Command
- Alternative: Use “dot”
- Display pwd
- Alternative 1: Change Current Directory Using Function
- Alternative 2: Change Current Directory Using alias
- Conclusion
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.
How to Change Current Directory Using cd, pwd in Bash Script?
In Linux, all the files are organized in a tree-like directory structure starting from the root directory. In order to navigate into the desired directory Linux offers the “cd” command. It changes the present working directory to the targeted one. On the other hand, the built-in “pwd” command is quite useful when the users want to get the path of the current directory where they are working.
This guide explains both the “cd” and “pwd” commands through the terminal or the bash script:
- Change the Current Directory Using cd, pwd
- Change Current Directory Using Function
- Change Current Directory Using alias
How to Change the Current Directory Using cd, pwd in Bash Script?
The “cd” command provides a way to change the current working directory in the present shell through the terminal or the bash script. Let’s see how this task can be performed:
Step 1: Create a Script
Create/open a bash script in the “nano” text editor having the following content:
#!/bin/bash cd /home/itslinuxfoss/Downloads
- The “#!/bin/bash” denotes the “Bash Shebang” which executes the “script.sh” in the bash shell.
- The “cd” command follows the absolute path of the “Downloads” directory in which “itslinuxfoss” shows the user name of this directory:
Press “Ctrl+S” to save and “Ctrl+X” to exit the text editor
Step 2: Make the Script Executable
The newly created bags script does not have the execute permissions by default. To do so, execute the “chmod” command followed by the “x(execute)” flag to make the “script.sh” executable:
The “script.sh” is now executable.
Step 3: Execute the Script
Lastly, specify the name of the “script.sh” with “dot().” and “forward-slash(/)”to run it:
The “script.sh” is executed in the subshell and does not change the directory in the terminal.
Use of “source” Command
The above output is an exceptional behaviour and can be handled through the “source” command. It is the built-in shell command that is used to read and execute the file/script content in the current shell environment:
The current working directory has been changed to “Downloads” instantly.
Alternative: Use “dot”
The user can also mention the “dot(.)” symbol with the “script.sh” instead of the “source” command for changing the current active directory:
The output of the “dot(.)” symbol is the same as the “source” command.
Display pwd
In order to show the “present working directory” in the terminal after changing the directory in the script use the “pwd” command in the “script.sh”:
#!/bin/bash cd /home/itslinuxfoss/Downloads pwd
Save the new changes and exit the editor.
Run the script.sh with the help of “dot(.)” symbol:
At this time the “script” shows the pwd as “.home/itslinuxfoss/Downloads” and then changes the directory in the current shell environment.
Alternative 1: Change Current Directory Using Function
The “cd” command can also be implemented in a function defined in the terminal apart from the bash script. Let’s see how it can be performed practically.
Step 1: Create a Function
First create a function named “chng-dir” having no argument in the parenthesis. It defines a single “cd” command following the absolute path of the “Pictures” directory:
The “chng-dir” function has been created.
Step 2: Invoke Function
Call the newly created “chng-dir” function in the terminal and see the output:
The “chng-dir” function has successfully executed and changed the present working directory as “Pictures”
Alternative 2: Change Current Directory Using alias
Another shortest method to use the “cd” command is creating an “alias”. The alias acts as a shortcut to perform the defined task to avoid typing the long commands. It can be created temporarily/ permanently.
Step 1: Create an alias
Type the “alias” keyword to declare the “chng” alias having the “cd” command along with the complete path of desired directory i.e “Pictures”:
$ alias chng='cd /home/itslinuxfoss/Pictures'
The “chng” alias has been declared.
Step 2: Activate the alias
Specify the “alias” name “chng” as a command and it will show you the same output as the “cd” command:
The present working directory has now changed to “Pictures”.
Conclusion
To change the dictionary in the current bash shell, execute the script having “cd” and “pwd” commands with the “dot(.)” symbol and the “source” command. Both perform the same task and change the present working directory via “cd” defined in the script and show its absolute path via “pwd” command line utility.
This guide has described how to change the current directory using “cd”,”pwd” in the bash script.