Linux which files in directory

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.

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).
Читайте также:  Установка сервера alt linux

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:

/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.

Читайте также:  Удаление пользователя linux ubuntu терминал

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
Читайте также:  Linux delete all directories in directory

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.

Источник

Recursively list all files in a directory including files in symlink directories

Suppose I have a directory /dir inside which there are 3 symlinks to other directories /dir/dir11 , /dir/dir12 , and /dir/dir13 . I want to list all the files in dir including the ones in dir11 , dir12 and dir13 . To be more generic, I want to list all files including the ones in the directories which are symlinks. find . , ls -R , etc stop at the symlink without navigating into them to list further.

8 Answers 8

The -L option to ls will accomplish what you want. It dereferences symbolic links.

You can also accomplish this with

The -follow option directs find to follow symbolic links to directories.

as -follow has been deprecated.

@pjz: is there a cross-reference for ‘-follow deprecated; use -L’? Somewhat to my considerable surprise, I did find ‘-L’ and ‘-H’ listed in the POSIX / SUS standard at opengroup.org/onlinepubs/009695399/toc.htm, and even more to my surprise no ‘-follow’, so I answered my own question.

this didn’t work for me. first nothing happened then i tried with -follow — and it said it couln’t find the folder ollow

Your answer is good, but I want to follow only file paths(full path to file) and not directory path, how can I do it ?

How about tree? tree -l will follow symlinks.

Disclaimer: I wrote this package.

It is a nice interface, but sometimes tree is not available and you cannot add it to the system you are trying to explore.

find /dir -type f -follow -print 

-type f means it will display real files (not symlinks)

-follow means it will follow your directory symlinks

-print will cause it to display the filenames.

If you want a ls type display, you can do the following

find /dir -type f -follow -print|xargs ls -l 
 -L, --dereference when showing file information for a symbolic link, show informa‐ tion for the file the link references rather than for the link itself 

If you find you want to only follow a few symbolic links (like maybe just the toplevel ones you mentioned), you should look at the -H option, which only follows symlinks that you pass to it on the commandline.

I knew tree was an appropriate, but I didn’t have tree installed. So, I got a pretty close alternate here

find -L /var/www/ -type l # man find 
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the 

properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.

Источник

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