Oracle Linux 6.4/Bash shell
I have six files as shown below. Using ls/grep (or anything) , I need to list all files which start with the pattern
stomper
but not the ones
which ends with 1.
$ touch stompera
$ touch stomperb
$ touch stomperc
$ touch stompera1
$ touch stomperb1
$ touch stomperc1
$
$
$ ls -l stomper*
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stompera
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stompera1
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperb
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperb1
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperc
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperc1
### This is what I've tried. It isn't working
$ ls -l stomper* | grep -v 1
$
$ ls -l stomper* | grep stomper | grep -v 1
$
Expected output:
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stompera
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperb
-rw-r--r-- 1 stack clsapp 0 Apr 11 09:29 stomperc
try:
ls -l stomper* | grep -v '1$'
1 Like
If you know that there is at least one matching file, you could also try:
ls -l stomper*[[:lower:]]
if you're interested in files starting with stomper
and ending with a lower case alphabetic character:
ls -l stomper*[a-c]
if you want filenames starting with stomper
and ending with a
, b
, or c
, or:
ls -l stomper*[!1]
if you want filenames starting with stomper
and ending with anything other than 1
.
1 Like
Thank you Don. Thanks everyone
One more thing.
I am a beginner in regular expression stuff. I tried to use ^ character to list the file names starting with 1.
It doesn't seem to work. Following is what I've tried
touch stompera
touch stomperb
touch stomperc
touch 1stompera
touch 1stomperb
touch 1stomperc
$ ls -l
total 0
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:15 1stompera
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:15 1stomperb
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:15 1stomperc
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:14 stompera
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:14 stomperb
-rw-r--r-- 1 strp nhuy 0 Apr 12 22:14 stomperc
$ ls -l | grep '1^'
$
$ ls -l | grep '^1'
$
ls -l
prints the filename last, and it prints permissions first. That is why grep '^1'
does not work, because none of the lines start with 1
, they start with -
1 Like
Shi@#$%. Why didn't I think of that ?