Linux search file in system

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:

Читайте также:  Linux raid controller drivers

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 

Источник

Classic SysAdmin: How to Search for Files from the Linux Command Line

This is a classic article written by Jack Wallen from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.

It goes without saying that every good Linux desktop environment offers the ability to search your file system for files and folders. If your default desktop doesn’t — because this is Linux — you can always install an app to make searching your directory hierarchy a breeze.

But what about the command line? If you happen to frequently work in the command line or you administer GUI-less Linux servers, where do you turn when you need to locate a file? Fortunately, Linux has exactly what you need to locate the files in question, built right into the system.

The command in question is find. To make the understanding of this command even more enticing, once you know it, you can start working it into your Bash scripts. That’s not only convenience, that’s power.

Читайте также:  Del all files linux

Let’s get up to speed with the find command so you can take control of locating files on your Linux servers and desktops, without the need of a GUI.

How to use the find command

When I first glimpsed Linux, back in 1997, I didn’t quite understand how the find command worked; therefore, it never seemed to function as I expected. It seemed simple; issue the command find FILENAME (where FILENAME is the name of the file) and the command was supposed to locate the file and report back. Little did I know there was more to the command than that. Much more.

If you issue the command man find, you’ll see the syntax of the find command is:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point. ] [expression]

Naturally, if you’re unfamiliar with how man works, you might be confused about or overwhelmed by that syntax. For ease of understanding, let’s simplify that. The most basic syntax of a basic find command would look like this:

find /path option filename

Find by name

Let’s break down that basic command to make it as clear as possible. The most simplistic structure of the find command should include a path for the file, an option, and the filename itself. You may be thinking, “If I know the path to the file, I’d already know where to find it!”. Well, the path for the file could be the root of your drive; so / would be a legitimate path. Entering that as your path would take find longer to process — because it has to start from scratch — but if you have no idea where the file is, you can start from there. In the name of efficiency, it is always best to have at least an idea where to start searching.

The next bit of the command is the option. As with most Linux commands, you have a number of available options. However, we are starting from the beginning, so let’s make it easy. Because we are attempting to find a file by name, we’ll use one of two options:

Remember, Linux is very particular about case, so if you’re looking for a file named Linux.odt, the following command will return no results.

If, however, you were to alter the command by using the -iname option, the find command would locate your file, regardless of case. So the new command looks like:

Find by type

What if you’re not so concerned with locating a file by name but would rather locate all files of a certain type? Some of the more common file descriptors are:

  • f – regular file
  • d – directory
  • l – symbolic link
  • c – character devices
  • b – block devices
Читайте также:  Отключить поддержку шифров 3des linux

Now, suppose you want to locate all block devices (a file that refers to a device) on your system. With the help of the -type option, we can do that like so:

The above command would result in quite a lot of output (much of it indicating permission denied), but would include output similar to:

/dev/hidraw6 /dev/hidraw5 /dev/vboxnetctl /dev/vboxdrvu /dev/vboxdrv /dev/dmmidi2 /dev/midi2 /dev/kvm

We can use the same option to help us look for configuration files. Say, for instance, you want to locate all regular files that end in the .conf extension. This command would look something like:

The above command would traverse the entire directory structure to locate all regular files ending in .conf. If you know most of your configuration files are housed in /etc, you could specify that like so:

find /etc -type f -name “*.conf”

The above command would list all of your .conf files from /etc (Figure 1).

Outputting results to a file

One really handy trick is to output the results of the search into a file. When you know the output might be extensive, or if you want to comb through the results later, this can be incredibly helpful. For this, we’ll use the same example as above and pipe the results into a file called conf_search. This new command would look like: ​

find /etc -type f -name “*.conf” > conf_search

You will now have a file (conf_search) that contains all of the results from the find command issued.

Finding files by size

Now we get to a moment where the find command becomes incredibly helpful. I’ve had instances where desktops or servers have found their drives mysteriously filled. To quickly make space (or help locate the problem), you can use the find command to locate files of a certain size. Say, for instance, you want to go large and locate files that are over 1000MB. The find command can be issued, with the help of the -size option, like so:

You might be surprised at how many files turn up. With the output from the command, you can comb through the directory structure and free up space or troubleshoot to find out what is mysteriously filling up your drive.

You can search with the following size descriptions:

  • c – bytes
  • k – Kilobytes
  • M – Megabytes
  • G – Gigabytes
  • b – 512-byte blocks

Keep learning

We’ve only scratched the surface of the find command, but you now have a fundamental understanding of how to locate files on your Linux systems. Make sure to issue the command man find to get a deeper, more complete, knowledge of how to make this powerful tool work for you.

Источник

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