Contents...
On a Linux machine, there are 2 ways one can use to locate a file – the locate command and the find command. With the find command in Linux, you can find files by name, by size, by user, by permission, and by date and time amongst others. In fact, it can search for the file using your search parameters. If you want to find the file that was accessed n minutes ago, then that’s possible too! There are many many search parameters you can use, but we will discuss only a few here. Further, you can also perform actions on the files you find! In this tutorial, we’ll review the find command.
Find Command in Linux
These are the commands to find files in Linux:
Finding files by Name
(a) Finding a name using a pattern
find -name [pattern] Ex: find -name “ha*”
The latter would find any file starting with the letters “ha”. However, this would search for the exact term “ha” and is case sensitive.
(b) Suppose you wanted it to be case insensitive, then you can use iname:
find -iname [pattern] Ex: find -iname “ha*”
Finding files by Size
Finding files by size n. Once again, the minus sign means less than and the plus sign means greater than. So if I say -1G, it means any file less than 1Gigabyte.
find -size [n] Ex: find -size 500b Ex: find -size -1G
Here:
b – 512-byte blocks
c – bytes
w – two-byte words
k – kilobytes
M – Megabytes
G – Gigabytes
Finding files by User
Lets find files via user (here, the user is kalyani):
find -user [uname] Ex: find -user kalyani
Finding files by Type
You can also find files by type where c is a type of file:
find -type [c] Ex: find -type f
Where c can be any of the following:
b – block special
c – character special
d – directory
p – named pipe
f – file
l – symbolic link
s – socket
D – door (Solaris)
Finding files by Permission
You can also find files by permission:
find -perm [mode] Ex: find -perm 777 Ex: find -perm g=w
The find where perm is g=w is equivalent to mode 020 (where only group has write permission). Here, this is an exact match. Now, what if you wanted a bit more freedom?
find -perm -mode Ex: find -perm -766
In this case, we have specified 766, that means that it will find all files that are a minimum of 766, that means that if the file has a permission of 776, it will be included in the search.
The next one is:
find -perm /mode Ex: find -perm /770
In this case, it will look for any owner or group files with read-write-execute permissions.
Finding files by Date and Time
(a) You can also find recently accessed files using the last n*24 hours (where n is any integer).
find -atime [n] Ex: find -atime 2
(b) Finding a file accessed n minutes ago (where a plus sign before the 10 means greater than 10 minutes, and a minus sign means less than 10 minutes):
find -amin [n] Ex: find -amin 10 Ex: find -amin +10
(c) The file was modified n minutes ago:
find -mtime [n] Ex: find -mtime +5
What this means is that the file was modified greater than 5 minutes ago.
(d) File was changed n minutes ago
find -cmin [n] Ex: find -cmin -5
Deleting
Next, let’s see what we can do with the files we find using the find command. In particular, let’s see how to delete a file that was found.
find -delete Ex: find -name kalyani.txt -delete Ex: find -name “*.txt” -delete
What this will do is to delete the file called kalyani.txt. Make sure to specify the delete command at the end and not the beginning or else you might lose more than you expected as it is evaluated as an expression!
Execute
You can also execute a command:
find -exec [command] Ex: find -name “ha.txt” -exec file {}\;
The {} is used to replace the filename, and the \ is used to escape.
On a linux system, finding files can be a tedious task for some, and for that purpose, there exists the find command that will allow you to not only find the files you want but also take an action on them!
Happy Coding!
FAQs
What is the find command used for on a linux machine?
The find command in Linux is used to locate a file using specific search parameters. Actions can also be performed on the files found using the find command.
How do you find a file with a specific name on a linux?
You can use the find command in Linux with search parameters name or iname. For example: find -name file.txt. We use iname when we want a case insensitive search.
How do you find a file by size on a linux?
You can use the find command in Linux with the search parameter size. For example: find -size +1G. Here, the plus 1G means greater than 1G.
How do you find a file by permission on a linux?
You can use the find command with search parameters perm. For example: find -perm 777 or find -perm -744 or find -perm /744.
How can you delete a file that you have found with the find command on linux?
You can delete it using the find command with search parameter delete. Please note that the delete must be located at the end and not the beginning of the expression. For example: find -name file.txt -delete.
How can you execute commands using the find command on linux?
You can execute a command using the exec search parameter. For example: find -name “ha.txt” -exec file {}\;.
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
Leave a Comment