IntroductionOne problem users run into when first learning how to work with Linux is how to find the files they are looking for. Show
This guide will cover how to use the aptly named PrerequisitesTo follow along with this guide, you will need access to a computer running a Linux-based operating system. This can either be a virtual private server which you’ve connected to with SSH or your local machine. Note that this tutorial was validated using a Linux server running Ubuntu 20.04, but the examples given should work on a computer running any version of any Linux distribution. If you plan to use a remote server to follow this guide, we encourage you to first complete our
Initial Server Setup guide. Doing so will set you up with a secure server environment — including a non-root user with Note: To illustrate how the This is to be expected, since you’re searching for files within directories that regular users typically don’t have access to. However, these example commands should still work and be useful for understanding how these programs work. Finding by NameThe most obvious way of searching for files is by their name. To find a file by name with the
This will be case sensitive, meaning a search for To find a file by name but ignore the case of the query, use the
If you want to find all files that don’t adhere to a specific pattern, you can invert the search with
Alternatively, you can invert the search using an exclamation point (
Note that if you use Finding by TypeYou can specify the type of files you want to find with the
Here are some of the descriptors you can use to specify the type of file:
For instance, if you wanted to find all of the character devices on your system, you could issue this command:
This command specifically only searches for devices within the
You can search for all
files that end in
Note: The previous example combines two You can combine expressions like this by separating them with the
This example will find any files whose names match either Filtering by Time and Size
SizeYou can filter files by their size using the
To illustrate, the following command will find every file in the
To find files that are less than 50 bytes, you can use this syntax instead:
To find files in the
TimeFor every file on the system, Linux stores time data about access times, modification times, and change times.
You can base your For example, to find files in the
If you want files that were accessed less than a day ago, you could run this command:
To find files that last had their meta information changed more than 3 days ago, you might execute the following:
These options also have companion parameters you can use to specify minutes instead of days:
This will give the files that have been modified in the last minute.
This syntax will return every file on the system that was created or changed more recently than the reference file. Finding by Owner and PermissionsYou can also search for files by the user or group that owns the file using the
Similarly, you can specify files in the
You can also search for files with specific permissions. If you want to match an exact set of permissions, you use can this syntax specifying the permissions using octal notation:
This will match files with exactly the permissions specified. If you want to specify anything with at least those permissions, you can precede the permissions notation with a minus sign:
This will match any files that have additional permissions. A file with permissions of Filtering by DepthIn this section, you will create an example directory structure that you’ll then use to explore filtering files by their depth within the structure. If you’re following along with the examples in this tutorial, it would be prudent to create these files and directories within the After running the commands in this section, your Create the example directory structure within the
Following that, populate these directories with some sample files using the
With
these files and directories in place, go ahead and navigate into the
To get a baseline understanding of how
This will return a lot of results. If you pipe the output into a counter, you’ll find that there are
This is probably too many results to be useful to you in most circumstances. To narrow it down, you can specify the maximum depth of the search under the top-level search directory:
To find
That is a much more manageable list. You can also specify a minimum directory if you know that all of the files exist past a certain point under the current directory:
You can use this to find only the files at the end of the directory branches:
Again, because of the branching directory structure, this will return a large number of results (1000). You can combine the min and max depth parameters to focus in on a narrow range:
Combining these options like this narrows down the results significantly, with only 110 lines returned instead of the previous 1000. Executing Commands on find ResultsYou can execute an arbitrary helper command on everything that
The For instance, assuming you’re still in the
You could also change the directory permissions in a similar way:
This example finds every directory with permissions set to Finding Files Using locateAn alternative to using You can install the command on Debian or Ubuntu with
On Rocky Linux, CentOS, and other RedHat derived distributions, you can instead use the
The reason
Remember, the
This will match any
files and directories that contain the string
To have
You can retrieve statistics about the information that
This can be useful for getting a high-level understanding of how many files and directories exist on your system. ConclusionBoth the From here, we encourage you to continue experimenting with Which of the following commands will help find you all the files in current directory?The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
Which of the following commands will help find you all the files in current directory its subdirectories their subdirectories and so on that contain string Hide and Seek A?ls: List the Contents of a Directory. Similar to the “dir” command in Windows, “ls”, short for list, shows the contents, files and subdirectories of a directory. This command lists both the files and directories contained in the directory.
What command is used for finding current directory?To determine the exact location of the current directory at a shell prompt and type the command pwd. This example shows that you are in the user sam's directory, which is in the /home/ directory. The command pwd stands for print working directory.
Which command can be used to search in all the files under the current directory and its subdirectory?The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories.
|