Searching for Files in Linux (find and fd)
The find command is used to search for files in Linux. It is very flexible: you can search recursively from any location and use parameters such as a file name, extension, regular expression or modification date. It can also mass-delete files or change the permissions of all files that match the search criteria.
General syntax of the find command
Here's the general way to use the command. The syntax is as follows:
find <options> <path> <expression>Explanation:
The "options" attribute specifies additional search parameters, allowing you to optimize and narrow down the search,
The "path" attribute specifies the folder where the search should start (folders inside the specified location are also included),
The "expression" attribute specifies the expression describing the file name — this can be a name or a description of the name
Examples of using the find command
Search by name
The simplest way to search by name is to use the name parameter. We don't specify a location, so the search starts in the folder you're currently in and also searches its subfolders recursively.
find -name plik.txt # file named plik.txt from the current directoryTo search without regard to letter case, use the iname parameter.
find -iname plik.txt # file named plik.txt regardless of letter case, from the current directorySearch in a specific location
You can also narrow down the search by specifying where it should start. Enter "." (a dot) to start searching from the current directory. Enter "/" (a slash) to search the entire file system. Enter an exact location to search there.
find . -name plik.txt # file named plik.txt from the directory above
find / -name plik.txt # file named plik.txt anywhere in the file system
find /var -name plik.txt # file named plik.txt in the /var directoryThe search is always recursive, but you can specify how many directory levels deep it should go, meaning how many nested directories should be searched. Use the maxdepth parameter for this.
find /var -maxdepth 2 -name plik.txt # file named plik.txt in the /var directory, but no more than 2 levels deepSearch by extension
You don't need to provide a specific name. Entering "*" (an asterisk) replaces any sequence of characters.
find -name "*.jpg" # finds every file with the jpg extension from the current directorySearch for empty files
Use two additional parameters: type, which specifies what you want to search for (the value "f" means files), and empty.
find . -type f -empty
# finds empty files from the directory aboveSearch for directories
You can also search for directories. To do this, use the type parameter with the value "d".
find . -name log -type d # finds directories named log from the directory aboveSearch by modification date
Use the mtime parameter to specify the maximum number of days ago the file could have been modified.
find . -type f -mtime -3 # finds files modified during the last 3 days from the directory aboveSearch for files that do not match a query
You can search for files by specifying an expression they must not match. To do this, use the not parameter.
find . -type f -not -name "*.html" # finds files with an extension other than html from the directory aboveRun an operation on found files
You can immediately run operations on the files you find. For example, you can set their permissions or change their last access time. Use the exec parameter, which runs the specified action for each search result.
find . -name "plik.txt" -exec chmod o+r '{}' \;
# sets permissions for plik.txt files from the directory above
find . -name "plik.txt" -exec touch '{}' \; # sets the last access time to now for plik.txt files from the directory aboveDelete found files
To delete files, you can use the exec parameter with the rm command, but using the delete parameter is more convenient.
find . -name "*.txt" -delete
# deletes files with the txt extension from the directory aboveIs searching too slow?
Try the fd command. It is an alternative to the standard find command written in Rust. Its main advantage is speed, while the downside is more limited functionality focused on the most common use cases. To use it, install the required package.
sudo apt update
sudo apt install fd-findGeneral syntax of the fd command
Using it is very simple.
fdfind wyrażenieExamples of using the fd command
Search by name
Enter part of the name or the full name. You can use "*" (an asterisk) to replace part of the name.
fdfind log # finds files containing logSearch using a regular expression
By default, the query is treated as a regular expression. Put it in quotation marks/apostrophes, start it with ^ (caret) and end it with $ (dollar sign). You can use any regular expressions inside. The example below finds all files starting with the word log and ending with the word txt.
fdfind '^log.*txt # finds files starting with the word log and ending with the word txtSearch in a specific location
Specify the location after the name of the file you're looking for.
fdfind log /var # finds files containing log in their name in the /var directoryMore ways to search
You can find more use cases on the authors' Github page.
Search in a specific location
Specify the location after the name of the file you're looking for.
fdfind log /var # finds files containing log in their name in the /var directoryMore ways to search
You can find more use cases on the authors' Github page.
Comments (0)
No comments yet.
Add a comment
Comments are published after moderation. Your e-mail address stays private.