- Unix Commands Reference
- Unix Commands - Home
find2perl Command in Linux
The Linux find2perl command translates the find command syntax into a Perl code. The Perl code typically runs faster than the find command itself. They can also be modified for further operations to enhance the search process.
Table of Contents
Here is a comprehensive guide to the options available with the find2perl command −
- Prerequisites to Use find2perl Command
- Syntax of find2perl Command
- Options of find2perl Command
- Examples of find2perl Command in Linux
Prerequisites to Use find2perl Command
The find2perl is a Perl module that needs to be installed in Linux. The steps of installing find2perl in Linux are listed below.
First, ensure that the Perl is installed by checking its version −
perl --version
In all modern Linux distributions, Perl is pre-installed. If it is not, install it using the respective package manager.
Now, install CPAN (Comprehensive Perl Archive Network) using the commands given below −
sudo apt install cpanminus #Debian/Ubuntu sudo yum install cpanminus #CentOS/Fedora sudo pacman -S cpanminus #Arch Linux
Now, install the find2perl module using the command given below −
sudo cpanm App::find2perl
Verify the installation using the which command −
which find2perl
If it shows the path of the find2perl command bin file, then it indicates that the utility has been installed.
Syntax of find2perl Command
The syntax of the find2perl command is as follows −
find2perl [paths] [predicates] | perl
In the above syntax, the [paths] option is used to specify the path from where the search will start. The [predicates] field is used to specify conditions such as wildcards or patterns, just like the find command. The | is used to pipe the output of the find2perl command to the perl interpreter. The | perl is optional; it executes the Perl code and displays it to the standard output; and without it, the Perl code will be displayed.
Options of find2perl Command
The options used with the Linux find2perl command are listed below −
Options | Description |
---|---|
! Predicate | It is used to negate the predicate |
( Predicates ) | It is used to group multiple predicates |
Predicate1 Predicate2 | It is true if both predicates are evaluated to be true |
Predicate1 -o Predicate2 | It is true if one of the predicates is true |
-follow | It is used to enable symlink follow |
-depth | It is used to set depth-first directory traversal instead of breadth-first |
-prune | It is used to skip the descending into the matched directory |
-xdev | It is used to prevent the directory traversal on different filesystems |
-name GLOB | It is used to match files with names that fit the specified wildcard pattern |
-iname GLOB | It is used to enable the case-insensitive filename match |
-path GLOB | It is used to match file paths with names that fit the specified wildcard pattern |
-ipath GLOB | It is used to enable the case-insensitive file path match |
-perm PERM | It is used to match the files with specified permission bits |
-perm -PERM | It is used to match the files with specified permission bits that are all set |
-type X | It is used to match files of a specific type (f is used for regular files) |
-fstype TYPE | It is used to match files on the specified filesystem |
-user USERNAME | It is used to match files owned by the specified user |
-group GROUP | It is used to match files owned by a specified group |
-nouser | It is used to match files owned by no user |
-nogroup | It is used to match files belong to no group |
-inum NUM | It is used to match files with the specified inode number |
-links N | It is used to match files with the specified number of hard links |
-size N | It is used to match files with the specified size |
-atime N / -ctime N / -mtime N | It is used to match files according to the access, change, and modification time in days |
-newer FILE | It is used to match the files modified recently |
It is used to print the paths of each matching file | |
-print0 | It is used to print the paths following by null instead of a new line |
-exec OPTIONS | It is used to execute the external command with options for each matching file |
-ok OPTIONS | It is equivalent to -exec but prompt the user before executing |
-eval EXPR | It is used to evaluate the Perl expression |
-ls | It is used to simulate -exec ls -dils {}; |
-tar FILE | It is used to add matching files to a tar archive named FILE |
-cpio FILE | It is used to add matching files to an old-style cpio archive named FILE |
-ncpio FILE | It is used to add matching files to a new-style cpio archive named FILE |
Predicates in the list above that take numeric arguments N can come in three forms: +N, -N, and N. "+N" is greater than "N", "-N" is less than "N", and "N" is equal to "N".
Examples of find2perl Command in Linux
This section demonstrates the usage of the find2perl command in Linux through examples −
- Translating find Command to Perl Code
- Saving the Perl Code to a File
- Generating Perl Code to Search Excluding Specific Files
- Generating Perl Code to Search a Group of Files
- Generating Perl Code to Search with Multiple Arguments
- Generating Perl Code to Search with Depth-first Traversal
- Generating Perl Code to Search Excluding a Specific Directory
- Generating Perl Code to Search with the File Path
- Generating Perl Code to Search with Specific Permission Bits
- Generating Perl Code to Search by Specific User or Group
- Generating Perl Code to Search by Time
- Generating Perl Code to Search by Executing a Command
Translating find Command to Perl Code
To translate the find command output to the Perl code, use the find2perl command with the file path argument and predicate −
find2perl . -name "*.txt"
The above command will find all the files with .txt extension in the current directory and display the Perl code to the standard output. The dot "." indicates the current working directory.
To interpret the code and display its output to the stdout, use the | perl argument −
find2perl . -name "*.txt" | perl
To ignore the case, use the -iname option −
find2perl . -iname "*.txt"
Saving the Perl Code to a File
To save the Perl code to a file, use the redirection operation > with the file name −
find2perl . -name "*.txt" > output.perl
To execute the Perl code, use the perl interpreter with the file name −
perl output.perl
Generating Perl Code to Search Excluding Specific Files
To exclude the specific files from the search and generate Perl code, use the ! with the predicate. For example, to exclude .pdf files, use the following command −
find2perl . ! -name "*.pdf"
Generating Perl Code to Search a Group of Files
To include a group of files in the search, use the -o option with the predicates −
find2perl . \( -name "*.html" -o -name "*.c" \)
The above command generates the Perl code that can search including .html and .c files.
Generating Perl Code to Search with Multiple Arguments
Files can be searched using multiple arguments, such as name, time, or size. The following command only searches the text files exactly equal to the specified size in the current working directory and generates the Perl code.
find2perl . -name "*.txt" -size 1M
To search all files that are greater than 1M, use the + sign −
find2perl . -name "*.txt" -size +1M
Similarly, to search all files smaller than the specified size, use the - sign −
find2perl . -name "*.txt" -size -1M
Generating Perl Code to Search with Depth-first Traversal
To enable the depth-first traversal in the search, use the -depth option −
find2perl . -depth -name "*.txt"
Generating Perl Code to Search Excluding a Specific Directory
To search and exclude a specific directory, use the -prune option. The following command skips the my_docs directory while searching all the files with the .txt extension in the current working directory and generates a Perl code.
find2perl . -name "my_docs" -prune -name "*.txt"
Generating Perl Code to Search with the File Path
To generate the Perl code of all the text files in the specific directory path in the current working directory −
find2perl . -path "*/my_docs/*.txt"
To ignore the case, use the -ipath option.
Generating Perl Code to Search with Specific Permission Bits
To get the Perl code for files with specific permission bits, use the -perm option −
find2perl . -perm 644
Generating Perl Code to Search by Specific User or Group
To generate the Perl code of the find command that searches the files owned by a specific user, use the -user option −
find2perl . -user sam -name "*.txt"
Similarly, for files owned by a specific group, use −
find2perl . -group linux -name "*.txt"
Generating Perl Code to Search by Time
To generate the Perl code of the find command that searches the files by a specific modification time in days, use the -mtime option −
find2perl . -mtime -4 -name "*.txt"
The above command searches for files that have been modified in the last four days.
Similarly, the commands for the last access time and change time are given below −
find2perl . -ctime -4 -name "*.txt" find2perl . -atime -4 -name "*.txt"
Generating Perl Code to Search by Executing a Command
The files can be searched by executing an external command. For this purpose, the -exec option is used. To execute the ls command to each searched file in the current working directory and generate a Perl code, use the command given below −
find2perl . -name "*.txt" -exec ls {} \;
Similarly, to execute the cat command, use −
find2perl . -name "*.txt" -exec cat {} \;
To enable prompt on each execution, use the -ok option −
find2perl . -name "*.txt" -ok cat {} \;
Conclusion
The find2perl command in Linux is used to translate the find command into the Perl code. The Perl code of a complex find command can execute faster than the find command. To use the find2perl command on Linux, it needs to be installed.
In this tutorial, we explained the find2perl command, its syntax, options, and usage in Linux through various examples.