Find the current folder name in Bash
Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated.
This article will introduce three methods to find the folder name from this article directory. In addition, we will see the necessary examples and explanations to make the topic easier to understand.
Find the current folder name in Bash
To do this, we will use a special keyword: PWD. This built-in keyword in Bash is used to find the current working directory.
In the following example, we will find the name of the folder we are currently in. The code for our example is shown below.
current_dir=${PWD##*/}
echo "$current_dir"
The above is the simplest way to accomplish the task. But our next example is a little more complicated.
We will use the example below to find the folder name.
Current_Dir=${PWD##*/}
Current_Dir=${Current_Dir:-/}
printf '%s\n' "${PWD##*/}"
printf '%q\n' "${PWD##*/}"
In the example above, we Current_Dir=${PWD##*/}
assign a variable using the line, and with Current_Dir=${Current_Dir:-/}
the line we correct PWD=/
the case of .
After that, you can use any of the next two lines. Both lines will be printed to standard output.
But the first line is more robust than echo for some unusual names, where the second line is quoted for use as shell input, and it's also useful for making hidden characters readable. After running the above script, you'll only get the folder names of the current directory.
We can also perform a similar task with the following example. Here, we put the entire directory in a variable.
The code for our next example is shown below.
DirectoryStr=/path/to/somewhere//
shopt -s extglob
CurrentDir=${DirectoryStr%%+(/)}
CurrentDir=${CurrentDir##*/}
CurrentDir=${CurrentDir:-/}
printf '%s\n' "$CurrentDir"
Let's explain this example piece by piece.
- In the first line, we create a variable and assign the directory path to it as a string.
- Then, with the shopt -s extglob line, we enabled the +(...) glob syntax.
-
With
CurrentDir=${DirectoryStr%%+(/)}
this line, we trim the string regardless of how many trailing slashes are present in the string. -
With
CurrentDir=${CurrentDir##*/}
this line, we delete everything before the last /. -
We have just
CurrentDir=${CurrentDir:-/}
corrected the directory case with the line - Finally, we just printed the directory folder.
All the codes used in this article are written in Bash. It will only work in Linux Shell environment.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Run a batch (.bat) file in CMD
Publish Date:2025/04/21 Views:169 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways in which you can run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way yo
Running batch scripts using Task Scheduler
Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM
-
This article will show you how to use Task Scheduler to run a batch file. Running batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't
Solve the error Make Command Not Found in Cygwin
Publish Date:2025/04/21 Views:75 Category:OPERATING SYSTEM
-
Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err
Difference between Bash Nohup and &
Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM
-
This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux processes in the backgr
Bash Nohup 与 & 的区别
Publish Date:2025/04/21 Views:186 Category:OPERATING SYSTEM
-
这篇简短的文章介绍了通过 Bash 在后台运行 Linux 进程的 nohup 命令和 控制运算符。 此外,我们将进一步研究 nohup 和 之间的主要区别。 在后台运行 Linux 进程 Linux 提供了两种在后台运行
Getting Timestamp in Bash
Publish Date:2025/04/21 Views:147 Category:OPERATING SYSTEM
-
This article discusses the date Bash command used to obtain the system date/time and UNIX timestamp. Get Timestamp Using date Command in Bash The Linux terminal uses the date command to print the current date and time. The simplest version
Pretty Printing JSON in Shell Script
Publish Date:2025/04/21 Views:145 Category:OPERATING SYSTEM
-
JSON is a textual method for representing JavaScript object literals and arrays and scalar data. It is relatively easier to read and write, and easier for manageable software to parse and generate. JSON is commonly used to serialize structu
Batch checks whether the specified environment variable contains a substring
Publish Date:2025/04/21 Views:72 Category:OPERATING SYSTEM
-
This article discusses how to use the Batch command to test whether an environment variable contains a specific substring. We will introduce two batch scripts that can be used in the above scenario. Checks whether the specified environment
Pull all branches in Git
Publish Date:2025/04/21 Views:98 Category:OPERATING SYSTEM
-
Git provides us a platform where we can maintain multiple separate development commits for a new project called branches. We can restore the latest version of a branch from a remote repository as needed or we can restore all branches at onc