Unix find do znajdowania nazw plików nie kończących się na konkretnych rozszerzeniach?

Czy istnieje prosty sposób na rekurencyjne znajdowanie wszystkich plików w hierarchii katalogów, które nie kończą się na liście rozszerzeń? Np. wszystkie pliki, które nie są *.dll lub*.exe

Unix / GNU find, choć jest potężny, nie wydaje się mieć trybu exclude (lub go brakuje) i zawsze było mi trudno używać wyrażeń regularnych, aby znaleźć rzeczy, które nie pasują do określonego wyrażenia.

Jestem w środowisku Windows (używam GnuWin32 portu większości GNU narzędzia), więc jestem równie otwarty na rozwiązania Tylko Dla Windows.

Author: Jeff Ferland, 2009-08-27

8 answers

Lub bez ( i konieczności ucieczki:

find . -not -name "*.exe" -not -name "*.dll"

Oraz wykluczenie listy katalogów

find . -not -name "*.exe" -not -name "*.dll" -not -type d

Lub w logice pozytywnej; -)

find . -not -name "*.exe" -not -name "*.dll" -type f
 272
Author: Hardy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-12 16:37:14
find . ! \( -name "*.exe" -o -name "*.dll" \)
 41
Author: Chen Levy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-27 14:28:55
$ find . -name \*.exe -o -name \*.dll -o -print

Pierwsze dwie opcje nazw nie mają opcji drukowania, więc zostały pominięte. Wszystko inne jest wydrukowane.

 7
Author: Jeff Ferland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-27 14:29:59

Możesz coś zrobić używając polecenia grep:

find . | grep -v '(dll|exe)$'

Znacznik -v na grep w szczególności oznacza "Znajdź rzeczy, które nie pasują do tego wyrażenia."

 4
Author: VoteyDisciple,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-10 13:30:50

Jeszcze jeden: -)

$ ls -ltr
total 10
-rw-r--r--    1 scripter     linuxdumb         47 Dec 23 14:46 test1
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test4
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test3
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:40 test2
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file5
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file4
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file3
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file2
-rw-r--r--    1 scripter     linuxdumb          0 Jan  4 23:41 file1
$ find . -type f ! -name "*1" ! -name "*2" -print
./test3
./test4
./file3
./file4
./file5
$

Unix find command reference

 2
Author: logic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-04 21:52:46

Linux / OS X:

Począwszy od bieżącego katalogu, rekurencyjnie Znajdź wszystkie pliki kończące się na .dll lub .exe

find . -type f | grep -P "\.dll$|\.exe$"

Począwszy od bieżącego katalogu, rekurencyjnie Znajdź wszystkie pliki, które nie kończą się .dll lub .exe

find . -type f | grep -vP "\.dll$|\.exe$"

Uwagi:

(1) Opcja P w grep wskazuje, że używamy stylu Perla do pisania naszych wyrażeń regularnych, które mają być używane w połączeniu z poleceniem grep . W celu wykonania polecenia grep W w połączeniu z wyrażeniami regularnymi uważam, że styl Perla jest najpotężniejszym stylem w okolicy.

(2) opcja v w grep nakazuje powłoce wykluczenie dowolnego pliku spełniającego Wyrażenie regularne

(3) znak $ na końcu powiedz".dell$ " to znak kontrolny ogranicznika, który informuje powłokę, że ciąg nazwy pliku kończy się ".dll "

 1
Author: Vietnhi Phuvan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-10 16:51:37

Inne rozwiązania na tej stronie nie są pożądane, jeśli masz długą listę rozszerzeń -- utrzymywanie długiej Sekwencji -not -name 'this' -not -name 'that' -not -name 'other' byłoby żmudne i podatne na błędy -- lub jeśli wyszukiwanie jest programowe, a lista rozszerzeń jest budowana w czasie wykonywania.

W takich sytuacjach pożądane może być rozwiązanie, które wyraźniej oddziela dane (listę rozszerzeń) i Kod (parametry do find). Podano strukturę katalogów i plików, która wygląda tak:

.
└── a
    ├── 1.txt
    ├── 15.xml
    ├── 8.dll
    ├── b
    │   ├── 16.xml
    │   ├── 2.txt
    │   ├── 9.dll
    │   └── c
    │       ├── 10.dll
    │       ├── 17.xml
    │       └── 3.txt
    ├── d
    │   ├── 11.dll
    │   ├── 18.xml
    │   ├── 4.txt
    │   └── e
    │       ├── 12.dll
    │       ├── 19.xml
    │       └── 5.txt
    └── f
        ├── 13.dll
        ├── 20.xml
        ├── 6.txt
        └── g
            ├── 14.dll
            ├── 21.xml
            └── 7.txt

Możesz zrobić coś takiego:

## data section, list undesired extensions here
declare -a _BADEXT=(xml dll)

## code section, this never changes
BADEXT="$( IFS="|" ; echo "${_BADEXT[*]}" | sed 's/|/\\|/g' )"
find . -type f ! -regex ".*\.\($BADEXT\)"

Co daje:

./a/1.txt
./a/b/2.txt
./a/b/c/3.txt
./a/d/4.txt
./a/d/e/5.txt
./a/f/6.txt
./a/f/g/7.txt

Możesz zmienić listę rozszerzeń bez zmiany bloku kodu.

Uwaga nie działa z natywnym OSX find - zamiast tego użyj gnu find.

 0
Author: Chris Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-16 17:37:25
find  /data1/batch/source/export   -type f -not  -name "*.dll" -not -name "*.exe"
 0
Author: gwecho huang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-02 04:21:15