Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have many files in a folder for example


From this directory, I want to know how I could use grep to display files based on part of their filename - for example those starting with "Account" or those ending in ".sh".

share|improve this question

migrated from Sep 13 '11 at 19:16

This question came from our site for professional and enthusiast programmers.

Thanks @DMA, I was just gonna do the same, you were a second faster. user663724, if you have similar questions, why not group them in one? – slhck Sep 14 '11 at 10:18
Nice edit, I thought I was looking at a different question at first, until I realised that the title and content had been modified to make the question more useful to a wider audience. – Joe Taylor Sep 14 '11 at 10:25
up vote 3 down vote accepted

You can find

find -type f -name "Account*"

Alternative 2 (this might include folder as well)

ls -1 Account*

Alternative 3 (grep, this could include folder as well)

ls -1 | grep -E "^Account"
share|improve this answer
Thank you , i found this ls -1 Account*" useful . – user663724 Sep 14 '11 at 9:32
I prefer find :-) – ajreal Sep 14 '11 at 10:15

Use a wildcard (What is this wildcard?):

ls *.sh

The * will match anything before the ending .sh that you want. Use the other ls options for displaying, e.g. -l to make it a long listing. See man ls for more info about what you can do with it.

Note that ls would also list any directory ending with .sh, not only files.

You can also use find to overcome this problem.

find . -type f -maxdepth 1 -name "*.sh"

This command only lists "real" files. Using find is something I'd recommend if you want to do something with the found files afterwards. You can do this with the -exec option.

share|improve this answer
For OP: In addition, you can also use ? for a single charector as in "hel?o" to find "hello " and "helwo" – Sirex Sep 14 '11 at 9:43
I would put quotes around *.sh in the find command, as otherwise the shell would expand it to find . -type f -maxdepth 1 -name in the question sample. – BennyInc Sep 14 '11 at 9:58
@BennyB Right, forgot them while copying. Thanks! – slhck Sep 14 '11 at 10:01

Use the following command

   ls *.sh
share|improve this answer

The standard solution is a pipe:

ls | grep ^Account

With something as simple as "Files starting with Account", globs would also work:

ls Account*

but in general a grep is more powerful, and it doesn't run the risk of overflowing the maximum command line length if your folder is really, really full.

share|improve this answer

You must log in to answer this question.

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