×

注意!页面内容来自https://linuxconfig.org/how-to-find-file-in-linux,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

How to find file in Linux

If you need to search for one or more particular filesLinux systems have a few powerful methods for locating themsuch as the find and locate commands. Searching for a file with a specific name can be donebut you can also search for files that follow certain naming patterns. This can be broadened all the way to finding files based on file sizefile extensionor a lot of other options.

It’s also possible to find a particular directory or search for files based on their contentssuch as finding all files containing a specific textbut we cover those topics in separate guides.

In this tutorialyou’ll learn how to find a file in Linux by using the command line and GUI. Let’s get started.

In this tutorial you will learn:

  • How to find a file in Linux via command line
  • How to find a file in Linux via GUI

How to find file in Linux

How to find file in Linux

Software Requirements and Linux Command Line Conventions
Category RequirementsConventions or Software Version Used
System Any Linux disto
Software findlocateGUI file explorer
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Find a file with via command line

Most of this section will revolve around the find command. When it comes to finding a particular file or set of filesthe find command is your best friend on Linux. So all you really need to know is how to use the command effectively. Through the following examples and explanationsyou’ll learn how to use it.

Search your present working directory and its subdirectories for a particular file:

$ find . -name "example.txt"

Find all .png image files in the /home directory and its subdirectories:

$ find /home -name "*.avif"

Consider using the type -f option to only search for files (ignore directories)and the -iname option to make your search case insensitive:

$ find /home -type f -iname "example.txt"


Find all .conf files that have been modified in the last seven daysare owned by user linuxconfigand exist in that user’s home directory:

$ find /home/linuxconfig -type f -user linuxconfig -mtime -7 -name "*.conf"

If you don’t want the find command to traverse too deeply into subdirectoriesyou can specify a limit with the -maxdepth option. For examplethis command will limit find to a depth of two subdirectories:

$ find . -type f -maxdepth 2 -name "example.txt"

The find command can automatically delete files it finds if you specify the -delete option. Be very careful with this optionand be sure to first run the find command without it so you know exactly what it plans to delete.

$ find . -type f -name "*.tmp" -delete

The find command’s functionality can be further extended with the -exec option. Using this option allows you to execute a command on every file that find finds. For examplelet’s change the file permissions to 750 for every file found:

$ find . -type f -name "*.conf" -exec chmod 750 '{}' \; -print

In the above command'{}' is a placeholder for the files that are found with find. The -exec option is terminated with a semicolonwhich must be escapedhence the \;. The -print option will output all the file names and paths to your terminal.

Finding a file on Linux

Finding a file on Linux

The locate command

The locate command works similarly to findbut it’s not installed by default on every Linux distro. It searches the file system and stores a list of file names and locations inside of a database. Then it queries this database whenever you search for a file.



This results in locate being a lot faster than find. Howeverlocate‘s database is only refreshed dailyso you should only use it to find files that are a couple days old. Stick to find when searching for recent files or when you want to specify the handy options we’ve shown you above. The locate command is much more limited as far as options go.

The command syntax is very simple. Just specify the file you’d like to find.

$ locate example.txt

To update the cache for locateyou can run:

$ sudo updatedb

Find a file with GUI

All GUIs look a little different but they all surely have a file explorer. We’re using GNOME on our test machinebut searching for files on any GUI is pretty much the sameregardless of the desktop environment you’re using.

On GNOMEwe just need to open the file browserclick the magnifying glass (maybe it says ‘search’ on your GUI)and type the name of the file we wish to find.

Finding a file via file explorer GUI

Finding a file via file explorer GUI

Conclusion

Finding a file on Linux is very easybut it’s surprising how complex the search query can be when you need to find something very specific. The find and locate commandsas well as the GUI methodare all easy to learn when performing a basic search. Once you get comfortable with some of the find command’s more complex capabilitiesit becomes an extremely viable tool for finding and manipulating files in one go.



Comments and Discussions
Linux Forum