Find files with file extension linux

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.

Читайте также:  Astra linux центр сертификации

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.

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

Читайте также:  How to make file executable on linux

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.

Источник

How to Find Files with Extension in Linux

As a Linux user, you might need to locate files with specific extensions for various reasons, such as managing configurations, troubleshooting, or scripting. For example, finding .ini files can help you manage settings for multiple applications. You can do it using the find command. In this article, I will show how to locate files with the .ini extension in the /etc/ directory.

sudo find /etc/ -type f -name «*.ini»

sudo-find-sl-etc-sl-type-f-name-ini.png

The sudo command is used when searching in the /etc/ directory because it may contain files and directories that are only accessible to the. However, If you are searching in the home directory — sudo is not necessary.

In the command mentioned above, /etc/ is the starting directory for the search. The -type f option instructs the find command to look only for regular files, excluding directories and other file types. The -name option specifies the search pattern. In this case, the wildcard pattern is *.ini. The asterisk (*) wildcard is utilized to match any sequence of characters, while .ini specifies the desired file extension at the end.

Читайте также:  Linux html to image

You can replace ini with any desired extension to find files with different extensions in Linux. If you are interested in learning more about file searching in Linux, consider reading this article.

Found a mistake in the text? Let me know about that. Highlight the text with the mistake and press Ctrl+Enter.

Источник

How to use find command to find all files with extensions from list?

@Gerald: It’s likely you need to group your OR expression in escaped parentheses: find /path/to/ \( -iname ‘*.gif’ -o -iname ‘*.jpg’ \) -exec ls -l <> \; otherwise the exec only applies to the last part ( -iname ‘*.jpg’ in this case).

That’s an important remark. find /path/to/ -iname ‘*.gif’ -o -iname ‘*.jpg’ -print0 will only print the jpg files! You need brackets here: find /path/to/ \( -iname ‘*.gif’ -o -iname ‘*.jpg’ \) -print0

find -E /path/to -regex «.*\.(jpg|gif|png|jpeg)» > log

The -E saves you from having to escape the parens and pipes in your regex.

Hmmm the -E option tells find to use «extended regular expressions». Several other tools have a similar option, but I’m not sure this option is available on all UNIX distributions.

@tboyce12 Working on ubuntu, I can specify the -regextype to simplify the regex expression: find . -regextype posix-extended -regex «.*\.(jpg|gif|png|jpeg)» .

@cjm Maybe find -E /path/to -iregex «.*\.(jpg|gif|png|jpeg)» > log . Using the -iregex flag tells find to match case insensitively.

find /path/to/ -type f -print0 | xargs -0 file | grep -i image 

This uses the file command to try to recognize the type of file, regardless of filename (or extension).

If /path/to or a filename contains the string image , then the above may return bogus hits. In that case, I’d suggest

cd /path/to find . -type f -print0 | xargs -0 file --mime-type | grep -i image/ 

Источник

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