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. ]
Über den Autor
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.
Ähnliche Einträge
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
Nach Thema durchsuchen
Automatisierung
Das Neueste zum Thema IT-Automatisierung für Technologien, Teams und Umgebungen
Künstliche Intelligenz
Erfahren Sie das Neueste von den Plattformen, die es Kunden ermöglichen, KI-Workloads beliebig auszuführen
Open Hybrid Cloud
Erfahren Sie, wie wir eine flexiblere Zukunft mit Hybrid Clouds schaffen.
Sicherheit
Erfahren Sie, wie wir Risiken in verschiedenen Umgebungen und Technologien reduzieren
Edge Computing
Erfahren Sie das Neueste von den Plattformen, die die Operations am Edge vereinfachen
Infrastruktur
Erfahren Sie das Neueste von der weltweit führenden Linux-Plattform für Unternehmen
Anwendungen
Entdecken Sie unsere Lösungen für komplexe Herausforderungen bei Anwendungen
Virtualisierung
Erfahren Sie das Neueste über die Virtualisierung von Workloads in Cloud- oder On-Premise-Umgebungen