20+ Find command which you can use daily

What is find command?

A situation may arise when you want to know where the specific files or directories are located. Finding a specific file in each directory is a menial task.

To overcome the above situation, you can use the find command, which is the most powerful utility to find a specific file or directories. Moreover, you can use a number of options along with grepawksed commands to refine your end results.

While executing the find command, make sure to have read permission on a specific directory; otherwise, you will get a “Permission denied” error.

To understand you better, we will show you the basic syntax of the find command.

Basic Syntax of find command

find [SEARCH-LOCATION] [-OPTIONS] [SEARCH EXPRESSION]
  • [SEARCH-LOCATION]:provide the location where you want to search.
  • [-OPTIONS]: find command accept more than 50 options like -name,-size,-inum and many more.
  • [SEARCH EXPRESSION]: specify the file name or an expression that you want to search..

Find file or directory using Time

Find all the files used in the last, more or exact 30 Minutes.

You can use the -amin option with the find command to list out the file which has been accessed in the last 30 minutes in a home directory.

To check that, pass the below command.

$ sudo find /home -amin -30

Suppose you want to find the file according to access time, you can freely do it but make sure that it should be in minutes format. The –amin works differently if you missed to put respective symbols +30, -30, or just 30.

The below output will show the file which has been accessed

$ sudo find /home -amin +30

If you just want to print which all file has been exactly accessed in 30 minutes back, then use the below command.

$ sudo find /home -amin 30

Find all the changed file in last 1 hours.

The -cmin option can be useful to know which file data or directory content has been changed, at a specific time, to check that pass pass the below command:

$ sudo find / -cmin -60

You can change the time duration as per your need and -cmin and make sure the result may be differ if you miss using (+n, -n or, n ) n denotes for minutes.

How to Find file using name conventions

Find File using name in current Directory

If you are very specific about file name and want to check whether the file is present in the current directory or not?

As you know, “.” is a shorthand for the current directory, and by using the -name option, you can find the specified file using the below code.

$ find . -name showcase.txt

Find Files under home directory

When you want to find a specific filename under a specific directory, then you can follow these steps and make sure to replace the path according to your choice.

$ find /home -name showcase.txt

Find Files Using name with ignoring case

The above options are good only when you know the exact name of the file or directory and what to do if you don’t know the file name. In that case, you can use the below command:

$ find /home -iname ShowCase.txt

e.g. showcase or ShowCase both are case-senestive.

Read this: 30 Basic commands which every Linux user should know

How to Check File type using Find

Find Files by type of files

As you know in Linux everthing is file and every file has specific type such as regular file, block, character, directory and many other types.

For a example if your linux system has multiple files with a same name but their internal file type is different from each other, how you can distingush?

To resolve this we have -type option where can speciy the type of file with the following flags.

  • -l : use for symbolic link
  • -f : useful for regular file.
  • -b : use for block file.
  • -c : use for character file
  • -d : use for directory
  • -s : use for socket
  • -p : use for FIFO

For example if you want to find all the symbolic file present in present directory for that throw below command.

$ find . -type l

Find Directories Using Name

There are possibilty showcase.txt can be a text file and showcase can be a directory, how to ask find command to find explictly search for showcase directory and ignore the text file.

To resolve this issue we can take a help of -type options which includes various flags like -f for regular file.

Lets find the all directories whose name is showcase.

$ find / -type d -name showcase

Find using extension name like .txt or any other

Of course, every time you will not remember the file name, but you are sure the file ends with a specific extension, which make easier your work to find a spefic file.

When you need to find all the txtformat files in the respective directory, then you can use the below command.

$ find . -type f -iname "*.txt"

How to find file size using find command

Find file size

You can leverage the find command to find a file according to size, with the help of -size options that support various data units such as.

  • b: default 512 bytes
  • c: bytes
  • w: 2 bytes word
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

If you want to list a less than 5MB file, type the following code.

$ find . -type f -size -5M   

What to do if you want to find a file which ranges from 5MB to 10MB?

$ find . -type f -size +5M -size 10M

The below command can be useful to find the size of file.

$ find -type f -exec du -Sh {} \;

How to Check File Permission using Find

Find file which has read permissions

When you want to know which file has what type of permission, then you can use the -perm parameters along with asymbolic code (r,w,x) and specify user (u,o,g).

If you want to check wheter the owner of file has just read permission in the current directory, you can type this to check.

$ find . -perm u=r

If you are not satisfied with the above result, you can use the below command, which will find all the files that have read permission, including write and execute, for the file owner.

$ find . -perm -u=r

To verify output you can use stat command.

Find file which has both read and write permissions

Like the above command, use this too; just make sure to add the correct appropriate attributes.

$ find . -perm u=rw

Find all file permission using 775

Not familiar with symbolic mode, you can use the octal number, which is more convenient to use. Personally, I use both commands.

If you want to find a file or directory that adheres to 775 permission, type the below command in your terminal screen.

$ find . -perm 775

You can alter the output using or / symbols.

$ find . -perm -775
$ find . -perm /775

To know what the above command can do, use a man command and search for -perm options.

Find directories with 777 permissions

When you want to check which directories have 777 permission, for that, you can use -type and d suffix for the directory.

$ find -type d -perm 777

Find file which has 777 permissions

When you want to check which file has 777 permission, then you can use -type and specify f as a file.

$ find -type f -perm 777

How to Change file permission using find

Change file permission

It would be better if we could change the permission using the find command itself rather than executing chmod on a respective file one by one.

I want to update all the file which has 0755 permission with a 644. To perform this, we require -exec options that can execute the chmod command to change permission.

$ find / -type f -perm 0755 -print -exec chmod 644 {} \;

Change directory permissions

The below command can be used to change all the directory permission which has 755 to 644.

$ find / -type d -perm 0755 -print -exec chmod 644 {} \;

How to find & remove empty file using find

Find the all empty files

It is common to create an empty file. To properly organize your directory, you should remove it if it is not required.

You can take the help of -empty options, which will read the file size, and if the file size is equal to 0, it will print out the empty file name list.

$ find -type f -empty 

Before making any executions, make sure to read a file using cat command.

Find all the empty directories

You should remove the empty directories from the system, which is no longer required, but how will you know which directory is empty?

Same use the find command with -empty options.

$ find -type d -empty 

Delete file using find command

You can even remove a file or directory using the find command, with extreme a caution, although the result has been evaluated using find, which may need human interference.

You can delete a file or directory using the -delete or -exec command.

To use -delete command, use the below code:

$ find -type f -name "showcase.txt " -delete

Remove empty file using find command

If you want to remove an empty file, pass the following command.

$ find -type f -empty -name "showcase.txt" -exec rm -f {} \;

Find all the hidden files

In Linux, many hidden files contain basic configuration, which can be fatal if the user makes any changes to it. It’s better to keep it hidden.

Perhaps, you want to know how many hidden files are stored on your computer. To check that, pass the below command.

$ find -type f -name ".*"

Wrap up

In the find command, every symbol or attribute can change the output result while typing; ensure that you have mentioned the correct symbol or attributes.

There are a number of options still left to write.
If you want me to write more on the find command, do let me know in a comment section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments