15. find command - muneeb-mbytes/linux_course GitHub Wiki

find _command

The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file name, folder name, creation date, modification date.

find

      Fig-1: All variations of find command

Cheat sheet for find command:

Sr.no cd commands with different variations description
1 find file Used to find the file and directory.
2 find case Used to find the file and directory which will not bother about the text case.
3 find extension Used to find the all the files and directories which has a pirticular extension.
4 find empty Used to find the all empty files and directories.
5 find delete Used to find and delete the pirticular file and directory.
6 find modified_date Used to find all the files and directories with in the mentioned modified date.
7 find help Gives all possible variations available in find.
8 find regex Use of regular expressions in the find command

find -name file

This command is used to find the specific file name.

In the below figure Fig-2, you can see that we are finding a file 'state'. It displays the path where actually the file is located on our system.

Untitled Diagram drawio (9)

      Fig-2: Find command usage

The below GIF shows the whole process from sourcing scripting file till the output.

gif-find

      GIF-1: giphy for find command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find.csh

find -iname file

This command is used to find the file irrespective of it's case sensitive.

In the below figure Fig-3 you can see that the file 'STATE' is upper case but by using find case command even if you give lower case characters for file 'STATE' it will match and find the file where it is.

Untitled Diagram drawio (10)

      Fig-3: Find -iname command usage

The below GIF shows the whole process from sourcing scripting file till the output.

gif-findcase

      GIF-2: giphy for find_case command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find_casein.csh

find -name "*.txt"

This command is used to find multiple files of same extension.

In the below figure Fig-4 you can see that if there are more files with same extension in fig it is '.txt' you can use the command so that it will show all possible files with '.txt' extension on your system along with their paths.

Untitled Diagram drawio (11)

      Fig-4: Find_extension command usage# 

The below GIF shows the whole process from sourcing scripting file till the output.

gif-find extension

      GIF-3: giphy for find_extension command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find_extension.csh

find -empty

This command is used to find all the empty files and directories.

In the below figure Fig-5 you can see that the files and directories those are empty will be found and displayed along with it's path where it is located on your system.

findempty

      Fig-5: Find_empty command usage

The below GIF shows the whole process from sourcing scripting file till the output.

gif-find empty

      GIF-4: giphy for find_empty command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find_empty.csh

find -name "file" -exec rm -rf {} ;

This command will first find the file and then delete it automatically.

In the figure Fig-6 you can seen that before using the command we have file2.txt but after using the command it will find for the file and then delete it automatically.

Untitled Diagram drawio (12)

      Fig-6: Find_delete command usage

The below GIF shows the whole process from sourcing scripting file till the output.

Untitled design (3)

      GIF-5: giphy for find_delete command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find_delete.csh

find -mtime no_of_days

This command is used to find files which are created on specific date and time.

In the below figure Fig-7 you can see that by using the command 'find -mtime -5' it will search for all the files which are created 5 days before on your system.

find modifi

        Fig-7: Find_modified command usage

The below GIF shows the whole process from sourcing scripting file till the output.

Untitled design

        GIF-6: giphy for find_modified command

GitHub link: https://github.com/muneeb-mbytes/linux_course/blob/b7_Team_BJT/find_commands/find_modified.csh

find --help

This command is used to display all possible variations of find command.

In the below Fig-8 you can see that by using this command we can see all possible variations of find command.

find_help (1)

      Fig-8: find --help command usage

find ./ -type -f -regex '.*.log'

This command is used to find all the files ending with "log" in current directory and all it's subdirectories.

To learn grep command : https://github.com/muneeb-mbytes/linux_course/wiki/16.-grep-command