Search linux for file name

6 ways to Find Files By Name in Linux

The best way to find files by name in Linux is using the find command with the “-name” option.

This command will search through the directories for files that have the specific word in their name.

This can be very useful when you need to find a specific file and don’t know where it is located.

In this blog post, we will discuss 6 ways to use the find command to search for files by name.

We will also show you how to pipe the output of the find command so that you can easily find the information that you need. Let’s get started!

The following Linux commands can be used to search files by name.

  • find /path -name *.txt
  • find /path -type f -name test.txt
  • find /path -name failed*.* -type f
  • find /path -type f -not -name “*.html”
  • find / -name “file.txt” -size +4M
  • find /dev/ -type b -name “sda*”

If you’re new to the world of Linux, you can refer to “Mastering the Linux Command: A Beginner’s Guide.” This comprehensive article serves as your gateway to gaining essential knowledge and honing the necessary skills for the Linux command line interface (CLI) with confidence.

Find command in Linux

Linux find command is a powerful tool that can be used to locate and manage files and directories based on a wide range of search criteria. This post will cover how to find file by name in Linux.

When using find, we would follow the syntax below.

  • path: This is the directory we want to search.
  • expression: This is where we place our search criteria for what we want to find whether by name, or file size etc.

To find files with a specific name in Linux, you can use the find command with the -name option. Here’s the basic syntax:

Where path is the directory to search, and filename is the name of the file you want to find.

Understanding -name option in find command

The -name option in the find command is used to search for files or directories based on their names. It allows you to specify a pattern or exact name to match when searching for files.

Читайте также:  Linux get block size

The syntax for using the -name option in the find command is as follows:

  • [path]: Specifies the starting directory for the search. It can be an absolute path or a relative path. If not provided, the search starts from the current directory.
  • -name “pattern”: Specifies the pattern to match the file or directory names. The pattern can be a complete filename or include wildcard characters for partial matching. Common wildcard characters are * (matches any sequence of characters) and ? (matches any single character).

Here are a few examples to illustrate the usage:

  • -name myfile.txt: Select files or directories with the exact name “myfile.txt”.
  • -name “*.txt”: Select files or directories with names ending in “.txt”.
  • -name “file*”: Select files or directories with names starting with “file”.
  • -iname “NAME*”: Select files or directories with names starting with “name” or “NAME” (case-insensitive).

You can combine the -name option with other find options, such as specifying the starting directory, specifying the type of files to search (-type f for regular files), and using the -exec option to perform actions on the matched files or directories.

Find All Files With A specific Name in Linux

We can use the find command to search for all files with a specific name. In this example, we will search for all files with the name “test.txt”.

To do this, we will use the following command:

This command will search through all of the directories on your system for a file named “test.txt“. The output of this command will look something like this:

As you can see, this command was able to find two files with the name “test.txt”. One file was in the user’s home directory and the other file was in the /tmp directory.

To list all files in the current directory, we can use the following command. ./ means current directory.

If we need to list the file under the current directory and its name is ‘test’, we can use this command.

Absolute path is a complete path from the start of the actual filesystem from / directory.

find /opt -name .profile -print

This command searches the /opt directory and prints the complete path names of all files named .profile.

The /opt (slash) instructs the find command to search the /opt directory and all of its subdirectories.

In order not to waste time, it is best to limit the search by specifying the directories where we think the files might be.

Check this post to learn more about file path.

Find Files with a name pattern in Linux

We can use basic shell wildcard characters to broaden our search. For instance, the asterisk (*) represents any number of characters:

Читайте также:  Vnc server linux gnome

/home/tux/Documents/examples/foo
/home/tux/Documents/examples/Foo
/home/tux/Documents/examples/foo.xml
/home/tux/Documents/examples/foobar.txt

A question mark (?) represents a single character:

This isn’t regular expression syntax, so the dot (.) represents a literal dot in this example.

If there is more than one file which name has ‘test’ in it like test1, test2, we can use this command.

If we want to search and list all files with a given name in multiple directories, we can either start the search at root directory, or if we know the directories, we can specify them.

find ./test ./logs -name failed*.* -type f

Sample output:
./test/failed_tests.txt
./logs/failed_tests.log

*
An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.txt) or to name many files at once (as in ae*).

?
A question mark is replaced by any single character (so h?p matches hop and hip, but not help).

For example, if you want to find all of the files that have the word “file” in their name, you can run the following command:

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name.

If you want to find all of the files that have the word “file” at the beginning of their name, you can use the following command:

This command will search through the current directory and all of its subdirectories for files that have the word “file” at the beginning of their name.

Find Multiple Files by name in Linux

Here is a little complex example. This command will remove all files named a.out or *.o that are not accessed for a week and that are not mounted by using nfs.

find / \( -name a.out -o -name ‘*.o’ \) -atime +7 ! -fstype nfs -exec rm <> \;

Note: The number that is used within the -atime expression is +7. It is the correct entry if we want the command to act on files that are not accessed for more than a week (seven 24-hour periods).

Find Files not matching a name pattern in Linux

This Linux find command using the “not” operator creates a list of all files not ending with the .html file extension (filename pattern).

find . -type f -not -name «*.html»

We can also use the following command to get this. find . -type f ! -name “*.html”

Читайте также:  Линукс руководство системного администратора

Excluding Files and Directories in Find command

You can use the “find” command with the “-exclude” option to exclude certain files and directories from your search.

For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the PDF files, you can run the following command:

find . -name ‘file*’ -exclude *.pdf

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the PDF files.

You can also use the “-exclude-dir” option to exclude certain directories from your search. For example, if you want to find all of the files that have the word “file” in their name, but you want to exclude all of the files in the “tmp” directory, you can run the following command:

«find . -name ‘file*’ -exclude-dir tmp

This command will search through the current directory and all of its subdirectories for files that have the word “file” in their name, but it will exclude all of the files in the “tmp” directory.

Advanced options in Find command

The find command also allows you to use advanced search options to filter results. For example, you can use the find command with the -type option to search for files of a specific type.

You can use the find command with the -mtime option to search for files that have been modified in a certain amount of time.

You can also use the find command with the –maxdepth option to specify how deep you want to search into directories.

If you want to find all of the PDF files that are older than one week, you can run the following command:

This command will search through the current directory and all of its subdirectories for PDF files that have been modified more than seven days ago.

Find command and other options in Linux

  • find / -name “file.txt” -size +4M
  • find /dev/ -type b -name “sda*”
  • find / -type d -name “a.txt”
  • find /opt -type f -name ‘howtouselinux’ -mtime +1

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

howtouselinux.com is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative.

Источник

Оцените статью
Adblock
detector