find folder name in linux

How to Find Folder By Name in Linux

Typically, we search for filenames using grep or find command in Linux. But sometimes you may need to find directory name in Linux or find folder name in Linux. In this article, we will look at how to find directory name in Linux. There are a couple of different ways to find folder name in Linux. We will look at each of them separately.


How to Find Folder By Name in Linux

Here are the different ways to find folder name in Linux.


1. Using Find & Grep Commands

In this approach, we use a combination of find and grep commands to find directory in Linux. Here is an example to find directory with name ‘projects’ in Linux in /home/ubuntu directory

$ sudo find /home/ubuntu -type d | grep 'projects'

In the above command the find command finds all directories in /home/ubuntu folder, and passes its output to grep command, which searches for ‘projects’ in it and lists only matching records.

In the above command, replace /home/ubuntu with your choice of search location and ‘project’ with your choice of folder name.


2. Using ls & Grep Commands

In this approach, we use ls command to list all contents of search location recursively, and pass its output to grep command to look for required folder e.g. /projects

$ sudo ls -lR /home/ubuntu | grep '^d' | grep '/projects'

ls -lR command recursively lists all files and folders of specified folder, along with their permissions. We pass this to grep command which searches for results beginning with ‘d’ character in Linux. This is because the permissions of directory start with ‘d’ character. Here is an example

drwxrwxr-x 2 root     ubuntu 4096 Dec  9 06:02 /project
drwxrwxr-x 2 root     ubuntu 4096 Dec  9 05:03 /data
drwxrwxr-x 2 root     ubuntu 4096 Dec  9 03:04 /files

Now we pass the above output again to grep command, this time searching for folder name ‘/project’.

drwxrwxr-x 2 root     ubuntu 4096 Dec  9 06:02 /project

That’s it. In this article we have learnt two simple ways to easily find directory by its name in Linux. You can use these commands in every Linux distribution since they are readily available in every Linux distribution.

Also read:

How to Debug Shell Script
How to Sort Text File in Python
How to Check Open Ports in UFW
How to Check UFW Log & Status
How to Create Symbolic Links in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *