Darkwind
Darkwind

Reputation: 345

How to find directories without dot in bash?

I try to find folders without dot symbol. I Search it in users directory via this script:

!#/bin/bash

users=$(ls /home)

for user in $users;

do

find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -iname "*.*"

done

But I see in result users with folders with dot, for example - test.uk or test.cf

What I do wrong?

Thanks in advance!

Upvotes: 5

Views: 3677

Answers (3)

Tom Fenech
Tom Fenech

Reputation: 74685

No need for find; just use an extended glob to match any files not containing a .

shopt -s extglob
for dir in /home/*/;
do
    printf '%s\n' "$dir"/!(*.*)
done

You could even do away with the loop entirely:

shopt -s extglob
printf '%s\n' /home/*/!(*.*)

To exclude directories in /home that contain a ., you can change /home/*/ to /home/!(*.*)/ in either example.

Upvotes: 0

that other guy
that other guy

Reputation: 123550

The problem is that your command finds directories in /home/username/web/ where the directory doesn't contain a dot.

It does not check to see if username itself contains a dot.

To see if there's a dot anywhere, you can use ipath instead of iname:

!#/bin/bash
users=$(ls /home)
for user in $users;
do
find /home/$user/web/ -maxdepth 1 -type d -iname '*' ! -ipath "*.*"
done

Or more correctly and succinctly:

#!/bin/bash
find /home/*/web/ -maxdepth 1 -type d ! -ipath "*.*"

Upvotes: 0

anubhava
anubhava

Reputation: 785491

You can use find with -regex option for that:

find /home/$user/web/ -maxdepth 1 -type d -regex '\./[^.]*$'

'\./[^.]*$' will match names without any DOT.

Upvotes: 5

Related Questions