Linux commands working directory

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.

Читайте также:  Linux return code process

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.

Читайте также:  I915 intel driver linux

Источник

15 ‘pwd’ (Print Working Directory) Command Examples in Linux

For those working with Linux command Line, command ‘pwd‘ is very helpful, which tells where you are – in which directory, starting from the root (/). Specially for Linux newbies, who may get lost amidst of directories in command Line Interface while navigation, command ‘pwd‘ comes to rescue.

Linux pwd Command Examples

What is pwd?

pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.

Basic syntax of pwd:
Options used with pwd
Options Description
-L (logical) Use PWD from environment, even if it contains symbolic links
-P (physical) Avoid all symbolic links
–help Display this help and exit
–version Output version information and exit

If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will avoid all symlinks, i.e., take option ‘-P‘ into account.

Exit status of command pwd:

0 Success
Non-zero Failure

This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.

1. Print your current working directory.

2. Create a symbolic link of a folder (say /var/www/html into your home directory as htm). Move to the newly created directory and print working directory with symbolic links and without symbolic links.

Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.

[email protected]:~$ ln -s /var/www/html/ htm [email protected]:~$ cd htm

3. Print working directory from environment even if it contains symlinks.

4. Print actual physical current working directory by resolving all symbolic links.

5. Check if the output of command “pwd” and “pwd -P” are same or not i.e., if no options are given at run-time does “pwd” takes option -P into account or not, automatically.

Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified with command “pwd”, it automatically takes option “-P” into account.

6. Print version of your ‘pwd’ command.

[email protected]:~$ /bin/pwd --version pwd (GNU coreutils) 8.23 Copyright (C) 2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Jim Meyering.

Note: A ‘pwd’ command is often used without options and never used with arguments.

Читайте также:  Rufus создать загрузочную флешку linux

Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.

So what’s the difference? Well “pwd” alone means shell built-in pwd. Your shell may have different version of pwd. Please refer manual. When we are using /bin/pwd, we are calling the binary version of that command. Both the shell and the binary version of command Prints Current Working Directory, though the binary version have more options.

7. Print all the locations containing executable named pwd.

[email protected]:~$ type -a pwd pwd is a shell builtin pwd is /bin/pwd

8. Store the value of “pwd” command in variable (say a), and print its value from the variable (important for shell scripting perspective).

[email protected]:~$ a=$(pwd) [email protected]:~$ echo "Current working directory is : $a" Current working directory is : /home/avi

Alternatively, we can use printf, in the above example.

9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a command (say ‘ls‘) to verify is everything is OK.

[email protected]:~$ cd /home [email protected]:~$ PS1='$pwd> ' [Notice single quotes in the example] > ls

10. Set multi-line command line prompt (say something like below).

And then execute a command (say ls) to check is everything is OK.

[email protected]:~$ PS1=' > $PWD $ 123#Hello#! $ ' /home 123#Hello#!

11. Check the current working directory and previous working directory in one GO!

[email protected]:~$ echo “$PWD $OLDPWD” /home /home/avi

12. What is the absolute path (starting from /) of the pwd binary file.

13. What is the absolute path (starting from /) of the pwd source file.

14. Print the absolute path (starting from /) of the pwd manual pages file.

15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under directory tecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a directory tecmint under your home directory and ask you to cd to it.

Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.

[email protected]:~$ mkdir tecmint [email protected]:~$ cd tecmint [email protected]:~$ nano pwd.sh

Next, add the following script to the pwd.sh file.

#!/bin/bash x="$(pwd)" if [ "$x" == "/home/$USER/tecmint" ] then < echo "Well you are in tecmint directory" echo "Good Bye" >else < mkdir /home/$USER/tecmint echo "Created Directory tecmint you may now cd to it" >fi

Give execute permission and run it.

[email protected]:~$ chmod 755 pwd.sh [email protected]:~$ ./pwd.sh Well you are in tecmint directory Good Bye

Conclusion

pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal. That’s all for now. I’ll be here again with another interesting article soon, till then stay tuned and connected to Tecmint.

Источник

Оцените статью
Adblock
detector