Archive

Beginner Unix Find Command Examples

Find command options in unix and linux:-
usuall find command syntax is as follows.

find {directorytosearch} {options}


The following are the options to be given to find command.
1.File name to be search in the list of files.
2. directory or list of files to be specified where search for file name is to find.
3. type of files or directories

1.how to list all the files in the directory and subdirectories?:-

find . or find . -print
./directory2
./directory2/HelloWorld.java
./dir
./dir/file.txt
./kiran
./kiran/HelloWorld.java

. represents the all the files in the current directory as well as sub directory.
this command shows all the files in the directory and sub directories to the console.

2. how to find files using name in the current directory:-

find . -name "HelloWorld.java" 
./directory2/HelloWorld.java
./kiran/HelloWorld.java

the above command, file name "HelloWorld.java" specified using the option -name searched in the current directory specified by option .
and print the file name to console.

3.How to execute last executed find command:-

!find
find . -name HelloWorld.java
./directory2/HelloWorld.java
./kiran/HelloWorld.java

The last executed command usually stored in the session level cache, so typing the same command is time consuming task.so alternative step is using this command will execute the last executed find command.
This ! can be used with any linux command to executed the last specified command.

4. Find all files that are owned by specific user?

$find /directory -user kiran 
./directory2
./directory2/HelloWorld.java
./dir
./dir/file.txt
./kiran
./kiran/HelloWorld.java

usually if we search the files that are not owned by specific user, it gives output as 'filename and permission denied' message to the user

5 how to find file search using name ignoring case sensitive :-

$ find . -iname helloworld.java
./directory2/HelloWorld.java
./kiran/HelloWorld.java

Here the file name case is ignored and displays all the files with ignoring case

6. Find the files which are modified in the last one day:-

$ find . -mtime 0
./directory2
./directory2/HelloWorld.java
./kiran
./kiran/HelloWorld.java

This is very useful for applicaiton log changes that were modified in the last 24 hours. This is my one of the favourite find command bash to know the modified files in the last one day.


7. How to find the files which are modified in the last 10 mins in unix.

find . -mmin 10

This is basicall used to search files which are modified/created in the last 10 mins. This is one of my favourite find command 
and few more examples for finding the modified files between 10 and 20 minutes in the linux

find . -mmin +9 -mmin -21 


8.How to list only the subdirectories and directories in file system.

find . -type d
./directory2
./kiran
./kiran/subdir

9.How to find file names in subdirectory that is one level down current directory?

find -maxdepth 2 -name "HelloWorld.java"
./directory2/HelloWorld.java
./kiran/HelloWorld.java

-maxdepth 2 means subdirectories i.e level 2
if we want to find the files in the current directory , we have to use -maxdepth 1 with find command. 

10.find all files based on file types?
normally, with find command, we search files only. with find command by providing -type option, we can search directories and symlinks in linux 
1.Find regular files in linux

find . -type f
./directory2/HelloWorld.java
./kiran/HelloWorld.java

1.Find directories in linux

find . -type d
./directory2
./dir
./kiran
./kiran/subdir

Find all the symlinks using find command

 
find . -type s