Linux: search for files using the terminal

Every person in his life wants to have time to do as many things as possible. Unfortunately, it is not he who decides. Time is what each of us always lacks. However, people have come up with a huge number of ways to speed up an operation or a certain action.

linux file search

This is not about global, but rather petty, in some ways even necessary. About how Linux searches for files. About how to speed up this process several times and how to automate the search for necessary files to the limit.

Simple file search

Now we will consider the case when on Linux you need to search for files, knowing only some of its properties (name, format or size). Knowledgeable people already realized that for this you need to use the find command.

So, let's say, on our computer, somewhere in the home folder, there is a picture called “Pokemon.jpg”, but we don’t remember exactly where it lies.

In order not to open each folder in turn, you can use the find command, namely register in the terminal:

find / home -name "Pokemon.jpg" -print

The path to this file will immediately appear. But to make it clearer, it's worth explaining everything. The “/ home” section indicates that the file will be searched in the home directory. "-Name" is written if the search is performed by name, then the full file name is indicated in quotation marks. And "-print" at the end gives the command to display the results found on the screen.

linux search text in files

Now let's simulate a situation where you do not know the file format. In this case, the command will look like this:

find / home -name "Pokemon *" -print

In this case, “*” is put at the end of the file name. This means that all files with the name "Pokémon" will be located, whether it be a picture, a document or music.

In order to find a file of a specific format, for example, “.jpg”, you must enter:

find / home -name "* .jpg" -print

We think the relationship is clear. And it became clear how Linux searches for files.

Search for text in files

Many people are used to the fact that you can search for a specific word or phrase in the text by opening the text in Word or a similar program and searching there, but on Linux, searching for text in files is much faster and somewhat easier.

First of all, we forget about the “find” command, it will not help us in this case. Now let's get acquainted with the grep command. It is she who will help to find the words that are necessary in the huge wall of the text.

Suppose you have a text document on your computer that contains the phrase “I love Linux” and you know that it is located in the folder: “/ home / user”. So, to find the text we need in this document, you need to enter the command:

grep "I love Linux" /home/user/*.txt

The syntax here is very simple. After grep, the search terms are indicated in quotation marks, and then you need to pave the path to the file where you need to look for them, and at the end specify the file format.

linux search for large files

Search for large files

In Linux, the search for large files is most often carried out in cases where there is already little disk space and urgent need to delete heavy files.

There are two options here to quickly find and delete such files. Dramatically they are no different, and only you decide how to use it.

linux duplicate file search

The first command will allow us to find files larger than the specified:

find / -size + 1G -print

In this case, we see that the size is “+ 1G”, this means that there will be files larger than 1 GB, if there were a minus instead of a plus, this would mean that files were searched for smaller than the specified size. But this method is bad for one reason: if there are a lot of such files, you will be sorting them out for a very long time, so there is another option.

Type in the command line:

find / -mount -type f -ls 2> / dev / null | sort -rnk7 | head -10 | awk '{printf "% 10d MB \ t% s \ n", ($ 7/1024) / 1024, $ NF}'

After that, ten files will appear on the screen, starting with the largest. This will save time on finding the right option for removal.

Before executing these commands, it is recommended to register the “sudo su” command in order to obtain superuser rights, as the search for files will affect root as well.

Search for file copies

It often happens that when you use the computer for a long time, duplicates of already created files appear on it. They arise for many reasons, one of which is negligence. Of course, this does not threaten anything serious, but it still takes up space, therefore, in order to quickly remove all of them, you need to search for duplicate files on Linux .

As last time, there are two ways to do this, but let's focus on one that is simpler.

You can download a special utility by writing in the terminal:

sudo apt-get install fslint

Its difference from other options is that it has a graphical interface, which will make it easier for beginners to work with it.

search string in linux files

By launching it, you can immediately click search, by default it will be carried out in the home directory. If desired, you can select any directory.

Alternate Search Options

Of course, remembering all the above commands is not for everyone, and, frankly, to no avail, and writing on a piece of paper is also not an option. That is why it is necessary to talk about an alternative that is a hundred times simpler, although time consuming.

The first option, if you need to find a file by name, will be a search from Explorer. Just select the folder in which the necessary file is supposedly located, and in the search bar located at the top right, enter its name. You can also enter the file format there.

The second option is suitable if you need to search for strings in Linux files. Download LibreOffice and copy your text there. After that press the key combination Ctrl + F and in the appeared line enter the necessary combination of words.

That's basically it. So you can quickly find what you are looking for without remembering the command.

Conclusion

As you can see, on Linux, you can search for files in various ways. Some of them are better, some are worse, it all depends on the situation. In any case, after reading this article, you can now do it. Using the command line for a while, you will remember most of the commands and then you can speed up the process several times.

Source: https://habr.com/ru/post/C42421/


All Articles