Learn Linux: Find Files And Directories By Name Using Linux Terminal Commands

Jason Bodie
7 min readMay 14, 2024

--

Linux Find Command
Linux Find Command

Welcome to the world of Linux File Management! In this article, I’ll teach you how to use the “find” command. I have both a simple example and a more in-depth explanation.

The Linux operating system can seem daunting at first. However, with a little knowledge about pathnames and how to navigate through directories, you’ll be organizing your files like a pro in no time.

What Is The Linux Terminal Find Command

The “find” command on the Linux terminal is a powerful tool used to locate files and directories within a specified directory or throughout the entire file system. It allows users to search for files by name, size, type, modified date, and other criteria. The find command also has advanced features such as searching based on permission settings and executing actions on the found files.

It is commonly used by system administrators and developers to efficiently manage their file systems and perform complex tasks such as backups, data transfers, and performing bulk operations. With its versatile capabilities, the find command offers users a comprehensive solution for navigating through large amounts of data quickly and efficiently.

Know Your Basic Linux Terminal Commands:

To navigate through these directories efficiently, you should familiarize yourself with some basic commands. These are:

  • “cd” or Change Directory: This command allows you to move from one directory to another.
  • “ls” or List: This command lists all the files and folders present in the current working directory.
  • “pwd” or Print Working Directory: This command displays the full path of your current working directory.
  • “find” or Find Specific File or Directory: The “find” command as mentioned above it allows you to find files and directories.

How To Find A Specific Directory or File:

Here is the find command in its most simplest form. If you need a more detailed explanation. I have one below.

# Exclude -type d when searching files
find / -type d -name "*insert search term*"

How To Use The Linux Find Command In Detail

This is the find command structure. It comes equipped with many options which I have listed below.

find [directory] [options] [expression]

The “find” command has many options that can be used to customize the search criteria.

  • -name: This option allows you to specify the name or pattern of the file you want to find. For example, “-name *.txt” will search for all files with .txt extension.
  • -type: With this option, you can specify whether the file should be a regular file (-type f), directory (-type d), or symbolic link (-type l).
  • -size: This option lets you search for files based on their size. You can use modifiers like + (greater than), — (less than), or c (bytes) to refine your search.
  • -user/-group: These options allow you to find files owned by a specific user/group.
  • -mtime/-atime/-ctime: Using these options, you can search for files based on their modification/access/change time respectively.
  • -perm: With this option, you can find files based on their permissions using octal numbers as arguments.
  • -maxdepth/mindepth: These options let you limit the depth of subdirectories that are searched while finding files.
  • -exec/-ok: These options allow executing commands on found items either automatically (‘-exec’) or after user confirmation (‘-ok’).
  • -prune: This option excludes specified directories from being searched within while finding files recursively in a given path(s).
  • -newer/!newer: The ‘newer’ operator finds only those items modified more recently than the reference item defined as an argument whereas ‘!newer’ does vice versa.
  • — help/ — version (dash-dash): These two common help switches print usage information and version details respectively when provided with other valid arguments.

Some common examples of using the “find” command are:

1. Finding all files with a specific name:

find /home/user -name example.txt

This will search for all files named “example.txt” under the “/home/user” directory.

2. Finding all directories with a specific permission:

find /var/www -type d -perm 755

This will search for all directories under “/var/www” that have read, write, and execute permissions for the owner (7) and read/execute permissions for others (5).

3. Finding recently modified files:

# shows everything modified within past 24 hours
find ~/Documents -type f -mtime -1

# shows everything modified before the last 24 hours, 2 days ago and beyond
find ~/Documents -type f -mtime +1

This will search for all files in the “~/Documents” directory using the last modified option.

4. How To Use The -exec Option on Files Filtered by The Find Command

The -exec option allows for the execution of a specified command on every file that is discovered during the search.

This option must be followed by the desired command and its corresponding arguments, with {} serving as placeholders for each file’s path.

To denote the completion of the -exec command, \; (a backward slash and semi-colon) must be included.

Example:

sudo find /var/www -name "*.html" -exec mv {} ~/html \;

Here we execute the move (mv) command on all .html files within the /var/www directory to the home HTML directory.

5. How To Use The Find -ok Option on Files

The -ok option functions similarly to the -exec option, except that it prompts for confirmation before executing the operation on each file. This is a highly useful command as it allows you to review which files will be affected before proceeding with the specific operation.

You also have the choice to decline if you are uncertain or do not want to apply the command.

For instance, let’s attempt to move all .txt files into another directory using this feature.

find ~/tmp -name "*.txt" -ok mv {} ~/tmp/final \;

Here we’re iterating over every text file within the ./tmp directory and manually choosing whether or not we want to move the file into the ./tmp/final directory. You need to type “yes” or “no”.

6. How To Use -delete with The Find Command

The -delete option can be useful if you need to free up space on your computer or if you want to remove certain files that are no longer needed. It is important to use this option carefully because it permanently deletes the files without any confirmation, so make sure you are certain about which files you want to delete before using it.

find . -name "*.html" -delete

This deletes all HTML files within the current directory.

7. Combining multiple conditions using logical operators:

sudo find /var -type f \( -name "*.conf" \) ! \( -user ubuntu \)

This will find all files in the “/etc” directory, with names ending in “.conf”, but not owned by user “root”. It will list them all out for you.

Linux Tip: Don’t Forget To Use Wildcards With Your Search If Needed

Sometimes we may not remember the exact name or spelling of a particular folder but know certain keywords associated with it. In such cases using wildcards can be helpful.

For instance:

- To search for all folder names starting with “intro”, use intro*

- To find all folders ending with “music”, try *music

The asterisk (*) acts as a placeholder for unknown characters.

For more information about available options and expressions, you can refer to the manual page of “find”. You can access it by typing `man find` in your terminal or online resources such as https://linux.die.net/man/1/find.

Linux File Management System: Understanding Both Relative And Absolute Paths

Pathnames are essentially the address or location of a file or directory within your system. They provide a way for your computer to locate and access specific files that you want to work with. There are two types of paths — absolute paths and relative paths.

Absolute paths start from the root directory (represented by “/”) and list out all the folders/directories needed to reach the desired file, starting from the root all the way down to the specific file itself. This is useful when working with files located in different areas of your system as it provides an exact pathway for accessing them. Example: “/var/www”

On the other hand, relative paths only specify how many steps away from your current location (or directory) you need to go in order to find the desired file or folder. Example: “./Documents/”

How To Find A Directory On Linux Conclusion

In conclusion, using the Find command on Linux is a great way to quickly locate directories on your system. By adding various options and arguments, you can make your searches even more specific and precise.

Remember, practice makes perfect so don’t be afraid to explore and experiment with various commands and techniques until they become second nature. Always keep an eye out for new tricks and shortcuts that can make your life easier when working with directories in Linux.

--

--

Jason Bodie

Internet Enthusiast. I search the planet for noteworthy information. Follow to learn more.