Best 10 find command examples in Unix and Linux
- Admin
- Dec 31, 2023
- Linux-unix
In this tutorial, Learned the frequently used top 10 find command examples in Unix/Linux OS.
- Show all files and nested directories in Linux
- Find a file by name in a directory structure in Unix
- How to run the last executed find command
- Find all files owned by a user
- Find file search using name ignore case sensitive
- Find files based on file type
- Find files in the level-down current directory
- Show folder and subfolders
- Find files that are modified in the last 10 mins Best Find command examples in Linux and Unix:-
find command
is one of the frequent commands used in Linux and Unix operating systems. The find command helps search for specific files in a directory and its subdirectories.
I use this command to search logs and java files on the Linux operating system as part of my java programming development.
Users can search any files directly using the windows explore search feature in windows. 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 the find command because they need to search source code files and application logs where the errors occurred.
In Linux/Unix, the find command
can be combined with grep and other file-related commands to provide a powerful search.
So, I am listing down some of the find commands
that were used daily as part of my software project development.
Find command options in Unix and Linux:- usually, find command syntax is as follows.
find {directorytosearch} {options}
The following are the options to be given to find commands
- Filename to be searched in the list of files.
- directory or list of files to be specified where a search for the filename is to find.
- the type of files or directories
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 all the files in the current directory as well as the subdirectory. this command shows all the files in the directory and subdirectories to the console.
how to find files using the 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 the console.
How to execute the last executed find command
!find
find . -name HelloWorld.java
./directory2/HelloWorld.java
./kiran/HelloWorld.java
The last executed command is usually stored in the session-level cache, so typing the same command is a time-consuming task. so the alternative step is using this command will execute the last executed find command. This ! can be used with any Linux command to execute the last specified command.
Find all files that are owned by a 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 a specific user, it gives output as ‘filename and permission denied’ message to the user
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 the ignoring case
Find the files which are modified on the last day:-
$ find . -mtime 0
./directory2
./directory2/HelloWorld.java
./kiran
./kiran/HelloWorld.java
This is very useful for application log changes that were modified in the last 24 hours. This is one of the favorite find command bashes to know the modified files on the last day.
How to find the files which are modified in the last 10 mins in Unix
find . -mmin 10
It searches files that are modified/created in the last 10 mins. It is one of my favorite find commands and a few more examples for finding the modified files between 10 and 20 minutes in the Linux
find . -mmin +9 -mmin -21
How to list only the subdirectories and directories in a file system
find . -type d
./directory2
./kiran
./kiran/subdir
How to find file names in a subdirectory that is one level down the 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 the find command.
find all files based on file types?
normally, with the find command, we search files only. with the find command by providing -type option, we can search directories and symlinks in Linux
- Find regular files in Linux
find . -type f
./directory2/HelloWorld.java
./kiran/HelloWorld.java
- Find directories in Linux
find . -type d
./directory2
./dir
./kiran
./kiran/subdir
Find all the symlinks using the find command.
find . -type s
I hope you understand the basics of find commands with examples. I will write one article about the advanced usage of the find command in Linux and Unix.
Please share your comments.