Find file linux extension pack

How to Find Linux Files With Extensions?

In Linux, searching for specific files is a difficult process, especially when you have a lot of files in the directory or you want to search for more than one file simultaneously. There are some utilities such as “find” and “locate” that can be used in this case. These commands have various features like searching for files with extensions, modification time, file size, etc.

This post will address the possible methods to find files with extensions in Linux. The content for the write-up is as follows:

  • Method 1: Using find Command
    • Example 1: Finding Files of Specific Extensions
    • Example 2: Finding Multiples Files With Extension
    • Finding “.deb” Extension Files
    • Finding “.zip” Extension Files

    Method 1: Using find Command

    The “find” command is a utility to search the files and directories. It also assists in searching the files with extensions. The syntax for the “find” command is given below.

    Syntax:

    $ find [Path] [-Options] [Expression]

    Type the “find” keyword, “path” where you want to search, “options” such as name, and “expression” to search for any files.

    Let’s practice and search some files using the “find” utility in Linux.

    Example 1: Finding Files of Specific Extensions

    To find files of a specific extension, the wildcard character “*” is utilized before the extension. This example will illustrate various scenarios to find files by extension. Let’s dig into them:

    Finding Text Files

    In the following example command, all the text files from the directory “dir1/demo” will be retrieved:

    The output shows the name along with its path.

    Finding “.sh” Files

    Similarly, to display “.sh” extensions files in the directory is obtained as follows:

    $ find /home/itslinuxfoss -name "*.sh"

    The above image shows all the files with the “.sh” extension.

    Example 2: Finding Multiples Files With Extension

    Using the “find” command, users can also search the multiple files with their extension name. The syntax for searching the multiple files is shown below:

    Syntax:

    $ find [Path] \( [-option] [Expression 1] -o [-option] [Expression 2]. [-option] [Expression n] \)

    Type the “find” keyword, and “path” where you want to search, type “option”, and “expression” multiple times, and separate them by “o” option. “o” is representing OR.

    Let’s apply this syntax in the terminal for multiple files. In this case, we are searching the “.gz” and “.deb” extension files via the below-mentioned syntax:

    $ find dir1/demo \( -name "*.gz" -o -name "*.deb" \)

    In the above image, we can see that files with the given extension’s name have been displayed.

    Note: The find command will search the files in the folders/directories and the sub-folder/sub-directories.

    Let’s move to the second method to find the Linux files with Extensions.

    Method 2: Using locate Command

    There is another “locate” command available that can be used to search the files with extensions. The Locate command is not pre-installed on some distributions of Linux. Still, it can be installed by installing the “locate” package:

    For Debian-Based Distros:

    For Fedora-Based Distros:

    For Arch-Based Distros:

    The syntax for the locate command is given below:

    Syntax:

    $ locate [Searching Path] [Expression]

    Type the “locate” keyword, “Path” for searching files, and “expressions” for searching files.

    Finding “.deb” Extension Files

    In this example, “.deb” extension files will be searched. To do this, execute the below command:

    All the files in the “home” directory and the sub-directories have been displayed, as shown in the above image.

    Finding “.zip” Extension Files

    Likewise, for searching any file having “.zip” extensions are obtained as follows:

    The above has displayed all the “.zip” files in the given path.

    Method 3: Find Files Using grep Command

    Except for these two methods, you can also find files with extensions using the “grep” command. The “grep” command is used to search and match any file text. We can use a combination of ls and pipe(|) commands to search the files by their extension name.

    Command to search all the files that end with the “.gz” extension is as follows:

    In the command, the “ls” command is used to locate the directory of the files which is sent to the “grep” command as an input and the “grep” command will search for the “.gz” extension files. Here, the “$” sign indicates to search the given expression at the end of the file name:

    The files having the “.gz” extensions are displayed on the terminal.

    Note: You can use extension names without “.” such as grep “.*gz”.However, the recommended method is practiced in the above command.

    Conclusion

    To find any Linux file with an extension, “find”, “locate”, and “grep” commands are used, which search the files from the given path. These utilities can be exercised to find files having the same extensions and of different extensions as well. This write-up has demonstrated all the possible methods to search any Linux files with their extensions.

    Источник

    Find All Files with Extension in Linux

    Often, we find ourselves stuck when we have to find all files with the same or different extensions. This has most likely happened to various Linux users while using the terminal. It is one thing to search for a single file type or file, but what will you do when you want to find out all files simultaneously? This article comes to the rescue for our readers who have such a dilemma.

    We can use various Linux utilities for finding or locating files on a file system, but searching all files or filenames with the same or different extensions can be difficult and require specific patterns or expressions. In the upcoming section of the article, we will understand the working, syntax, and execution of these utilities.

    Find command

    One of the most powerful file searching tools in the Linux system is the “find command.” It searches the entire directory for files and folders to get matched with the user’s expression and performs actions on these files. File permission, file size, type are some other factors based on finding files on Linux. Find command also be combined with other utilities such as sed or grep. Now, lets’ head towards the practical implication of find command.

    Find command syntax:

    Finding all files with a single extension:

    To find all files with a file extension, write out its path to find a command with the options and expression specifying the extension. In the below-given example, we will find all files with the “.txt” extension.

    “.” in this command denotes that this tool will find all the “.txt” files in the current directory.

    Find “.exe” files in the same find command by adding the extension as “*exe.”

    Configuration files are also an essential part of any file system that can be used for multiple purposes. Write out this command for searching configuration files in the current directory.




    Finding files with multiple extension:

    You can also add more than extension in your find command so that you can find several extension files easily and quickly.

    The execution of below given command will retrieve files with extension “.sh” and “.txt”

    Locate command

    The locate command is a faster and better tool as compared with “find.” When a file is initiated, instead of searching it in the file system, locate utilize the database for the searching requirement. This database stores parts and bits of the information related to files and their addresses on your system.

    locate command syntax:

    Finding a file with a specific extension, such as “.conf,” which is considered in our case, adds the directory path where the process of searching files will occur.

    Find configuration files in the present working directory by utilizing the below-given command.

    Similarly, you can follow the syntax of locate command for finding all files with any specific extension such as “.txt.”

    Conclusion:

    This post covers two powerful yet simple utilities for you to find all files with the same or different extensions. We have provided you the fundamental concepts regarding the “find” and “locate” command and shown you how to utilize these two Linux command-line tools to find all files with several extensions.

    About the author

    Talha Saif Malik

    Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.

    Источник

    How to Use ‘find’ Command to Search for Multiple Filenames (Extensions) in Linux

    Many times, we are locked in a situation where we have to search for multiple files with different extensions, this has probably happened to several Linux users especially from within the terminal.

    There are several Linux utilities that we can use to locate or find files on the file system, but finding multiple filenames or files with different extensions can sometimes prove tricky and requires specific commands.

    Find Multiple File Names in Linux

    One of the many utilities for locating files on a Linux file system is the find utility and in this how-to guide, we shall walk through a few examples of using find to help us locate multiple filenames at once.

    Before we dive into the actual commands, let us look at a brief introduction to the Linux find utility.

    The simplest and general syntax of the find utility is as follows:

    # find directory options [ expression ]

    Let us proceed to look at some examples of find command in Linux.

    1. Assuming that you want to find all files in the current directory with .sh and .txt file extensions, you can do this by running the command below:

    # find . -type f \( -name "*.sh" -o -name "*.txt" \)

    Find .sh and .txt Extension Files in Linux

    Interpretation of the command above:

    1. . means the current directory
    2. -type option is used to specify file type and here, we are searching for regular files as represented by f
    3. -name option is used to specify a search pattern in this case, the file extensions
    4. -o means “OR”

    It is recommended that you enclose the file extensions in a bracket, and also use the \ ( back slash) escape character as in the command.

    2. To find three filenames with .sh , .txt and .c extensions, issues the command below:

    # find . -type f \( -name "*.sh" -o -name "*.txt" -o -name "*.c" \)

    Find Multiple File Extensions in Linux

    3. Here is another example where we search for files with .png , .jpg , .deb and .pdf extensions:

    # find /home/aaronkilik/Documents/ -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.deb" -o -name ".pdf" \)

    Find More than 3 File Extensions in Linux

    When you critically observe all the commands above, the little trick is using the -o option in the find command, it enables you to add more filenames to the search array, and also knowing the filenames or file extensions you are searching for.

    Conclusion

    In this guide, we covered a simple yet helpful find utility trick to enable us find multiple filenames by issuing a single command. To understand and use find for many other vital command line operations, you can read our article below.

    Источник

    How to use find command to search for multiple extensions

    find ./ -type f \( -iname \*.jpg -o -iname \*.png \) works like a charm.

    NOTE There must be a space between the bracket and its contents or it won’t work.

    • -type f — only search for files (not directories)
    • \( & \) — are needed for the -type f to apply to all arguments
    • -o — logical OR operator
    • -iname — like -name , but the match is case insensitive

    @miked The command will «work» without them, yes, but you’d wind up getting hits on directories that end in .png as well as files that end with .jpg, which is not exactly what was intended.

    Thanks for clarifying! The type -f does not extend and apply to both expressions without the parentheses, So, find ./ -type f -iname \*.jpg -o -type f -iname \*.png also works . although it’s two characters longer 🙂

    Thanks, and I have rewritten a * (b + c) as a * b + a * c with: find ./ -type f -iname \*.jpg -o -type f -iname \*.png

    You can combine criteria with -o as suggested by Shadur. Note that -o has lower precedence than juxtaposition, so you may need parentheses.

    find . -name '*.jpg' -o -name '*.png' find . -mtime -7 \( -name '*.jpg' -o -name '*.png' \) # all .jpg or .png images modified in the past week 

    On Linux, you can use -regex to combine extensions in a terser way. The default regexp syntax is Emacs (basic regexps plus a few extensions such as \| for alternation); there’s an option to switch to extended regexps.

    find -regex '.*\.\(jpg\|png\)' find -regextype posix-extended -regex '.*\.(jpg|png)' 

    On FreeBSD, NetBSD and OSX, you can use -regex combined with -E for extended regexps.

    Источник

    Читайте также:  Test java in linux
Оцените статью
Adblock
detector