jrara
jrara

Reputation: 16981

How to fetch certain filenames using grep?

I have files like this in a certain directory:

my@unix:~/kys$ ls
address_modified_20130312.txt   customer_rows_full_20131202.txt
customer_full_20131201.txt      customer_rows_modified_20131202.txt
customer_modified_20131201.txt
my@unix:~/kys$ 

I want to use grep to fetch certain filenames which begin with a word "customer". I tried this

my@unix:~/kys$ ls | grep customer.*
customer_full_20131201.txt
customer_modified_20131201.txt
customer_rows_full_20131202.txt
customer_rows_modified_20131202.txt
my@unix:~/kys$

But this gives me these customer_rows.* files which I don't want. The correct result set is

customer_full_20131201.txt
customer_modified_20131201.txt

How to achieve this?

Upvotes: 0

Views: 97

Answers (4)

tripleee
tripleee

Reputation: 189357

With Bash extended globbing, you could say

ls customer_!(rows)*

or, more likely, something like

for f in customer_!(rows)*; do
    : something with "$f"
done

With POSIX shell or traditional Bourne, you could say

for f in customer_*; do
    case $f in customer_rows* ) continue ;; esac
    : something with "$f"
done

Upvotes: 0

hwnd
hwnd

Reputation: 70722

Using grep

ls -1 | grep "^customer_[^r].*$"

Using the find command

find . \! -iname "customer_rows*"

Upvotes: 1

Barmar
Barmar

Reputation: 780851

Use grep -v to filter out what you don't want.

ls customer* | grep -v '^customer_rows'

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can try:

ls customer_[fm]*

or

ls customer_[^r]*

Upvotes: 1

Related Questions