Find the various ways with command examples to search and find files directly from the Ubuntu command terminal.
If you are on the Graphical desktop of Ubuntu then finding a file using its File Manager is not a difficult task but how to do the same on command line servers where CLI is the only way. For that Ubuntu offers an inbuilt command line tool called “find“, apart from that other open-source tools are available as well such as Locate, here we discuss that as well.
These command-like tools allow users to search for files efficiently. We can use them to search for specific files based on certain criteria such as name or file extension.
So, let’s see a few commands that we can use to find our files in the Ubuntu command terminal.
1: Seach files using the ‘find’ Command
The ‘find’ command is by default available on all Ubuntu Linux systems to search for files directly in the terminal. We can even define different criteria such as name, type, size, and modification time.
To use it either switch to the directory where the file you want to search is located or declare the path where you want the “find” command to search for it.
To search in the current directory
find -name you-file-name
Whereas, to search without switching to any folder we have to declare the path of the directory where the files are located in the command.
find /path/to/search -name "your-filename"
Replace “/path/to/search” with the directory where you want to start the search, and “your-filename” with the name or pattern, or extension of the file you’re searching for.
For example, if you want to search for a file in the terminal let’s say “example.txt” in the current directory and its subdirectories, use:
find -name "example.txt"
Whereas, if you don’t know the exact file name but are aware of the extension it is using then we can list all such files using the wildcard:
Example: It will list all files of the directory where you are searching with .txt extension.
find -name "*.txt"
2: Using the ‘find’ Command with Additional Options
To get more out of the ‘find‘ command we can use additional options to refine our search results in the Ubuntu terminal. Here are a few commonly used ones:
Replace /path/to/search with the directory path where the files are located or remove it if you are in the same directory where the file is available.
Searching by file type:
find /path/to/search -type file_type
Replace /path/to/search with the directory where you want to begin the search. The file_type can be one of the following:
f: Regular files
l: Symbolic links
b: Block devices
c: Character devices
p: Named pipes (FIFOs)
Searching for Regular Files:
To search for regular files (excluding directories, symbolic links, etc.) within the current directory and its subdirectories, use the following command:
find -type f
Searching for Directories:
If you’re interested in finding directories only in your current location, you can use the following command:
find -type d
Searching for Symbolic Links:
To search for symbolic links within a specific directory, use the following command:
find /usr/local -type l
This command will search for symbolic links within the /usr/local directory.
Searching for Block Devices:
If you want to find block devices, such as hard drives or partitions, you can use the following command:
find /dev -type b
This command will search for block devices within the /dev directory.
Searching for Character Devices:
To search for character devices, such as terminals or serial ports, use the following command:
find /dev -type c
This command will search for character devices within the /dev directory.
Searching for Sockets:
If you’re interested in finding sockets, which are used for interprocess communication, use the following command:
find /var/run -type s
This command will search for sockets within the /var/run directory.
Searching for Named Pipes (FIFOs):
To search for named pipes or FIFOs, which are special files for interprocess communication, use the following command:
find /tmp -type p
This command will search for named pipes within the /tmp directory.
Searching by file size (e.g., files larger than 1MB):
find /path/to/search -size +1M
Searching by modification time (e.g., files modified within the last 7 days):
find /path/to/search -mtime -7
3: Search File with ‘locate’ Command
The ‘locate‘ command is not available to use in Ubuntu by default but is obtainable through the system repository to install. It uses a pre-built database of file names available on the system to search for the one we need. Hence, faster than the “Find” command but users has to update its database from time to time.
First, let’s install the Locate tool
sudo apt install locate
Now, update its database using the given command:
After updating the database, we can search for the file or files using the locate command, here is how?
Replace “myfile” with the name or pattern you want to search.
If you want to search all files by ignoring case sensitivity use the -i option, here is the example:
locate -i Myfile
For locating multiple files add their names of them in front of Locate command, for example:
locate –i mfile.txt myfile.iso demo.txt
4: Using the ‘grep’ Command
The ‘grep’ command is primarily used for searching patterns within file contents but can also be used to search for files based on specific text patterns in their names.
To search for files using ‘grep‘, open a terminal and use the following command:
ls -R | grep "pattern"
Replace “pattern” with the text pattern you’re searching for. The ‘ls -R’ command lists all files recursively, and ‘grep’ filter the output based on the specified pattern.
Example: To find all the files in your current directory ended up with the ‘.txt’ extension.
ls -R | grep ".txt"
So, these were a few basic commands tools that we can use on Ubuntu to search files using the Terminal. It will enhance productivity because you don’t have to go through a long list of files to find the one you are looking for. By utilizing commands such as ‘find’, ‘locate’, and ‘grep’, you can search for files based on different criteria, including name, type, size, and modification time.