The way to discover what you are looking for can be deeper than you thought, or it may be right around the corner. It all depends on how you look, where you look, and what you are looking for. When you are troubleshooting problems that arise, it can be easier than it seems. One of my favorite tools is the find command. The find command is a utility to walk through a directory tree, descending into directories with each path listed, and evaluating expressions added for each file listed. With this command, you can accomplish quite a lot. In this article, I cover different ways, as well as one-liners, to help you to find large files, to find multiple files, and even to locate specific file types.
Find multiple files in Linux
The find command is used in various ways. One thing you don't want to do as a system administrator is work harder than you need to. Instead of running the same command to search for one file over and over, you can use the find command to locate multiple files at the same time.
$ find /home -type f -name file.txt -exec {} \;
This one-liner can be broken down. I find it best almost to read it as a sentence:
- searching the
/homedirectory - searching for a file (
-type f) or a directory (-type d) - filename is file.txt (
-name file.txt) - executing another command from the previous output
Find large files in Linux
You can also use find to discover large files in Linux. Finding large files has proven helpful to me in the long run. find can help to locate large files quickly, such as backups and ISO files.
$ find / -type f -size +500000k -exec ls -lh {} \;
This one-liner can be broken down:
- searching the
/directory - searching for a file (
type -f) - searching for a file larger than
500000k - executing the command
ls -lhon the files found in the previous output
Find specific file types in Linux
Another good method is to locate file extensions using the find command. I find this helpful, as it has shown me ways of finding specific files with only a specific keyword. In this case, the example below is looking for files that only contain a specific extension:
# find / -type f \( -name "*.sh" -o -name "*.txt" )
To dissect this:
- searching in the
/directory - searching for a file (
-type f) or a directory (-type d) - searching for a file name that is a wildcard but ends with the extension
.shor.txt
I have even looked for content within a file that matches a specific keyword. These commands can be tweaked and modified to achieve the desired result.
Find modified files in Linux
The last example shows how to find a file modified in the last 50 days. This can be helpful when you need to locate recently modified files due to a security reason or if there are unwanted users on the network accessing other files.
# find / -type f -ctime +50 -exec rm -f {} \;
The command above shows:
- searching in the
/directory - searching for a file (
-type f) or a directory (-type d) - searching for files older than
50days - executing the command
rm -fon the files found in the previous output
This can help remove those malicious files all in one go. You just have to make sure that the files you select are the files you want to remove. One way to check is to run the command without the exec section to see the files that come up in the output. If there are a large number of files, redirect the output into a file:
# find / -type f -ctime +50 > files.txt
The content can be reviewed and verified before you run a one-liner that removes the /etc folder. Not ideal.
Wrapping up
The find command has a variety of uses and availability where administrators can find the content they need (no pun intended). With this command, the possibilities are literally endless! When it comes to troubleshooting, having that flexibility in searching and investigating allows you to look for things that you may not have noticed before. As a result, you might just find the answer you were looking for (pun intended).
[ Free online course: Red Hat Enterprise Linux technical overview. ]
À propos de l'auteur
I currently work as a Cloud Support Supervisor for Acronis . I have worked with Linux and OpenSource tools for a decade, constantly wanting to make new resolutions for obstacles and always training others on improving systems as a systems administrator. I’ve helped manage and maintain elaborate servers and websites over the years. In my spare time, I enjoy spending time with my dogs, reading, and playing video games.
Plus de résultats similaires
More than meets the eye: Behind the scenes of Red Hat Enterprise Linux 10 (Part 4)
Why should your organization standardize on Red Hat Enterprise Linux today?
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
Parcourir par canal
Automatisation
Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements
Intelligence artificielle
Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement
Cloud hybride ouvert
Découvrez comment créer un avenir flexible grâce au cloud hybride
Sécurité
Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies
Edge computing
Actualité sur les plateformes qui simplifient les opérations en périphérie
Infrastructure
Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde
Applications
À l’intérieur de nos solutions aux défis d’application les plus difficiles
Virtualisation
L'avenir de la virtualisation d'entreprise pour vos charges de travail sur site ou sur le cloud