How to Find File In Linux Ubuntu?

By Hardik Savani May 1, 2024 Category : Ubuntu

Hello,

Today, I would like to show you how to find file in linux terminal. We will look at an example of how to find file in ubuntu terminal. This example will help you how to find a file in ubuntu 22.04 command line. you will learn ubuntu find file by name command line. So, let's follow a few steps to create an example of ubuntu find file command line.

To find files in Linux Ubuntu from the Terminal, you can use the `find` command. It's a powerful command-line tool that allows you to search for files and directories based on various criteria. Here's how to use it step by step:

Step 1: Open the Terminal

Press `Ctrl + Alt + T` on your keyboard to open the Terminal if it's not already open.

Step 2: Basic Syntax

The basic syntax for using the `find` command is as follows:

find [search_directory] [options] [expression]

- `search_directory`: The directory where you want to start the search. If you want to search the entire filesystem, you can use `/` as the search directory.

- `options`: Various options to specify the search criteria.

- `expression`: The search expression that defines the criteria for finding files.

Step 3: Find Files by Name

To find files by name, you can use the `-name` option followed by the file name you want to search for. For example, to find all files named "example.txt" within the current directory and its subdirectories, you can use:

find . -name "example.txt"

Replace "example.txt" with the actual filename you're looking for.

Step 4: Find Files by Extension

To find files by their extension (e.g., all `.jpg` files), you can use the `-name` option with a wildcard `*` to match files with a specific extension. For example, to find all `.jpg` files within the current directory and its subdirectories, you can use:

find . -name "*.jpg"

Step 5: Find Files by Type

You can also search for files based on their type (e.g., regular files, directories, symbolic links) using the `-type` option. For instance, to find all directories within the current directory and its subdirectories, you can use:

find . -type d

Here, `-type d` specifies directories. You can use `f` for regular files or `l` for symbolic links.

Step 6: Find Files Modified in a Certain Time Frame

You can search for files modified within a specific time frame using the `-mtime` option. For example, to find files modified within the last 7 days, you can use:

find . -mtime -7

This will find files modified within the last 7 days.

Step 7: Combining Multiple Criteria

You can combine multiple search criteria using logical operators like `-and`, `-or`, and `-not`. For example, to find all `.txt` files modified within the last 7 days in the home directory, you can use:

find ~ -name "*.txt" -mtime -7

Step 8: Execute an Action on Found Files (Optional)

You can also perform actions on the found files, such as deleting them or executing a command. For example, to delete all `.tmp` files in the current directory and its subdirectories, you can use:

find . -name "*.tmp" -exec rm {} \;

This command will execute the `rm` (remove) command on each found file.

Remember to use the `find` command with caution, especially when using actions like deletion, as it can't be undone. Always double-check your search criteria and review the list of found files before executing any actions.

Tags :
Shares