- Get current directory or folder name (without the full path)
- 24 Answers 24
- Bash Get Current Directory
- How do I get the current directory in Linux?
- How do I get the current working directory in terminal?
- How do I get the current directory name?
- Which command will allow you to change your current directory?
- What is the directory in Linux?
- How do I cd into a directory?
- Which command is used for printing the current working directory?
- How do I change my directory?
- How do I get the current directory in DOS?
- How do I find the current directory in Windows?
- How do I change directories in command prompt?
- How do I change directory in bash?
- Which command is used to remove directory?
- How do I list all directories in Linux?
- 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)
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
Bash Get Current Directory
Print Current Working Directory ( pwd ) To print the name of the current working directory, use the command pwd . As this is the first command that you have executed in Bash in this session, the result of the pwd is the full path to your home directory.
- How do I get the current directory in Linux?
- How do I get the current working directory in terminal?
- How do I get the current directory name?
- Which command will allow you to change your current directory?
- What is the directory in Linux?
- How do I cd into a directory?
- Which command is used for printing the current working directory?
- How do I change my directory?
- How do I get the current directory in DOS?
- How do I find the current directory in Windows?
- How do I change directories in command prompt?
- How do I change directory in bash?
- Which command is used to remove directory?
- How do I list all directories in Linux?
How do I get the current directory in Linux?
To determine the exact location of the current directory at a shell prompt and type the command pwd. This example shows that you are in the user sam’s directory, which is in the /home/ directory. The command pwd stands for print working directory.
How do I get the current working directory in terminal?
To get the current working directory use the pwd command.
How do I get the current directory name?
Use pwd and basename command to get current folder name
The pwd display name of current or working directory. The current working directory as set by the cd command stored in $PWD shell variable.
Which command will allow you to change your current directory?
The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.
What is the directory in Linux?
A directory is a file the solo job of which is to store the file names and the related information. All the files, whether ordinary, special, or directory, are contained in directories. Unix uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree.
How do I cd into a directory?
The second way to list files in a directory, is to first move into the directory using the «cd» command (which stands for «change directory», then simply use the «ls» command. I’ll type «cd Downloads/Examples» to change directories into the «Examples» directory that is inside the «Downloads» directory.
Which command is used for printing the current working directory?
The pwd command stands for print working directory. It is one of the most basic and frequently used commands in Linux. When invoked the command prints the complete path of the current working directory.
How do I change my directory?
To access another drive, type the drive’s letter, followed by «:». For instance, if you wanted to change the drive from «C:» to «D:», you should type «d:» and then press Enter on your keyboard. To change the drive and the directory at the same time, use the cd command, followed by the «/d» switch.
How do I get the current directory in DOS?
MS-DOS and Windows command line current directory
To list the files in the current directory use the dir command, and if you want to change the current directory, use the cd command. You can use the chdir command by itself to print the current directory in MS-DOS and the Windows command line.
How do I find the current directory in Windows?
- find /dir/to/start/from -type f -ls This format the date to numeric find /dir/to/start/from -type f -exec ls -l —time-style=»+ %Y %m %e %H:%M» \; – user1378779 May 7 ’12 at 0:54.
- have a look at this answer stackoverflow.com/a/52301748/2704032 – Vishrant Mar 19 ’20 at 17:33.
How do I change directories in command prompt?
- To a Directory of Current Drive : To change the working directory, execute command cd followed by an absolute or relative path of the directory you are wanting to become the CWD.
- To a Directory of Another Drive : To change the working directory to another drive, execute command cd /D followed by a path to a directory.
How do I change directory in bash?
To change directories, use the command cd followed by the name of the directory (e.g. cd downloads ). Then, you can print your current working directory again to check the new path.
Which command is used to remove directory?
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
How do I list all directories in Linux?
- To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) .
- To display detailed information, type the following: ls -l chap1 .profile. .
- To display detailed information about a directory, type the following: ls -d -l .
Docker
This guide shows you how to list, stop, and start Docker containers.A Linux-based operating system. . As you can see, the image above indicates ther.
Flash
How do I enable Adobe Flash Player in chromium?How do I install Adobe Flash Player on chromium Ubuntu?How do I fix Adobe Flash player on Chrome?How do.
User
How do you add a user to the Sudoers file in Debian?How do I add user to Sudoers file?How do I get sudo privileges in Debian?Where is the Sudoers file.
Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system
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.