Search code examples
bashshellunixlsutilities

Using ls -lR ignore given REGEX


I’m using ls -lR | ... to get my desired output, but now I need something that would force ls to ignore given regex expression. Is it possible to achieve this? I found ls -I and ls --ignore but for some reason I get: illegal option error [macOS Catalina 10.15.3]. I need this to be working on most frequently used shells(bash, ksh, zsh etc..)

Output

ls -lR | ...

./NOEMPTY:
total 72
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 10.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 15.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 20.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 25.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 40.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 5.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b.c

./NOT_THIS:
total 32
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c

./THIS_YES:
total 32
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c

Desired Output:

ls -lR IGNORE NOT_THIS | ... something more
./NOEMPTY:
total 72
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 10.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 15.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 20.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 25.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 40.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 5.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b.c

./THIS_YES:
total 32
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c

I’ve tried to use sed utility but failed to achieve this, since I wasn’t able to select empty line between folders such as this: ls -lR | sed -n '/.\/NOT_THIS/,/[[:space:]]/p'


Solution

  • Try this : ls -lR -I '*NOT_THIS*'

    Demo :

    :=>ls -1
    1.txt
    2.txt
    a.csv
    b.csv
    :=>ls -1 -I '*txt'
    a.csv
    b.csv
    :=>
    

    Using SED

    sed '/\.\/NOT_THIS/, /^$/'d

    explanation : sed '/start/,/end/d' <-- this will delete all data between first /<patern>/ and second pattern.

    ^$ <-- Blank line. ^ -- Start of record. $ -- end of record

    Demo:

    :=>sed '/\.\/NOT_THIS/, /^$/'d file.txt
    ls -lR | ...
    
    ./NOEMPTY:
    total 72
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 10.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 15.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 20.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 25.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 40.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 5.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b.c
    
    ./THIS_YES:
    total 32
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30 2.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 30.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35 2.c
    -rw-r--r--@ 1 makaveli_10  staff  34 Mar 15 09:26 1b copy 35.c
    :=>