loading...

Linux Mint – Searching for files

install phpMyAdmin On CentOS 8

Now that you’ve had a crash course on how to create and manage files, you probably have quite a few files all over the hard disk that you’ve created. However, what do you do when you want to update a file, but you’ve forgotten where it is? The find command comes to your rescue.

The find command will allow you to search your filesystem for files based on the search criteria. To use it, you type find, a path to start the search from, the search criteria, and then the name of the file. For example, consider the following command line:


find / -name myfile

In the preceding example, we chose to start our search in / (the beginning of the filesystem). We are searching for a specific name ( -name), and the name we’re looking for is myfile. After executing the command, a search will be conducted for the file, and the output returned is the full path of the file once (and if) it’s found.

However, you are more likely to see one or more errors when executing the preceding command. As we started our search in /, the search would have been conducted in directories that a normal user may not have access to. Therefore, we can narrow down the search by starting it further up the filesystem tree using the following command:


find /home/user -name myfile

This is better. The search should be faster as we’re not searching the entire hard disk for the file that we’re only looking for in our home directory.

Note

The find command is also very useful from a system administrator’s standpoint. For example, if you wanted to edit a configuration file in /etc, but you weren’t sure of where exactly it was located, you can search for the file. If you were looking for the smb.conf file, the find command would find it under /etc/samba/smb.conf. You would then know where the file that you’d like to edit is.

Sometimes, you may not know the actual filename you’re trying to find. Perhaps you were working on a file during the last week, but you don’t remember what you named it or where you saved it. The find command can still save the day. Instead of passing the -name option, you can pass -mtime (stands for modified time) instead. Let’s take a look at the following example:


find /home/user -mtime -7

In the preceding example, we’re looking for all files contained in /home/user that were modified seven or less days ago. Unfortunately, if you use a web browser or similar software which saves its local configuration in your home directory, this command will likely display a large list of junk files. If you also know the file extension you used, you can narrow the results down even further.


find /home/user -mtime -7 -name *.txt

In this example, we used the same find command with –mtime, as used in the preceding command, but we also appended –name, as done in our first example. The *.txt portion returns all files that end with the .txt file extension. After you put it all together, you’re essentially searching for all files in your home directory that were modified seven or less than seven days ago and have a filename ending in .txt.

There are other variations of the find command. Check out the main page for the find command and experiment with finding other files in your filesystem.

Comments are closed.

loading...