Find all files ending in extension in bash

how to list all files ending with txt in linux
bash find all files with extension
bash find all files with extension recursively
linux find files matching extension
bash find all files with extension in directory
find all files having extension sh
mac terminal find all files with extension
find files ending with linux

Write a bash script accepting two arguments: directory and extension. The script must print to stdout the path of all files in directory (and, recursively, its sub-directories) with a name ending with extension.

So far I have:

find {directory} -type f -name ' *.extension *

when I give it 2 args it doesn't seem to find anything.

This script should do the trick:


find "$1" -type f -name "*.$2"

Examples to run the script:

/bin/bash /srv/directory css
/bin/bash '/srv/directory with space' css

List all the files in ending with several file extensions?, Use this: ls -l /users/jenna/bio/*.{fas,pep}. Or this: find /users/jenna/bio -iname '*.​pep' -or -iname '*.fas'. Say, for instance, you want to locate all regular files that end in the.conf extension. This command would look something like: find / -type f -name "*.conf" The above command would traverse the entire directory structure to locate all regular files ending in.conf.

In this answer, I'm just giving some explanation for Viktor's code.

This explanation might help those of you who are learning bash. Viktor updated his answer after I posted the "space issue" part and before I finished writing and saving this explanation. That's what I hoped for.

It is very concise, and there are great explanations. The original code was:


find $1 -type f -name "*.$2"

It sounds like the question came from some kind of assignment, and I imagine that this original code might fulfill the assignment's expectations. It will probably also work with most programs you will write, which is especially important if the question did not come from an assignment.

I often develop on Windows or Cygwin, so I run into paths and filenames that have spaces. If I use @Viktor's code on Cygwin (note that the same behavior happens when I use Viktor's code on linux)

$ pwd

#Write the program
$ vim

#view the program
$ cat

find $1 -type f -name "*.$2"

#Create a path with spaces in one of the directory names
#Name it after Viktor, for the great answer
CORP+dblack@CAP-D-ENG-INT3 ~
$ mkdir -p viktor/dir\ with\ spaces/directory

#put some css files in that directory
$ touch viktor/dir\ with\ spaces/directory/a.css

$ touch viktor/dir\ with\ spaces/directory/b.css

$ touch viktor/dir\ with\ spaces/directory/c.css

# run Viktor's code, using the well-written instructions
$ /bin/bash ./ 'victor/dir with spaces/directory' css
find: ‘viktor/dir’: No such file or directory
find: ‘spaces/directory’: No such file or directory

Note that I used ./ rather than plain, because I had not set to be executable. I can run it as Viktor shows if I first run

$ chmod +x

The problem comes from the way a bash shell and the find command handle spaces. With the first argument given simply as $1, the spaces aren't treated a special way. That's probably not the best way to describe things. Perhaps a better way of explaining this concept is to show that bash will assign the following.

$1='viktor/dir with spaces/directory'

This will happen even if we tell bash that our directory has spaces

$ /bin/bash viktor/dir\ with\ spaces/directory css
find: ‘viktor/dir’: No such file or directory
find: ‘spaces/directory’: No such file or directory

This can be seen by adding the lines, echo "\$0 is $0" , echo "\$1 is $1" , echo "\$2 is $2" , and further with other lines having $(insert_number_here_but_no_parentheses), if you'd like.

So, how do we fix this problem? Let's look at the man page for find

$ man find

# (press 'q' to get out of the display of the man page)

Okay, that was kind of scary.

Let's cut the original synopsis line down to

find  [starting-point...] [expression]

What the DESCRIPTON means in its 'starting-point' explanation is that [starting-point...] is a directory (or directories) from which you'll start your search. For now, just take it at face value that 'expression' is something that restricts the search -- in your case, you look for only files and then further filter the results by specifying that they have a certain file-extension.

Let's get back to the find $1 -type f -name "*.$2" command. The main point to know here is that the command as shown here will be expanded to

find viktor/dir with spaces/directory -type f -name "*.css"

The "space problem" is that it will be interpreted by find and by the shell as follows

find victor/dir with spaces/directory -type f -name "*.css"
 |     \         |      \             `--------------------'
 |      \        |       \              /
 |       |       |        \       Another '[expression]' (2)
 |       |       |         -- Another '[starting-point]'
 |       |        -- '[expression]' (1)
 |        -- '[starting-point]' (supposedly a directory)
  -- Command name

Note that our '[expression]' (1) and '[expression]' (2) don't factor into what happens because the two '[starting-place]' paths fail.

What's now shown in Viktor's code


find "$1" -type f -name "*.$2"

Leads to

find "viktor/dir with spaces/directory" -type f -name "*.css"
`--' `--------------------------------' `-------------------'
 |               |                             /
 |               |             valid '[expression]' to define
 |               |             a "filter" for files outputted
 |               |
 |                -- '[starting-point]' (a real directory)
 |                    bash knows to include the spaces because
  -- command name                    of the quotation marks

Let's run it, first making sure it's executable

$ chmod +x

$ /bin/bash ./ css

$ /bin/bash 'viktor/dir with spaces/directory' css
viktor/dir with spaces/directory/a.css
viktor/dir with spaces/directory/b.css
viktor/dir with spaces/directory/c.css

How do I perform an action on all files with a specific extension in , The canonical way is to do find . -name '*.jpg' -exec echo {} \;. (replace \; with + to pass more than one file to echo at a time). or (GNU specific, though some BSDs  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" \)

It doesn’t look like you are using arguments here.

You should replace extension and directory with $1 and $2 to use arguments 1 and 2.

Also, it looks like you forgot to close that quote... and the braces around the directory look wrong... I think you need quotes around the braces.

(Can’t test the result unfortunately at the minute, sorry).

command line, Just press Ctrl + Alt + T on your keyboard to open Terminal. When it opens, run the command below: find . -type f -name "*.txt". This will list all  3. Search String in Specific Files. You can search string in files matching the file name criteria. Below example command will search string “tecadmin” in files ending with .log extension in /var/log directory and its sub-directories. grep -rlw --include="*.log" -e "tecadmin" /var/log 4. Exclude Some Files from Search

A big collection of Unix/Linux 'find' command examples , Linux/Unix FAQ: Can you share some Linux find command examples? -type f -​not -name "*.html" # find all files not ending in ".html" find files by text through the /usr/local directory for files that end with the extension .html. The first command returns a list of all files in the entire file system that end with the characters conf and have been modified in the last 7 days. The second command filters exampleuser user’s home directory for files with names that end with the characters conf and have been modified in the previous 3 days.

Use the Unix find command to search for files, To use the find command, at the Unix prompt, enter: To match all files ending in .txt except the file notme.txt , use: \! -name notme.txt -name \*. locate command – find files by name. It reads one or more databases created by updatedb and writes file names matching at least one of the PATTERNs to the screen, one per line. This may not contains file created within last 12-24 hrs. find command – search for files in a directory hierarchy in real time. locate command in bash.

Find Files in Linux, Using the Command Line, Use the Find command from the Linux command line to locate files in a Use find from the command line to locate a specific file by name or extension. a list of all files in the entire file system that end with the characters conf  The -name expression lets you search for the name of a file or folder. There are, however, other expressions you can use: -amin n: The file was last accessed n minutes ago. -anewer: The file was last accessed more recently than it was modified. -atime n: The file was last accessed more n days ago.

  • You have an unmatched quote. What is the * at the end for?
  • You are right. I updated my script to wrap the directory in double quotes so it takes space input correctly.
  • This is a great answer, and it should be said that answers that build on other answers are very welcome on Stack Overflow; the CC license conditions explicitly allow it. There is no need to put any disclaimers about this in the post, nor to direct people how to vote.
  • Thanks @halfer for helping me to understand some of the meta stuff, and thanks for editing.
  • Okay I have changed it to find "$1" -type f -name "*$2" but it doesnt print the files
  • Try referring here, it’s similar but had a slightly more complex requirement. Maybe you need a -print?
  • @JohnHumphreys-w00te -print is the default.
  • @jsmith147 That should work, although I'd write "*.$2", so it requires a . before the extension.
  • @jsmith147 Put set -x at the beginning of the script, so you can see the variable expansions.