
:max_bytes(150000):strip_icc()/002_uses-of-linux-command-find-2201100-5bd8f0d64cedfd00264fc4e5.jpg)
It is always a good idea to print the matched files before using the -delete option.įor example, to delete all files ending with. To delete all matching files, append the -delete option to the end of the match expression.Įnsure you are using this option only when you are confident that the result matches the files you want to delete. The common example would be to recursively change the website file permissions to 644 and directory permissions to 755 using the chmodĬommand: find /var/www/my_website -type d -exec chmod 0755 \ Find and Delete Files # To search for files based on their type, use the -type option and one of the following descriptors to specify the file type:įor instance, to find all directories in the current working directory Sometimes you might need to search for specific file types such as regular files, directories, or symlinks. For example, to find all files that don’t end in *.log.gz you would use: find /var/log/nginx -type f -not -name '*.log.gz' Find Files by Type # To find all files that don’t match the regex *.log.gz you can use the -not option. It is important to mention that you must either quote the pattern or escape the asterisk * symbol with backslash \ so that it doesn’t get interpreted by the shell when you use the wildcard character. log.gz inside the /var/log/nginx directory, you would type: find /var/log/nginx -type f -name '*.log.gz' For example, to find all files ending with. Searching for files by extension is the same as searching for files by name. The command above will match “Document.pdf”, “DOCUMENT.pdf”. To run a case-insensitive search, change the -name option with -iname: find /home/linuxize -type f -iname document.pdf The grep command searches through the file, looking for matches to the pattern specified. To find a file by its name, use the -name option followed by the name of the file you are searching for.įor example, to search for a file named document.pdf in the /home/linuxize directory, you would use the following command: find /home/linuxize -type f -name document.pdf Here, not is the pattern were searching for. js (JavaScript files).įinding files by name is probably the most common use of the find command.
FIND FILE BASH HOW TO
In this lesson, well learn how to find all the.
FIND FILE BASH CODE
Just save the above code in a file ending with a. find is a powerful tool that can not only find files but it can run a command on each matching file too. In that case, you can make use of a bash script that helps us to achieve the same output.Ĭonsider the script code shown below − for file in ** do echo $ done It should be noted that if you are not on a GNU terminal, then you might get an error stating that -printf is not found. Just write the command to your GNU terminal find. If you are making use of GNU terminal, then the command shown below will help you to achieve just that. Now we know how find statement works, let’s explore the case where we only want to get the names of the files and not everything that comes with it. Notice that if the find command is able to locate the file then it will simply print the name of the file, if not then it will not return anything the terminal process will In the linux code shown below I am trying to search for a file inside my Downloads folder, and for that I am making use of the find statement. Let’s explore an example of a find statement to understand it better.

It is used to mostly find a specific file or directories and we can also append different other Linux statements or flags along with it to enhance or do a complex operation. Linux find statement is one of the most widely used statements that allows us to walk a file hierarchy.
