Linux find all files with extension

Recursively look for files with a specific extension

I’m trying to find all files with a specific extension in a directory and its subdirectories with my Bash (Ubuntu 10.04 LTS (Lucid Lynx) release). This is what’s written in a script file:

#!/bin/bash directory="/home/flip/Desktop" suffix="in" browsefolders () for i in "$1"/*; do echo "dir :$directory" echo "filename: $i" # echo $ extension=`echo "$i" | cut -d'.' -f2` echo "Erweiterung $extension" if [ -f "$i" ]; then if [ $extension == $suffix ]; then echo "$i ends with $in" else echo "$i does NOT end with $in" fi elif [ -d "$i" ]; then browsefolders "$i" fi done > browsefolders "$directory" 
[: 29: in: unexpected operator 

(with $extension instead of ‘in’ ) What’s going on here, and where’s the error? But this curly brace.

10 Answers 10

find "$directory" -type f -name "*.in" 

is a bit shorter than that whole thing (and safer. It deals with whitespace in filenames and directory names).

Your script is probably failing for entries that don’t have a . in their name, making $extension empty.

@flip: that’s a different question. Post a new question, detailing exactly what you’d like to do and what you’ve tried so far.

One little correction: use ‘*.in’ or \*.in instead of «*.in» because double quotes don’t prevent shell expansion. I.e. your script will not work properly if there’s a file with .in extension in the current directory.

find -type f -name '*.extension' 

Example: To find all csv files in the current directory and its sub-directories, use:

The syntax I use is a bit different than what Mat suggested:

find $directory -type f -name \*.in 

Matt’s script also won’t work if there’s a file with .in extension in the current directory, while yours would still work. See stackoverflow.com/questions/5927369/…

@gniourf_gniourf You should provide some reference for your statement, otherwise one could simply argue: «No, you are wrong». But in fact you’re right: gnu.org/software/bash/manual/html_node/Double-Quotes.html

@user1885518: I think that it should be the guy who claims that the script doesn’t work who should provide some examples where the script fails. That’s what I do when I leave comments where there are broken scripts: it’s usually about quotes and filenames containing spaces, newlines, globs, etc., and I specifically explain why it’s broken.

Providing reference is always a good way in a discussion, it does not depend on who was the first. He should, you should.

du -a $directory | awk '' | grep '\.in$' 

The grep isn’t really necessary here. awk has regular expressions and could limit its output to values matching a pattern.

This method is extremely useful if your going through 100s of terabyte. Find command takes too much time to process. This starts immediately.

Though using find command can be useful here, the shell itself provides options to achieve this requirement without any third party tools. The bash shell provides an extended glob support option using which you can get the file names under recursive paths that match with the extensions you want.

Читайте также:  Server installation in linux

The extended option is extglob which needs to be set using the shopt option as below. The options are enabled with the -s support and disabled with he -u flag. Additionally you could use couple of options more i.e. nullglob in which an unmatched glob is swept away entirely, replaced with a set of zero words. And globstar that allows to recurse through all the directories

shopt -s extglob nullglob globstar 

Now all you need to do is form the glob expression to include the files of a certain extension which you can do as below. We use an array to populate the glob results because when quoted properly and expanded, the filenames with special characters would remain intact and not get broken due to word-splitting by the shell.

For example to list all the *.csv files in the recursive paths

The option ** is to recurse through the sub-folders and *.csv is glob expansion to include any file of the extensions mentioned. Now for printing the actual files, just do

Using an array and doing a proper quoted expansion is the right way when used in shell scripts, but for interactive use, you could simply use ls with the glob expression as

This could very well be expanded to match multiple files i.e. file ending with multiple extension (i.e. similar to adding multiple flags in find command). For example consider a case of needing to get all recursive image files i.e. of extensions *.gif , *.png and *.jpg , all you need to is

This could very well be expanded to have negate results also. With the same syntax, one could use the results of the glob to exclude files of certain type. Assume you want to exclude file names with the extensions above, you could do

excludeResults=() excludeResults=(**/!(*.jpg|*.gif|*.png)) printf '%s\n' "$" 

The construct !() is a negate operation to not include any of the file extensions listed inside and | is an alternation operator just as used in the Extended Regular Expressions library to do an OR match of the globs.

Note that these extended glob support is not available in the POSIX bourne shell and its purely specific to recent versions of bash . So if your are considering portability of the scripts running across POSIX and bash shells, this option wouldn’t be right.

Источник

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.

Читайте также:  Ssh server on kali linux

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.

Источник

Find All Files with Extensions in Linux

When working with Linux, you may often need to find all files with a specific extension in a directory and its subdirectories. So for this many commands are available in Linux. In this tutorial, you will learn how to find all files with an extension in Linux using find, locate, and ls commands.

Читайте также:  Какие файлы открывает linux

How to Find All Files with Extensions in Linux

To find all files with extensions in Linux, you can use the following methods:

Using the find command

To find all files with an extension, use the following command:

find /path/to/directory -type f -name "*.extension"

In the above given command, you need to replace “/path/to/directory” with the path to the directory that you want to search for files. Replace “extension” with the extension of the files that you want to find.

For example, if you want to find all files with the “.pdf” extension in the /home/user/Documents directory, use the following command:

find /home/user/Documents -type f -name "*.pdf"

The above-given command will search for all files with the “.png” extension in the /home/user/Documents directory and its subdirectories.

Using the ls command

The ls command is used to list files and directories. To find all files with an extension, you can use the following command ls command:

ls /path/to/directory/*.extension

In the above-given command, you need to replace “/path/to/directory” with the path to the directory that you want to search for files. Replace “extension” with the extension of the files that you want to find.

For example, if you want to find all files with the “.pdf” extension in the /home/user/Documents directory, use the following command:

The above-given command will list all files with the “.png” extension in the /home/user/Documents directory.

Using the locate command

The locate command is used to search for files based on their names. To find all files with an extension, use the following command:

In the above-given command, you need to replace “extension” with the extension of the files that you want to find.

For example, if you want to find all files with the “.xls” extension, use the following command:

This command will list all files with the “.xls” extension on your Linux system.

Here are some faqs on How to Search and Find Files Recursively in Linux:

Q: How do I find all files with a specific extension in Linux recursively?

  • Another way is to use the grep command with the output of the find command.
  • One way is to use the find command with the -type f and -name options.
  • A third way is to use the ls command with the -R option and the output piped to grep.

Q: How do I find all files with a specific extension in the current directory and its subdirectories using the find command?

A: To find all files with a specific extension in the current directory and its subdirectories using the find command, use the following command:

find . -type f -name '*.extension'

Conclusion

In conclusion, finding all files with an extension in Linux is a straightforward task. You can use the find, ls, or locate command to search for files with a specific extension. The find command is the most powerful and flexible command that can search for files based on different criteria. The ls command is the easiest and quickest way to list files with a specific extension. The locate command is the most efficient way to search for files based on their names.

Источник

Оцените статью
Adblock
detector