3 Easy Ways to Find Files in Linux

3 ways to find files in linux

As you become more experienced with the command line in Linux, more and more questions arise. One of them is how to find files in Linux easily and effectively. In this article, I will list 3 common ways to search for files in the Linux command line.

1. Filter files by name in a directory using grep

This is the easiest way to find files (and folders) on Linux systems. grep is a program that is shipped with all Linux and FreeBSD systems (and even macOS). grep is used for filtering data using regular expressions. While data can be anything from the contents of the file to output from ping, I will show you how to find files and folders in one specific folder using grep.

Suppose that you want to list all storage devices connected to your computer. If you go to the /dev folder and ls in it, you will get quite a lot of devices:

list files on linux terminal

But we are specifically looking for hard drives. Since they start with sd, we can pipe the ls output to grep and filter:

find files on linux using grep

If you are looking for SSD devices, you can filter with nvme:

find files on linux using grep

If you are not familiar with the | symbol, do not panic. It is called the pipe operator, and all it does is take the output of the first command and pass it to the second command.

So, now you know how to use grep to find files. But it does not end here! You can also use grep to find content in a file. For example, you can cat a file and pipe it to grep:

find logs on linux using grep

Here I just showed you the basics of grep and how to use it to find files on Linux. To learn more about this awesome tool, run man grep.

2. Find files using the find command

The most robust command to find files on a Linux system is the find command. It has lots of features, but I will explore the most popular use cases here. If you want to learn more, run man find.

1. Find a file by name: this one is probably the most used. To use it, run find <path> -name <filename>. For example:

find files on linux using file

2. Find by type: sometimes it is convenient to only find files of a specific type. You can use the -type argument for this. It requires a file type which is one of these:
f – regular file
d – directory
l – symbolic link
c – character devices
b – block devices
For example, to find all block devices on a system you would run find / -type b. The -name argument, of course, can work together with -type.

3. Find by size: if you are running low on space, you can specifically look for large files on your system. You can use the -size argument to do this. It expects a file size, prefixed with + (to find files smaller use -) and ending with one of c (bytes), k, M, G. For example, to find all files larger than 2 gigabytes, run find / -size +2G.

3. Find executable files

When you enter a command in the shell, like python, the shell looks up a list of known folders with binary files and executes it. Is there a way to get the exact path of an executable? Of course there it.

The command which will return the absolute path of an executable. For example, here is the path to the Python instance on my PC:

find executable using which

If you run python, it will actually execute /home/mk/anaconda3/bin/python. By default, which will return only one primary result. If you want to view the rest of them, use the -a argument:

find executable using which

Closing notes

Thank you for reading, I hope now you have an idea how to find files on a Linux system. Let me know in the comments about your favorite tips on how to search for files on Linux!

Resources

Get new content delivered to your mailbox:

leave a comment