- How can I find a specific file from a Linux terminal?
- 6 Answers 6
- How to Search Files Effectively in the Linux Terminal
- What is the find Command in Linux?
- How to Search a File by Name
- How to Find a Directory in Linux
- How to Search a File by Size in Linux
- How to Search a File Based on Time Modified
- How to Execute a Command on Files Filtered with a Confirmation
- How to Find a File with Detailed Information
- How to Find and Remove Files
- Conclusion
How can I find a specific file from a Linux terminal?
I am trying to find where index.html is located on my linux server, and was wondering if there was a command to do that. Very new to linux and appreciate any help I can get.
Searching from / would take a few days to complete. And spit out a bunch of (non-critical) errors for certain types of files while searching. Not the most efficient.
Several years later, Googling «find file in linux» gives this as a top 10 result. I’m sure glad the question was asked and answered.
6 Answers 6
Find from root path find / -name «index.html»
Find from current path find . -name «index.html»
The below line of code would do it for you.
However, on most Linux servers, your files will be located in /var/www or in your user directory folder /home/(user) depending on how you have it set up. If you’re using a control panel, most likely it’ll be under your user folder.
Ah, liked stated below, the find program might take a while to complete. I’m not aware of any other option.
update db locate index.html
Replace /var with your best guess as to the directory it is in but avoid starting from /
All other answers suggest using find (well, this answer also suggests using find but only after locate ), however, after several months of using find I realise locate is more convenient indeed! By the way, what is update db ? Do I need it?
@shintaroid locate does not actually search on your files system for a given file. What it does is it caches the information about the files into a database which is refreshed by a job on a given time interval. So what updatedb does is just to update the database manually.
Solution: Use unix command find
The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the ‘primaries’ and ‘operands’) in terms of each file in the tree.
- You can make the find action be more efficient and smart by controlling it with regular expressions queries, file types, size thresholds, depths dimensions in subtree, groups, ownership, timestamps , modification/creation date and more.
- In addition you can use operators and combine find requests such as or/not/and etc.
The Traditional Formula would be :
Easy Examples
1.Find by Name — Find all package.json from my current location subtree hierarchy.
2.Find by Name and Type — find all node_modules directories from ALL file system (starting from root hierarchy )
sudo find / -name "node_modules" -type d
Complex Examples:
More Useful examples which can demonstrate the power of flag options and operators:
3.Regex and File Type — Find all javascript controllers variation names (using regex) javascript Files only in my app location.
find /user/dev/app -name "*contoller-*\.js" -type f
-type f means file -name related to regular expression to any variation of controller string and dash with .js at the end
4.Depth — Find all routes patterns directories in app directory no more than 3 dimensions ( app/../../.. only and no more deeper)
find app -name "*route*" -type d -maxdepth 3
-type d means directory -name related to regular expression to any variation of route string -maxdepth making the finder focusing on 3 subtree depth and no more /depth1/depth2/depth3 )
5.File Size , Ownership and OR Operator — Find all files with names ‘sample’ or ‘test’ under ownership of root user that greater than 1 Mega and less than 5 Mega.
find . \( -name "test" -or -name "sample" \) -user root -size +1M -size -5M
-size threshold representing the range between more than (+) and less than (-) -user representing the file owner -or operator filters query for both regex matches
6.Empty Files — find all empty directories in file system
7.Time Access, Modification and creation of files — find all files that were created/modified/access in directory in 10 days
# creation (c) find /test -name "*.groovy" -ctime -10d # modification (m) find /test -name "*.java" -mtime -10d # access (a) find /test -name "*.js" -atime -10d
8.Modification Size Filter — find all files that were modified exactly between a week ago to 3 weeks ago and less than 500kb and present their sizes as a list
find /test -name "*.java" -mtime -3w -mtime +1w -size -500k | xargs du -h
How to Search Files Effectively in the Linux Terminal
Arunachalam B
Have you ever felt frustrated searching for files manually on your computer? If you’re a developer or DevOps engineer working on GUI-less Linux servers, it’ll be hard navigating back and forth to find files.
Many people are unaware of the power of Linux terminals. Linux has an incredibly powerful command line that allows you to search files and directories in a fraction of a second.
Whether you’re a beginner or an expert, and if you’re looking to take your file management skills to the next level, you’ve arrived at the right spot. This article will help you understand the basics of the most commonly used find command in Linux.
What is the find Command in Linux?
The find command allows you to search for files and directories on your computer. It adds the flexibility to search for files in a specific directory or recursively through all sub-directories.
Let’s explore the power of the find command
How to Search a File by Name
Let’s say you saved a file called hello_world.html somewhere and you don’t even remember the directory name. But your boss is asking you to send them the file immediately.
Usually, if you forgot where you stored a file, you’d begin by going through folder after folder and checking if the file exists.
This is when the find command does a great job. Instead of searching the file manually on your computer, you can use the find command to automate the process.
By passing the name of the file using the -name flag, the find command searches and returns the location of the file.
But remember the -name flag performs a case-sensitive search. If you are looking to do a case-insensitive search, you can use the -iname flag instead.
You can also use the find command as an alternative to the ls command in some places. Let’s assume you need to find all the files ending with the .txt extension. You can do so with the find command using the regex pattern ( *.txt ).
This command will list all the .txt files in your current directory and its sub-directories.
To find .txt files in a particular directory and sub-directory, you can replace the /path/to/search with the path of your directory.
How to Find a Directory in Linux
Searching for a directory is possible by passing the d to the -type parameter in the find command.
In the above screenshot, we’re finding a directory named zip from our current directory.
Similarly the -type option accepts other parameter options to simplify our finding process.
- f finds the list of regular files
- b finds the list of block devices
- c finds the list of character devices
- l finds the list of symbolic links
- s finds the list of sockets
- p finds the named pipes
How to Search a File by Size in Linux
Adding the -size option along with the find command helps you find files based on size. Prepend a + or — to the size to represent greater than and less than, respectively.
In the above screenshot, we’re finding all the files that have a size greater than 1 GB.
You can also search for all files that fall within a specific size range.
For example, if you want to find all the files that are above 50 MB and below 100 MB, you can run the following command:
You can specify the size in your preferred notation. A few of the available notations are:
- K represents KB
- M represents MB
- G represents GB
- b represents bytes
- c represents blocks
How to Search a File Based on Time Modified
Every file has a created and last updated time associated with it. Let’s assume you have thousands of files in your directory. You edited a file in the past couple of days and forgot its name. You’re sure that you have edited only a couple of files after that.
In such cases, you can find all the files that were modified within the past 7 days. This limits your search from 1000+ files to a more manageable amount. You’ll be able to find the file you edited in seconds after running the command.
This is possible by passing -mtime parameter with the find command.
Let’s assume another scenario, where today’s date is February 10, 2023. You modified a file before Feb 3, 2023. After Feb 3, 2023, you modified a lot of files. You have to find the file which you modified before Feb 3, 2023. So, basically, you need the files that were modified before Feb 3, 2023.
But, you can also run this query using the find command. You can achieve this by exchanging the negative sign (-) with the positive sign (+).
Here’s the modified command for you:
This command searches for all files ending with a .zip in the ./5minslearn directory and then moves each file to the ./5minslearn/zip directory.
The -exec option allows you to perform a wide range of operations on the files that are found. You can replace the move command from the above example by copying, deleting, or even changing the file permission command.
How to Execute a Command on Files Filtered with a Confirmation
Most people will prefer to use this if they’re not sure about whether to apply the operation on each file.
The -ok option is similar to the -exec option except that it will ask for confirmation before executing the operation on each file. This command is super helpful to review files that will be affected before executing the specific operation. You also have the option to decline if you’re not sure or don’t wish to apply the command.
For example, this time let’s try to move the .txt files to the other directory.
The above command searches for all files with a .txt extension in the ./5minslearn directory and then prompts the user to confirm before moving each file to the ./5minslearn/text_files directory.
To approve the operation, enter yes and no to decline the operation and skip to the next file.
The -ok option is useful when you want to be cautious about the files you are modifying, as it allows you to inspect each file and its location before executing the specified command.
How to Find a File with Detailed Information
The -ls option in the find command is used to display information about the files found in the search, in the format of the ls command. This option provides detailed information about the files, such as their permissions, owner, size, and last modified time.
How to Find and Remove Files
Have you ever needed to find files and remove them from your computer? The -delete option in the find command does this for you. It allows you to delete files that match the specified criteria.
In the above example, you can see that the find command deleted the files with the .html extension
Note: This operation is irreversible. Be 100% sure when you run the delete operation.
I would advise running the find command without the -delete flag at first and ensure that only the files that need to be deleted are shown. Once you’re sure, you can execute the same command appending -delete flag.
Conclusion
In this article, you learned how to search files effectively using your Linux terminal.
These are very basic options in the find command that I think every developer should know. I believe mastering the fundamentals is the first step to becoming more advanced with Linux. I’ve been covering basics in all my blogs to help you create a solid foundation.
To learn more about Linux, subscribe to my email newsletter on my site and follow me on social media.