
The find command in Linux is a powerful and versatile utility for searching or locating files and directories in a file system. The command uses file attributes like name, size, time, and permissions to perform the search. You can also combine find with other commands to perform complex searches.
This article explains how to use the find command in Linux to effectively manage and search files.
find Command SyntaxThe following is a basic find command syntax:
In the above find command:
[path]: Specifies the directory to search. It defaults to the working directory . if you don't specify a path.[expression]: Combines options, tests, and actions to filter and manipulate the search results such as the filename, file type, size, and modification time.Follow the steps below to set up example directories and files to test the find command.
Switch to your user's home directory.
Create vultr.txt, Vultr.txt, and VULTR.txt sample files.
Create dir, dir1 directories. Then create a sub_dir sub-directory under dir1.
Create a new file.txt text file.
Change the file.txt file permissions mode to 755.
Check out how to use the tar command in Linux for quick file archiving and unzipping.
find Command OptionsThe find command supports the following options for refining search results.
find Command in Linux with ExamplesFollow the steps below to test the find command and search the example files you created earlier.
Find a file named vultr.txt in your working directory and subdirectories.
Output:
Perform a case-insensitive search for all files that match vultr.txt.
Output:
Find all directories in the working directory using the -type d option.
Output:
Find files by size. For example, files larger than 100MB.
Output:
Find files by modification time. For example, find files modified in the last 7 days.
Output:
Find all files by access time. For example, find all files accessed in the last 7 days.
Output:
Find all files a specific user owns, such as vultr_user.
Output:
Find all files owned by a specific group such as vultr_user.
Output:
Find all files with 755 permissions.
The 755 permission means the owner can read, write, and execute the file, while others can only read and execute it.
Output:
find Command OptionsFollow the steps below to perform advanced find command searches.
Execute a command on each matching file using the -exec option.
The above command finds all files named vultr.txt and runs the ls -l command on each file to list them in a long format. {} is a placeholder for the working filename, and the \; specifies the end of the command to execute.
Output:
Delete all files named vultr.txt.
Output:
Find all .txt files and copy them to another directory.
The above command finds all .txt files and copies them to the /path/to/directory/ directory. The file name replaces {} during execution. Replace /path/to/directory with your actual path.
Find all .txt files and move them to another directory.
The above command finds all .txt files and moves them to the /path/to/directory/ directory. The filename replaces {} during execution. Replace /path/to/directory with your actual path.
Output:
Find all .txt files a specific user owns, such as vultr_user and modified in the last 7 days.
The above command searches for all .txt files owned by the vultr_user and modified within the last 7 days. It combines the -name, -user, and -mtime options to create a more specific search.
Output:
find Command in Linux with ExamplesFollow the steps below to perform interactive searches using the find command.
Find all files named Vultr.txt and list them with detailed information.
The above command finds all files named Vultr.txt and uses ls -l to display detailed information about each file, such as permissions, owner, size, and modification date.
Output:
Combine the find command with wc to locate all files named Vultr.txt and count the number of lines.
The above command finds all files named Vultr.txt and uses the pipe (|) option to output the results to the wc -l command, which counts the number of matching lines.
Output:
Find all files with 644 permissions and change them to 600.
The above command finds all files with 644 permissions and changes the permissions mode to 600 using the chmod command. The {} option represents the filename, while \; marks the end of the command.
You have used the find command in Linux with practical examples to efficiently search for files and directories. The find command performs searches based on a specific criteria and runs specific actions on the matched results to streamline your file system workflow.
0 Comments
Be the first to comment and share your perspective with the community.