Table of Contents
Today, in this article, we show how to find files in linux. Sometimes we miss a file or directory, and looking through the terminal can take up valuable time. The terminal and file manager on a Linux desktop come with a built-in search tool. The locate command is quite useful and simple to use.
Working with test files and folders is a good idea as you get used to these commands and you should take special care to ensure that you follow the instructions. The Linux cd command makes changing directories simple, but only if you know where you are going. This post includes a script to identify and “remember” files or websites you want to return to, as well as some tips to navigate between known destinations.
It doesn’t matter where you are on the file system now, returning to your home directory is one of the simplest things you can do with the cd command. Just type cd yourself to return to your home directory. While adding the tilde won’t get you there any faster, typing cd will accomplish the same task. We have mentioned below the steps to find files on Linux and make them easier to find.
Steps to find files on Linux
Finding a file in Linux
Step 1: create a test folder containing test files. After creating the test directory and files, verify that the files were created using ls.
mkdir testcd testtouch test1.txt test2.h test3.c TEST.fls
Step 2: In the test directory, find the file called test1.txt. Using find with “.” indicates that the search should be limited to the current working directory. After running the find command, you should see test1.txt file listed as a result.
find . -name test1.txt
Searching using a partial filename in Linux
Step 1: In the test directory run the following command looking for files that contain the term “test” in your name.
find . -name “*tes*”
Step 2: Repeat the command looking for a specific file extension. We can use the same method to look for a specific file type. Changing the command to search for “*.TXT*” will return only the .txt file type.
find . -name “*.txt*”
Step 3: Use -iname to return case insensitive results. Here we use the partial search term “*test*” again, but using –Name forces the command to show all results regardless of case. So the results include our file TEST.f .
find . -name “*tes*”
Distinguishing between directories and files in Linux
Step 1: Add a directory inside our test directory called test2. Use ls to confirm the directory has been created.
cd testmkdir test2ls
Step 2: Run a find command which will return the file and directory results. You should see that the result contains all the test files and also the test2 directory.
find . -name “*test*”
Step 3: Add -type f tag to return file results only. Note that in the results the test2 directory is omitted.
find . -name “*test*” -type f
Step 4: Add -type d tag to return directory results only. Note that the only result should now be the test2 directory.
find . -iname “*test*” -type d
Searching the entire file system in Linux
Step 1: look for the test1.txt file from the root (/) of the file system. This step is not very successful and was added to illustrate a common problem.
cdfind / -iname test1.txt
Step 2: Repeat the previous search, but use sudo to add root privileges. This gives the command permission to access most places within the file system, and as such the report returned will be much clearer and easier to read.
sudo find / -iname test1.txt
Final Words
We hope you like our article on how to find files on Linux. The Linux find command will be needed to search your directory or file system. If you use the -exec flag, you can also discover a file on Linux right away and process it without using another command. The find command on Linux can filter objects recursively using a simple conditional approach.