- Get current directory or folder name (without the full path)
- 24 Answers 24
- Find a directory in Linux
- Find a directory via command line
- Find a directory via GUI
- Conclusion
- Related Linux Tutorials:
- NEWSLETTER
- WRITE FOR US
- TAGS
- FEATURED TUTORIALS
- LATEST TUTORIALS
- Linux command which directory
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- How to Use the which Command in Linux
- Linux which Command Syntax and Options
- Linux which Command Examples
- 1. Display the Path of Any Executable File
- 2. Display Multiple Paths of Executable Files
- 3. List All Instances
- 4. Find Symbolic Links
- 5. Exclude Shell Built-ins
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
Find a directory in Linux
If you need to find a certain directory on your Linux system, we’ve got just the guide for you. In this tutorial, we’ll be going through the step by step instructions to locate a folder on Linux via both the command line and GUI.
In this tutorial you will learn:
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | N/A |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user |
Find a directory via command line
Finding a directory or folder with the command line should work identically across any Linux distribution of your choice. All you need to do is open a terminal on your system and use the following find command syntax to see the location of a specified directory:
$ find /path/to/search -type d -name "name-of-directory"
Using that syntax, here’s how to search for a directory named “test” inside the home directory.
$ find $HOME -type d -name "test"
The -d option is what causes find to display directories (or folders) only. The -f option can be used for files, or the options can be omitted entirely to find both directories and files with the specified name.
Use wildcards in your search if you’re only specifying part of the directory name, like so:
$ find $HOME -type d -name "test*"
Use the iname option in place of name if you’d like to perform a case insensitive search. Example:
$ find $HOME -type d -iname "test"
The find command is probably going to be your best bet for finding a directory, but the locate command deserves a brief mention as well. The find command searches the file system in real time, whereas locate has a database of the file/directory names and locations on a system.
This allows locate to be a lot faster, but possibly less accurate as the database isn’t constantly refreshed. The find command is also more flexible, offers more options, and is certain to be installed by default.
Find a directory with locate command:
Find a directory via GUI
To find a directory/folder with GUI, you can use your system’s file browser. We’ll show you what that looks like on our test system running the GNOME desktop environment, but your system may look different, depending on what desktop environment you’re running.
The instructions should be similar, as it can be assumed that any file browser you have will certainly offer a search function.
- Start by opening your file browser.
Conclusion
Finding directories on Linux is simple. Between the find and locate commands as well as the built in file browser, there are a variety of good methods to quickly find a directory or folder in Linux.
Related Linux Tutorials:
Comments and Discussions
NEWSLETTER
Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials.
WRITE FOR US
LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.
When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.
TAGS
FEATURED TUTORIALS
- VIM tutorial for beginners
- How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux
- Bash Scripting Tutorial for Beginners
- How to check CentOS version
- How to find my IP address on Ubuntu 20.04 Focal Fossa Linux
- Ubuntu 20.04 Remote Desktop Access from Windows 10
- Howto mount USB drive in Linux
- How to install missing ifconfig command on Debian Linux
- AMD Radeon Ubuntu 20.04 Driver Installation
- Ubuntu Static IP configuration
- How to use bash array in a shell script
- Linux IP forwarding – How to Disable/Enable
- How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux
- How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux
- Netplan static IP on Ubuntu configuration
- How to change from default to alternative Python version on Debian Linux
- Set Kali root password and enable root login
- How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux
- How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
- How to check NVIDIA driver version on your Linux system
- Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux
LATEST TUTORIALS
- Enabling SSH on Raspberry Pi: A Comprehensive Guide
- Boot Your Raspberry Pi from a USB: A Tutorial
- Checking Your Raspberry Pi’s OS Version
- Install an OS on Your Raspberry Pi: Step-by-Step
- Finding Your Raspberry Pi’s IP Address: A Quick Guide
- Easy Steps to Update Your Raspberry Pi
- Connecting Your Raspberry Pi to Wi-Fi: A How-To
- How to install RealVNC viewer on Linux
- How to check Raspberry Pi RAM size and usage
- How to check Raspberry Pi model
- Understanding UEFI and BIOS in Relation to Linux Nvidia Driver Installation
- How to orchestrate Borg backups with Borgmatic
- How to monitor filesystem events on files and directories on Linux
- Debian USB Firmware Loader Script
- How to install and self host an Ntfy server on Linux
- How to backup your git repositories with gickup
- How to bind an SSH public key to a specific command
- Creating a Bootable USB for Windows 10 and 11 on Linux
- How to list all displays on Linux
- List of QR code generators on Linux
Linux command which directory
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
How to Use the which Command in Linux
The which command allows users to search the list of paths in the $PATH environment variable and outputs the full path of the command specified as an argument. The command works by locating the executable file matching the given command.
In this tutorial, you will learn to use the which command.
Linux which Command Syntax and Options
The syntax for the which command is:
The [argument] variable specifies the command or commands you want to find.
For example, the following command outputs the location of the cat command:
The which command has only one option, -a . It is optional and used to print all the matches it finds.
The command searches for matches from left to right. If there are multiple matches found in the directories listed in $PATH , which prints only the first one. The -a option instructs which to print all the matches.
Important: On many Linux distributions, which excludes the shell built-in commands and does not output their location.
Having multiple matches sometimes means one match is a symlink to the other. However, it is possible to have two versions of the same command in different locations or two different commands using the same name.
Note: Unlike many other commands, which has no —help option. To see the command description and help, run man which .
Exit Status
The which command returns one of the following values that indicate its exit status:
- 0 . All arguments were found and executable.
- 1 . One or more arguments don’t exist or aren’t executable.
- 2 . An invalid option has been specified.
Linux which Command Examples
The following examples showcase how the which command works and how to use the available option.
1. Display the Path of Any Executable File
To display the path of any command, pass the command name as an argument after which .
The output shows the path to the tr command executable file, located in /usr/bin/tr.
2. Display Multiple Paths of Executable Files
which accepts multiple arguments and outputs the path to each one in the specified order.
The command works through the supplied list and outputs the results for the nc command, mount command, and sort command, separating each result with a newline character.
3. List All Instances
which only shows the first match it finds in the $PATH variable directory list. Use the -a option to show every match for the specified command.
For example, searching for instances of the less command outputs two results when using the -a option:
Use the ls command to check file details and determine if both versions are executable files. Run:
ls -lh /usr/bin/less ls -lh /bin/less
The output shows two identical versions of the same command in two locations, both 176 KB large, and both executable.
Note: The /bin directory contains executables that can be used by the system administrator and any other user, and which are required for emergency system repairs. The /usr/bin directory is the primary directory for executable commands on the system.
4. Find Symbolic Links
Using the -a option lists all the paths containing an instance of the specified program. While multiple versions of the same program can exist on a system, sometimes one of the instances is only a symbolic link and not a binary file.
For example, running the following command outputs two instances of the atq command:
Again, use the ls command to check the details for both files. Run:
ls -lh /usr/bin/atq ls -lh /bin/atq
The output shows that both files are symbolic links ( -> ) only 2 bytes large and pointing to the at command.
5. Exclude Shell Built-ins
As previously mentioned, the which command excludes shell built-ins from its output.
For example, asking for the location of the read and man commands only outputs the location for the man command executable file, as read is a bash shell command.
This tutorial showed how to use the which command in Linux to find the path to a command’s executable binary. See and download our Linux commands cheat sheet for other essential Linux commands and examples of using them.