3

I want to select (find/grep/ls) certain files in a directory starting with certain words and ending with a certain extension. Example:

  • bluebelt_hans_hoff.jpg
  • bluebelt_hans_hoff.JPEG
  • bluebelt_peter_gort.jpg
  • bluebelt_peter_gort.JPEG

I only want the files starting with "bluebelt" AND ending with only the ".jpg" extension (and not the JPEG extension). How to do this on the commandline?

0

2 Answers 2

3

If you just want to list the files in the terminal, use:

ls -d bluebelt*.jpg

If you want to do something with those files, use:

for file in bluebelt*.jpg; do
    your_command "$file"
done
1
find /dir/with/yor/files -type f -iname 'bluebelt*.jpg'

Advantage of this method over the ls-driven is in that than this method will find a files only while behaviour of the ls may be unexpected. Let's say if you have a dir named as bluebelt1.jpg then the ls utility will output all the files contained in this dir.

Thought this variant has two things. (1) it will search case-insensitively. If you does not want specify -name. (2) it will search recursively. If you does not want you could use the -maxdepth. Please see the man find for explanation.

2
  • Read up on ls -d Dec 23, 2016 at 17:01
  • @roaima, yes, but it still output a dirs etc... Dec 28, 2016 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .