- How To Find Directory In Linux?
- Find Directories and Files with find Command
- Get Detailed Information about The Directories and Files
- Only List Directories
- Locate
- How To Find a Directory On Linux Based System
- How to find a directory on Linux
- Linux find directory command
- Finding a directory
- Dealing with “Permission denied error messages” on Linux
- How to find a directory named Documents on Linux?
- Getting a detailed list of files/dirs
- How do I list only directories?
- How do I perform a case insensitive search?
- How do I find a directory called project.images?
- A note about locate command
- How to find and delete directory recursively on Linux or Unix-like system
- Find command syntax to delete directory recursively
- Finding and deleting directory recursively using xargs
- Shell script to recursively remove backups older than 30 days
How To Find Directory In Linux?
Linux provides different ways to find directories. Here we will look at how to find directories in a recursive way. In this tutorial, we will use commands like find and locate .
Find Directories and Files with find Command
Find command is a popular command to used a lot of different purposes like find file and directory, take a backup, copy files. Actually find do not have these abilities it just runs commands over search results like copy and backup.
Syntax
- `LOCATION` is the path or location we will search in
- `SEARCH_TERM` is the term which is the file or directory name we want to search
- `ACTION` is optional which can take actions like print, delete, rename in the search results
We will search for a directory bin in the root file system.
Find Command
Get Detailed Information about The Directories and Files
We can get detailed information about found directories by using -ls parameter for the find command.
Get Detailed Information
Here results will list the size of the directory permission of the directory, owner, last change date, etc.
Only List Directories
Up to now, we have searched for all files and directories. We can search for only directories by providing a type parameter with a directory specifier.
Only List Directories
Locate
Locate command is a non-interactive alternative to find command. Also, locate have restricted capabilities. The advantage of the locate command is that it is fast because locate use database to search. Manually a database for file and directories is created. Manually this database is updated. The search is done directly in this database. Database is located at /var/lib/mlocate/mlocate.db .
We will update our database to search with locate command. To update the locate database we need root privileges.
We will search for files and directories ends with /bin . In this example, we will use the regex option of the locate to specify the end of the line.
Locate
Источник
How To Find a Directory On Linux Based System
I just switched from MS-Windows server admin to Debian Linux server system administration roles. I need to find a directory called project.images. I was also told that the locate command is the simplest and quickest way to find the locations of files and directories on Linux. But the locate command is not working out for me. How do I find project.images directory using command-line options only?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | find command on Linux or macOS/Unix |
Est. reading time | 5m |
You need to use find command. It is used to locate files on Linux or Unix-like system. The locate command will search through a prebuilt database of files generated by updatedb.
The find command will search live file-system for files that match the search criteria.
How to find a directory on Linux
The find command syntax is:
find /where/to/look/up criteria action
find /dir/path/look/up criteria action
find /dir/path/look/up -name «dir-name-here»
find /dir/path/look/up -name «pattern»
find /dir/path/look/up -name «dir-name-here» -print
find /dir/path/look/up -name «dir-name-here»
find / -name «dir-name-here»
find / -type d -name «dir-name-here»
find / -type d -name «dir-name-here» 2>/dev/null
Linux find directory command
The following example will show all files in the current directory and all subdirectories:
Finding a directory
To find a directory called apt in / (root) file system, enter:
Alert: When searching / (root) file system, you need to run the find command as root user.
Dealing with “Permission denied error messages” on Linux
Find will show an error message for each directory/file on which you don’t have read permission
How to find a directory named Documents on Linux?
Type the following command to search for Documents directory in your $HOME dir:
$ find $HOME -type d -name Documents
Sample outputs:
Getting a detailed list of files/dirs
Pass the -ls to list current file in ls command output format:
How do I list only directories?
Just find directories and skip file names pass the -type d option as follows:
How do I perform a case insensitive search?
Replace -name option with -iname as follows:
The patterns ‘apt’ match the directory names ‘apt’, ‘APT’, ‘Apt’, ‘apT’, etc.
How do I find a directory called project.images?
Type any one of the following command:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
A note about locate command
See also
- All find command examples from our /faq/ sections.
- Find command man page
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Why don’t you run updatedb and then locate and again you’ll have “simplest and quickest way to find the locations of files and directories on Linux”.
updatedb will update your database.
That only helps for semi-permanent files since it only checks periodically to update the updatedb database. For files that were created recently it will not be found.
Find is a great tool that i use a lot.
You could have talk about the -exec switch wich allows you to process the outpout. ie : find and delete all file in ./ that haven’t been modified since 90 day:
Anyway, great job on this website, keep it on!
I have to second that updatedb is the way to go for a novice linux user. No worries about syntax and whatnot. Its also very useful for when you need to do multiple scans since you only traverse the filesystem once.
In regards to -exec, you should be using -execdir when available due to some security implications… and the above rm -rf is somewhat dangerous since find by default traverses from the top down. Delete would be a much safer (and faster!!) operation than-exec rm.
I have to agree with your update, rm -Rf is maybe too dangerons to use for novice users.
I did not know -execdir wich seems to be very usefull.
Thank you for that update =)
Great article. so useful. since I don’t have root, I get very verbose “no permission” output that is useless and I have to find the actual location through all the muck. Is there a way to only print found paths? Thanks so much for this article!
All those “no permission” messages should be on stderr while the information you want is on stdout. Both stderr and stdout default to printing on the controlling terminal. Tacking “> stdoutfile” on the end of the command would separate them, leaving all the unwanted noise on the terminal and putting the good stuff in stdoutfile. It would make more sense to redirect stderr to /dev/null (throwing it away) and leaving the useful output on the controlling terminal, but that would require finding the instructions for redirecting stderr in the shell docs (again).
To avoid seeing stderr messages, just use something like this:
Источник
How to find and delete directory recursively on Linux or Unix-like system
Find command syntax to delete directory recursively
Try the find command:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf <> +
Another option is as follows to recursively remove folders on Linux or Unix:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf \;
Warning : Be careful with the rm command when using with find. You may end up deleting unwanted data.
Find will execute given command when it finds files or dirs. For example:
find . -type d -name «foo» -exec rm -rf <> +
OR
find . -type d -name «bar» -exec rm -rf «<>» \;
Sample outputs:
You can find directories that are at least four levels deep in the working directory /backups/:
find /backups/ -type d -name «bar» -depth +4 -print0 -exec rm -rf <> +
Finding and deleting directory recursively using xargs
The syntax is as follows to find and delete directories on Linux/Unix system. For example, delete all empty directories:
find /path/to/dir/ -type d -empty -print0 | xargs -0 -I <> /bin/rm -rf «<>»
In this example, remove all foo directories including sub-diretories in /backups/ folder:
find /backups/ -type d -name «foo*» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
The second command is a secure version. It is fast too, and it deals with weird directory names with white spaces and special characters in it:
Hence, I would like readers to use the following syntax:
find /path/to/search/ -name «pattern» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
Where find command options are:
- -name «pattern» : Base of file name that matches shell pattern pattern. For example, foo, Foo*3 and so on.
- -print0 : Show the full file name on the standard output, followed by a null character. This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs
And xargs command options are:
- -0 : Input items given by find are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
- -I <> : <> in the initial-arguments with names read from standard input. For example, dir names given by find command./li>
- /bin/rm -rf «<>« : Run rm command that remove files or directories passed by <> .
Shell script to recursively remove backups older than 30 days
Here is my sample script that removes older weekly backup of my VM tarballs stored in the following format:
Источник