Practical Guide: Searching for Files under Linux
Published on 23 September 2024
Reading time: 5 to 7 minutes.
Audience: Beginners and intermediates.
The problem: Searching for files in a Linux operating system via the terminal.
Introduction
In this article, we will explore how to search for a file under Ubuntu using a Zsh terminal. Zsh (Z shell) is a powerful command-line interpreter widely used for its flexibility and advanced features.
Ubuntu, a popular Linux distribution, offers several command-line tools for searching for files. We will see how to use commands such as`find`, locate et `grep`to locate files quickly and efficiently in the terminal.
Using the find command
The order`find`allows you to search for files in a specific directory and its subdirectories. Its basic syntax is as follows:
find [chemin] -name [nom_du_fichier]
Here is a practical example where we are looking for a file named`example.txt`in the directory`/home`(No text provided for translation)
find /home -name "example.txt"
This command will traverse all subdirectories of`/home`and display the full path of the file if it is found.
*Online Ordering Manual`find`(The text to translate was not provided. Please provide the French text.)man findPlease provide the text you would like me to translate.
Additional options for `find
The order`find`is very powerful and has many options. Here are some useful examples:
-
Search by file typeTo search for files only, use`-type f`, and to search for directories, use`-type d`. -Search by sizeYou can also filter files by size using the option`-size`. Par exemple, pour rechercher des fichiers de plus de 100 Mo, utilisez :
find /home -size +100M
Using the locate command
The order`locate`is faster than`find`because it uses a pre-indexed database of the files present on the system. You must first update this database with the following command:
sudo updatedb
Next, you can search for a file like this:
locate example.txt
I usehttps://fr.wikipedia.org/wiki/Z_Shell[zsh]withhttps://github.com/ohmyzsh/ohmyzsh/wiki[oh-my-zsh], it turns out that the order`locate`is not accessible from`zsh`but quite possible since`bash`, to remedy this, the argument`-c`of the order`bash`allows you to run a command`bash`from another type of terminal.
Here I am looking for the files that end with the pattern`001.pdf`in the user directory
bash -c "locate ~/*001.pdf"
Online Ordering Manual`locate`(The text to translate is empty).https://man.archlinux.org/man/locate.1.fr/[man locate,windows=read-later]
Using the grep command to search for text in files
If you want to search for a specific text within a file, you can use`grep`For example, to search for the occurrence of the word "Ubuntu" in all files`.txt`from the current directory, use:
grep "Ubuntu" *.txt
Conclusion
Searching for files under Ubuntu via a Zsh terminal can be done using several powerful tools.`find`is flexible and offers a multitude of options, while`locate`allows for a quick search using a pre-built database. Finally,`grep`allows you to find text occurrences within files.
These commands offer you a complete range of tools to make your file searches more efficient and tailored to your needs, and each of these commands has a manual accessible via the command.man