How to Find the Current Folder Name in Bash

MD Aminul Islam Feb 02, 2024
How to Find the Current Folder Name in Bash

Finding a directory is very easy through the Bash script. But finding the exact directory folder name you are in right now is a bit complex.

This article will introduce three ways to find the folder name from the directory in this article. Also, we will see necessary examples and explanations to make the topic easier.

Find the Current Folder Name in Bash

We will use a special keyword for this purpose: PWD. This built-in keyword in Bash is used to find the current working directory.

In our example below, we will find the folder name where we are right now. The code for our example will look like the below.

current_dir=${PWD##*/}
echo "$current_dir"

Above is the easiest way to do the task. But our next example is a bit complex.

We will find the folder name by using the example below.

Current_Dir=${PWD##*/}
Current_Dir=${Current_Dir:-/}
printf '%s\n' "${PWD##*/}"
printf '%q\n' "${PWD##*/}"

In the example above, we assign a variable with the line Current_Dir=${PWD##*/} and through the line Current_Dir=${Current_Dir:-/} we corrected the case where the PWD=/.

After that, you can use any one of the lines from the next two lines. Both lines will print to stdout.

But the first line is more robust than the echo for some unusual name where the second is quoted for using as a shell input, and it is also useful for making hidden characters readable. After running the script above, you will get only the current directory folder name.

We also can perform a similar task through our below example. Here we took the whole directory in a variable.

The code for our next example is something like the below.

DirectoryStr=/path/to/somewhere//
shopt -s extglob
CurrentDir=${DirectoryStr%%+(/)}
CurrentDir=${CurrentDir##*/}
CurrentDir=${CurrentDir:-/}
printf '%s\n' "$CurrentDir"

Let’s explain this example part by part.

  1. On the first line, we create a variable and assign it with the path to the directory as a string.
  2. Then, through the line shopt -s extglob, we enabled the +(...) glob syntax.
  3. Through the line CurrentDir=${DirectoryStr%%+(/)}, we trimmed the string no matter how many trailing slashes exist on the string.
  4. Through the line CurrentDir=${CurrentDir##*/}, we removed everything before the last /.
  5. We just corrected the directory case through the line CurrentDir=${CurrentDir:-/}.
  6. Lastly, we just printed the directory folder.

All the codes used in this article are written in Bash. It will only work in the Linux Shell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash Directory