How to use the Find command on Linux

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 b…


This content originally appeared on DEV Community and was authored by Gilbert Nkolo Acquostic

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.

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.

find / -type d -name "*insert search term*"

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.

  1. 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).

  1. Finding recently modified files:
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.

  1. 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.

  1. 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”.

  1. 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.

  1. 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.


This content originally appeared on DEV Community and was authored by Gilbert Nkolo Acquostic


Print Share Comment Cite Upload Translate Updates
APA

Gilbert Nkolo Acquostic | Sciencx (2024-06-27T23:41:14+00:00) How to use the Find command on Linux. Retrieved from https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/

MLA
" » How to use the Find command on Linux." Gilbert Nkolo Acquostic | Sciencx - Thursday June 27, 2024, https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/
HARVARD
Gilbert Nkolo Acquostic | Sciencx Thursday June 27, 2024 » How to use the Find command on Linux., viewed ,<https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/>
VANCOUVER
Gilbert Nkolo Acquostic | Sciencx - » How to use the Find command on Linux. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/
CHICAGO
" » How to use the Find command on Linux." Gilbert Nkolo Acquostic | Sciencx - Accessed . https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/
IEEE
" » How to use the Find command on Linux." Gilbert Nkolo Acquostic | Sciencx [Online]. Available: https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/. [Accessed: ]
rf:citation
» How to use the Find command on Linux | Gilbert Nkolo Acquostic | Sciencx | https://www.scien.cx/2024/06/27/how-to-use-the-find-command-on-linux/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.