Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all directories except one

Tags:

linux

bash

shell

I have a following directory structure

libs logs src etc .........
  |-- logs
  |-- src
  |-- inc

"logs" directory is everywhere inside. So I want to list all directories except "logs". What will be shell command for that.

Something like

#!/bin/bash
for dir in `find * -type d`; do
    if [[ ${dir} != "{logs}*" ]]; then
        echo ${dir}
    fi
done

but this does not seems to be working.

Regards, Farrukh Arshad.

like image 490
Farrukh Arshad Avatar asked Feb 21 '13 14:02

Farrukh Arshad


2 Answers

Rather than trying to process these things one at a time with checks, why don't you get all directories and just filter out the ones you don't want:

find * -type d | egrep -v '^logs/|/logs/'

The grep simply removes lines containing either logs/ at the start or /logs/ anywhere.

That's going to be a lot faster than individually checking every single directory one-by-one.

like image 119
paxdiablo Avatar answered Nov 10 '22 14:11

paxdiablo


As mentioned in the above comment you can use egrep and | to separate patterns or like below define it all in find

 find . -type d -print
.
./logs1
./test
./logs


$ find . -type d -not -name logs -not -name logs1  -print
.
./test
like image 4
V H Avatar answered Nov 10 '22 16:11

V H