Top 10 examples of find command in unix and linux


Best Find command examples in linux and unix:-

find command is one of frequent command used in linux and unix operating systems.
find command is used by unix/linux users to search the specific files in directory as well as sub directories.
 find command examples in unix and linux
I use this command as part of my programming development with java for searching logs as well as java files in linux operating system.

In windows enviornment, users have the ability to search the any files directly using windows explore. But in Linux/Unix, we have to use command based options to search files.

Most of the people working on development projects, need to know the basics of find command, because they need to search source code files as well as applicaiton logs where the errors occured.

Find command can be combined with grep and other file related commans to make the powerful search in linux/unix.

So i am listing down the some of the find commands that i use daily as part of my software project development.


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

I hope you understand the basics of find command with examples.
I will write one article about advanced usage of find command in linux and unix.

Please share your comments .


1 comment:

  1. Thanks for the post. It is really helpful. I'm customising my linux and as a newbie it is a stepping stone for me. Many Thanks.

    ReplyDelete

Note: only a member of this blog may post a comment.