What Is the Find Command That Will List All of the Directories That "Other" Can Write to?
The find command allows users to search for files and take deportment on them. Information technology is part of the "findutils" package and comes arranged with all distributions. It is highly flexible, allowing you to look for files and directories based on a variety of conditions. Optionally, it also allows you to take dissimilar types of actions on the results.
In this commodity, nosotros volition understand how to work with the notice command. We will also illustrate its usage through diverse examples throughout this article.
Introduction
The basic structure of the detect command is like so:
observe [paths] [expression] [actions]
The find command takes a number of paths, and searches for files and directories in each path "recursively". Thus, when the discover control encounters a directory inside the given path, information technology looks for other files and directories inside it. Again, if in that location are other directories inside, the find command will look within them equally well. This process continues till information technology has searched for all items within the path you lot specified.
By default, the observe command finds everything within a directory. If y'all desire to filter some of them out based on certain criteria, y'all tin specify an expression to do so.
The default action is to print all the results. However, y'all can also specify a custom activity that the discover command can take on the results.
These concepts will become clearer every bit we go through various examples mentioned afterward in this article.
Finding all files and directories
Imagine that you want to listing all the directories and files for a given path. For example, if you want to listing the contents of the/usr/share
directory, run:
find /usr/share
This will give you a list of files and directories, as shown in the screenshot below. Depending upon the contents of the directory, this listing can exist very large!
If y'all want to list the contents of multiple directories, you can do this similar so:
discover /usr/share /bin /usr/lib
If you want to list the contents of the current working directory, apply a menstruation(.
) as the path:
find .
When in that location are no paths, the discover command assumes that information technology should work with the current directory. So, you can go out the .
and simply use:
observe
Finding items past their name
As nosotros mentioned previously, you tin can filter the results of the find command using expressions. In this section, we will larn how to filter items past their name.
If y'all desire to find a file or directory named NEWS.txt
within the /usr
directory, use the -proper noun
switch like so:
discover /usr -name NEWS.txt
The -proper noun
switch is case sensitive. If you lot don't know the exact case of the particular you're looking for, y'all tin use the -iname
switch which is instance insensitive:
find /usr -iname news.txt
The -name
and -iname
switches too accept "wildcards", which are special characters that human action equally placeholders. There are two wildcards — the ?
character represents a single unknown character, and the *
character represents whatever number of unknown characters (including zero).
If you lot want to use a wildcard grapheme, you should go on the name
/iname
parameter inside single or double quotes. Since the *
and ?
characters are besides special characters for the shell, putting them in quotes ensures that the control works correctly.
As an case, if y'all accept to search for all files and directories inside /usr
that have a .txt
at the stop of their name, yous can utilise:
find /usr -name '*.txt'
If you want to search for files and directories with four letters in their name, run:
find /usr -name '????'
Sometimes, you may want to lucifer the full path to a file/directory, instead of only matching the name. In such a state of affairs you lot can use the -path
switch. As an example, say you want to discover all files and directories with a .txt
at the end of their proper noun, just simply if they are in a directory named src
. Thus, in the full path, in that location will be a /src/
somewhere in the eye, and a .txt
at the terminate. Then, the command will be:
detect /usr -path '*/src/*.txt'
There is besides the -ipath
switch, which is the case insensitive version of -path
.
Searching for files or directories
All the examples we've seen so far return files and directories. Even so, if you need to search for either files or directories only, you tin use the -type
switch. The almost common parameters of -type
are:
-
f
: files -
d
: directories -
l
: symbolic links
For case, to find all the files in the /usr
directory, run:
find /usr -type f
You can also combine the various switches of the discover command. For instance, to find all .txt
files in the /usr
directory, you can apply:
find /usr -type f -proper noun '*.txt'
Similarly, to find directories inside /usr
that begin with "python", use:
find /usr -type d -name 'python*'
Finding empty files
The find command supports the -empty
flag to search for files and directories that are empty. An empty file is ane that has no content in information technology, whereas an empty directory is ane that has no files or subdirectories in it.
For example, if you want to find list empty directories in your home directory, y'all tin run:
find ~ -type d -empty
Negating matches
The find control also allows you to "negate" a match. This is useful when we have a criteria to exclude from our search.
As an example, say you lot desire to observe files in the /usr
directory which don't accept the .txt
extension. You can negate the -name
switch by adding a assertion mark (!
) in front of it, every bit shown:
find /usr -type f ! -name '*.txt'
You tin also invert any other switch. Then, if yous want to notice the non-empty directories in your habitation directory, run:
discover /usr -type f ! -empty
Searching based on ownership
The find command supports searching for files and directories based on their buying information.
To find the files or directories that vest to a detail user, you tin apply the -user
switch. For case, if yous desire to find all files on the arrangement that belong to the user booleanworld
, run:
notice / -type f -user booleanworld
You tin can as well use a user's ID in the in a higher place command, instead of the username. You can wait up an user's ID with the following command:
id -u <username>
Likewise, the -grouping
switch allows you to look for files and directories that belong to a particular group. It tin too accept a group proper noun, or a group ID. You lot can view a user's group ID with the following command:
id -g <username>
Searching for files based on engagement and fourth dimension
Sometimes, you may need to search for files depending on when information technology was accessed or modified. On a Linux system, there are three types of "time" associated with a file:
- Modification time: The concluding time when the file's content was modified.
- Access fourth dimension: The last fourth dimension when the file was read.
- Change time: The final time when the file (either its content or metadata, such as permissions) were modified.
To find files based on the modification, admission or alter time, the find control has the-mtime
, -atime
and -ctime
switches. These switches allows y'all to filter files based on the number of days. Here, a "day" refers to a period of 24 hours.
Let us understand how to utilize these flags. Equally an example, if you use -mtime
, then:
-
-mtime 2
: The file was modified ii days ago (i.e. in the last 48 to 72 hours). -
-mtime -ii
: The file was modified less than 2 days agone (i.e. inside the last 48 hours). -
-mtime +2
: The file was modified more than than 2 days ago (i.eastward. iii or more days, which means 72 hours or later).
And then, to find files in /usr
which were modified two days agone, run:
find /usr -type f -mtime 2
The -atime
and -ctime
switches work exactly in the same way equally -mtime
.
If you find that a twenty-four hour period is a bit too long, y'all can likewise use -mmin
, -amin
or -cmin
to filter in minutes. So, -amin +5
ways files that were accessed more than 5 minutes ago, -amin -5
means the file was modified less than 5 minutes ago, and then on.
Yous tin use many of these flags at one time. If y'all desire to find files modified ii days ago and accessed v minutes ago, run:
find /usr -blazon f -mtime 2 -amin 5
You can fifty-fifty echo the same flag! As an example, you lot desire to find files modified betwixt fifty and 100 days ago, employ:
notice /usr -blazon f -mtime +50 -mtime -100
Searching based on size
The find command has a -size
switch to allow users to observe files based on their size. Since simply files tin have a file size, when you use this switch no directories volition be listed.
To specify the file size, nosotros put a number followed by a alphabetic character to represent the unit. You can utilise the following messages:
-
c
: bytes -
k
: kilobytes -
Yard
: megabytes -
G
: gigabytes
In add-on, you tin use a +
or -
to impose a "greater than" or "less than" status.
Permit us understand this with a few examples. If you want to list all files on the organization with a size of 10 KB, utilize:
detect / -size 10k
In order to find files that are more than one GB in size, utilise:
find / -size +1G
Similarly, to find files that are less than 10MB in size, use:
notice / -size -10M
Searching based on permissions
The find command supports the -perm
switch to allow searches based on permissions. It allows you to search based on both numeric and symbolic modes. If you lot're unfamiliar with permissions, y'all can discover an overview here.
Using symbolic permissions
Allow us brainstorm with a simple example. Suppose, you lot want to search for all files and directories in the /usr
directory with permissions of rwxr-xr-x
. In lodge to do so, run:
find /usr -perm u=rwx,k=rx,o=rx
The messages u
, grand
and o
stand for "user", "group" and "owner" respectively.
You can also use the letter a
to check for permissions that employ to all users. For example, to discover all files and directories on the system with the permission r-xr-xr-x
, run:
find /usr -perm a=rx
Oft, we are interested to match a subset of the permissions. For case, you may want to search for files on the organisation that can be executed past all users. In this case, nosotros just care that the execute bit is set for all three permission classes. However, the rest of the bits may be gear up or unset.
Since all permission classes should have the execute fleck set, we write the permission every bit a=ten
. And then, we add a /
in front to indicate that we intend to match a subset of permissions. Thus, the command is:
detect / -type f -perm /a=10
Using numeric permissions
Let usa consider the first example in the previous section. This is pretty straightforward — rwxr-xr-x
is 644 in numeric fashion, and so yous tin can simply run:
notice /usr -perm 644
Checking for subset of permissions is a niggling more complex. As nosotros mentioned previously, searching for files that anybody tin can execute involves checking if the execute bit is set. We don't care about the other bits. For equally that we should check, nosotros put a 1, and for the residuum we put a 0. With this process, we obtain a binary number and we convert it to octal as shown:
Next, we add together a -
in front end of this number to betoken that we intend to friction match a subset of permissions. Thus, the command is:
discover / -type f -perm -111
Searching based on access rights flags
Now, say y'all're interested to search based on the numeric rights flags. For this purpose, y'all can employ either numeric or symbolic modes.
Equally an instance, say you're interested to observe all files on your system with the setuid chip set. We tin extend what we learnt about numeric modes to this situation. In this instance, we only care about the setuid flag, which has a value of 4. All the same, nosotros don't care about any of the permission bits, which means we get 000. Thus, we tin can search for these files using:
find / -type f -perm -4000
Of course, you tin can also filter for permission bits too. If y'all're interested in files that all users tin can execute, and too have the setuid fleck set, supercede 4000 with 4111 in the above command. (We've previously seen how we get the value 111.)
Similarly, you tin employ 2000 for the setgid bit and one thousand for the sticky bit. If you lot desire to exam for a combination of these $.25, add upwards the values. For example, to exam for the setuid and gluey flake, y'all would use 5000 (= 4000 + k).
If you want to do the aforementioned with symbolic modes, yous can use:
discover / -blazon f -perm /u=s
For the setgid flake, utilize /grand=south
, and for the sticky flake use /o=t
. To examination for a combination of these bits, split up them with commas, such as /u=south,o=t
.
Limiting the depth of traversal
As we mentioned previously, the detect command looks for items recursively. Nonetheless, the results obtained may be very large, and yous might desire to gear up a limit on how deep the find command can search. This can be washed with the -maxdepth
switch.
If y'all run the following control, y'all will find that all the displayed items are no more than 3 levels deep:
find / -maxdepth three
In other situations, you may want to set up a limit on the minimum depth. The discover command besides supports the -mindepth
switch for this purpose. Then, if yous run:
discover / -mindepth 3
Logical operations in the notice command
In the previous sections, nosotros take seen the flags supported by the discover command. We have also seen how we tin combine various flags and negate them. In some situations, we need more powerful constructs.
The discover command supports "and" and "or" weather condition with the-a
and-o
switches. It also supports grouping parts of an expression with parentheses ()
. However, since parentheses are also special characters for the shell, y'all should put them in single or double quotes. In improver, when you specify multiple flags ane afterward the other, the discover command automatically assumes an "and".
Permit us consider an case we saw earlier — finding directories inside /usr
that begin with "python". In that location, we used the control:
find /usr -type d -name 'python*'
You lot tin likewise write the "and" explicitly like and so:
find /usr -type d -a -name 'python*'
Now, allow united states consider a different case. Suppose, you want to search files/directories on the organization which were modified in the terminal five minutes or earlier than 50 days. For this command, we will use the "or" operator like so:
discover / -mmin -5 -o -mtime +l
Adjacent, suppose nosotros impose an boosted restriction to the above problem — you desire to search for files only. Start, we will keep the conditions for filtering modified files in a group. Then, we will "and" information technology with the condition to detect files. Thus, the final control is:
discover / '(' -mmin -5 -o -mtime +50 ')' -a -blazon f
Since the -a
is implicit, you can also leave it out of the above command.
Taking actions
So far, all the observe commands impress the results on the last. Usually, we want to perform some actions with these results, such as copying or deleting these items. Fortunately, the discover control also supports taking "deportment" on these results.
The default action is to print all the results; y'all tin can verify this by calculation the-print
switch at the end of any find command. There are many other actions besides. We will look at some of the most useful ones beneath.
Deleting files
To delete any files or directories, use the -delete
activity. It works with both files and directories. For example, if you want to delete all the empty directories from the home directory, run:
observe ~ -type d -empty -delete
Executing commands
The find command also supports executing a custom command with the -exec
switch.
Suppose, you want to back up all your MP3 sound from your dwelling house directory to your external difficult drive. Assume that the external hard drive is available at /media/MyDrive
. To copy a single file, you would use:
cp <path to file> /media/MyDrive
To copy all the files, y'all can use the following command.
find ~ -type f -name '*.mp3' -exec cp {} ';'
Hither, the pair of braces ({}
) act as the placeholder for the path to the file. In order to tell the file command where the command ends, we use a semicolon(;
). Even so, since information technology is also a special character for the shell, we wrap it in single quotes.
Whenever the observe control finds a file matching the status, it replaces the braces with the actual path, and executes the control. So, the cp
command is executed for every MP3 file.
Let us expect at another important utilize — finding a string across many files. To observe the cord "hullo" in a file, we employ grep like so:
grep hi <file proper noun>
If you are familiar with grep
, y'all might be tempted to write a command like this:
observe ~ -type f -exec grep hello {} ';'
However, if you lot use this command, you lot will immediately sympathize the trouble. We want a list of files with the string "hello"; however we get a list of matched lines. Fortunately, grep
has a -l
switch, which makes it impress the path of the file if there is a match. So, the command volition exist:
detect ~ -type f -exec grep -l hello {} ';'
Executing commands: a unlike variant
Suppose, you want to create a compressed file. To create a Gzip-compressed file with the proper noun music.tar.gz
, you would run:
tar -czf music.tar.gz <listing of files>
Now, suppose you lot desire to compress all MP3 files in your dwelling house directory, to a unmarried compressed file. Perhaps you would come up up with the following:
observe ~ -blazon f -name '*.mp3' -exec tar -czf music.tar.gz {} ';'
Nevertheless, if you open the music.tar.gz
file, you will notice that there's only one MP3 file. Remember that detect
executes the command every time it finds a new file. So, the previous file got overwritten with a new archive every time; and we don't want that to happen.
All of our issues could be solved if there was a way to tell find
to laissez passer a list of files to tar
, after it has institute all the files. This is where the second variant of the-exec
switch comes in. Instead of ending the command with a ;
, we use a +
like so:
find ~ -type f -name '*.mp3' -exec tar -czf music.tar.gz {} +
Executing commands on directories
Sometimes, it is useful to execute a control on the directory within which a file/directory nowadays. The detect command has a -execdir
switch which does this. Information technology is similar to -exec
in all other ways, and fifty-fifty has the +
and ;
variants.
Viewing file information
If yous want to view information well-nigh the files/directories (such equally permissions and size), you can utilize the -ls
switch. For case, if you lot want to view details near files on the system that are bigger than 1GB, run:
find / -type f -size +1G -ls
Removing mistake messages
While trying some of the discover commands, you may take got some errors such as "Permission denied". You tin hide these fault messages by redirecting them to /dev/null
, as shown:
find [paths] [expression] [actions] 2>/dev/cypher
Conclusion
In this article, we've seen how to utilise the find command, forth with examples that comprehend most of the utilize cases. If you want to read more, you can read the homo page by typing homo discover
in your terminal.
Source: https://www.booleanworld.com/guide-linux-find-command/
0 Response to "What Is the Find Command That Will List All of the Directories That "Other" Can Write to?"
Post a Comment