Information Technology Tips

Innotechtips_logo_v4
Menu
  • Home
  • Linux
  • Programming
  • Other Tech Tips
    • Windows
    • Security
Youtube
Home Linux

How to Find Files in Linux Using the Find Command

Inno by Inno
July 27, 2023

The find command in Linux is a useful tool for searching for files and directories. The find command offers different search criteria or options including searching by file name, type, size, owner, modification time, empty files, etc. The searches are performed in a recursive manner. Additionally, there is an added flexibility of searching using regex patterns.

Examples:

To find a file by name in the current directory, use the below find command:

find . -name testfile1

To search for a file by name but ignore the case:

find . -iname testfile1

To find a file by name and search recursively from the root directory, use:

find / -name auth.log

find / -name auth.log  2>/dev/null   (here the 2>/dev/null is used to suppress error messages)

To search specifically for files that match a given search string:

find . -type f  -name “test*”

To search specifically for directories by matching the given search string:

find . -type d  -name “test*”

To find files of a specific size:

To find hidden files:

find . -type f -name “.*”

To find files that were modified in the last 1 hour:

find / -cmin -60  2>/dev/null

To find files that were modified more than 1 day and less than 40 days ago:

find . -mtime +1 -mtime -40

To find files that have specific permissions:

To find empty directories, use the below find command:

find /tmp -type d -empty 2>/dev/null

Another powerful aspect of the find command is the ability to take its results and provide them as input for other commands. Consequently, we perform specific actions on files or directories that we discover using the find command. The find command can be used together with the xargs command to pipe output and then feed it to a different command as input. The xargs command makes it possible to input results to commands, such as mkdir and rm, that do not accept piped input. In the below example, we are feeding the files found by the find command into the xargs command, which in turn pipes them into the gzip and gunzip commands that compresses and decompresses those files respectfully.

Command to compress files that start with “test”:

find ./ -name “test*” -type f -print0 | xargs -0 gzip

Command to decompress files that start with “test”

find ./ -name “test*” -type f -print0 | xargs -0 gunzip

ls -l  –> Command to list all files

find ./ -name “test*”  –> Find command; start the search in the current directory and search by the name for those files that match the string “test*”

-type f –> Specifies to search for only files and not directories.

-print0 –> This argument tells the find command not to treat whitespace as the end of a filename.

xargs –> Reads items from standard input and executes some action for each item

xargs -0 –> The -0 option tells xargs not to treat whitespace as the end of a filename.

The find command also supports the use of the -exec option to perform actions on those files that are found using the find command. The two examples below provide a comparison of using xargs and -exec option to execute a rm action on files resulting from the find command.

find ./ -name “*.txt” -type f | xargs rm

find ./ -name “*.txt” -type f -exec rm {} \;

Inno

Inno

Related Posts

Linux

Getting Started with Kali Linux: A Guide for Beginners

July 31, 2024
Linux

20 Linux Commands for Beginners

March 31, 2024
Linux

What is KVM Virtualization?

March 31, 2024
Linux

The Linux Booting Process in Linux

February 17, 2024

Category

  • Linux
  • Other Tech Tips
  • Programming
  • Windows

Recommended.

How to Configure the Firewall on Ubuntu Linux using ufw

July 27, 2023
File/Directory Ownership and Permissions in Linux

File/Directory Ownership and Permissions in Linux

July 27, 2023

Trending.

How To Connect Kali Linux to Wi-Fi on VirtualBox

October 9, 2023

How to Install and Enable Free VPN in Kali Linux

July 27, 2023

How to Connect Kali Linux on VMware Workstation to a Wi-Fi Network

July 27, 2023

How to Capture Network Traffic on a Wi-Fi Network using Kali Linux

July 27, 2023
How to Connect to Kali Linux from Window’s Command Prompt

How to Connect to Kali Linux from Window’s Command Prompt

July 27, 2023

About us

This site is dedicated towards highlighting various important aspects of information technology. The key areas includes programming, Linux, software, and security. The content will include articles as well as videos.

Quick Links

Menu
  • Home
  • Linux
  • Programming
  • Other Tech Tips
    • Windows
    • Security

Privacy Policy

Menu
  • Privacy Policy
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}
No Result
View All Result
  • Cookie Policy (EU)
  • Home 1
  • Home 2
  • Home 3
  • Mytest Page
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.