Find files by file type linux

Recursively look for files with a specific extension

I’m trying to find all files with a specific extension in a directory and its subdirectories with my Bash (Ubuntu 10.04 LTS (Lucid Lynx) release). This is what’s written in a script file:

#!/bin/bash directory="/home/flip/Desktop" suffix="in" browsefolders () for i in "$1"/*; do echo "dir :$directory" echo "filename: $i" # echo $ extension=`echo "$i" | cut -d'.' -f2` echo "Erweiterung $extension" if [ -f "$i" ]; then if [ $extension == $suffix ]; then echo "$i ends with $in" else echo "$i does NOT end with $in" fi elif [ -d "$i" ]; then browsefolders "$i" fi done > browsefolders "$directory" 
[: 29: in: unexpected operator 

(with $extension instead of ‘in’ ) What’s going on here, and where’s the error? But this curly brace.

10 Answers 10

find "$directory" -type f -name "*.in" 

is a bit shorter than that whole thing (and safer. It deals with whitespace in filenames and directory names).

Your script is probably failing for entries that don’t have a . in their name, making $extension empty.

@flip: that’s a different question. Post a new question, detailing exactly what you’d like to do and what you’ve tried so far.

One little correction: use ‘*.in’ or \*.in instead of «*.in» because double quotes don’t prevent shell expansion. I.e. your script will not work properly if there’s a file with .in extension in the current directory.

find -type f -name '*.extension' 

Example: To find all csv files in the current directory and its sub-directories, use:

The syntax I use is a bit different than what Mat suggested:

find $directory -type f -name \*.in 

Matt’s script also won’t work if there’s a file with .in extension in the current directory, while yours would still work. See stackoverflow.com/questions/5927369/…

@gniourf_gniourf You should provide some reference for your statement, otherwise one could simply argue: «No, you are wrong». But in fact you’re right: gnu.org/software/bash/manual/html_node/Double-Quotes.html

@user1885518: I think that it should be the guy who claims that the script doesn’t work who should provide some examples where the script fails. That’s what I do when I leave comments where there are broken scripts: it’s usually about quotes and filenames containing spaces, newlines, globs, etc., and I specifically explain why it’s broken.

Providing reference is always a good way in a discussion, it does not depend on who was the first. He should, you should.

du -a $directory | awk '' | grep '\.in$' 

The grep isn’t really necessary here. awk has regular expressions and could limit its output to values matching a pattern.

Читайте также:  Linux удаление файлов старше файла

This method is extremely useful if your going through 100s of terabyte. Find command takes too much time to process. This starts immediately.

Though using find command can be useful here, the shell itself provides options to achieve this requirement without any third party tools. The bash shell provides an extended glob support option using which you can get the file names under recursive paths that match with the extensions you want.

The extended option is extglob which needs to be set using the shopt option as below. The options are enabled with the -s support and disabled with he -u flag. Additionally you could use couple of options more i.e. nullglob in which an unmatched glob is swept away entirely, replaced with a set of zero words. And globstar that allows to recurse through all the directories

shopt -s extglob nullglob globstar 

Now all you need to do is form the glob expression to include the files of a certain extension which you can do as below. We use an array to populate the glob results because when quoted properly and expanded, the filenames with special characters would remain intact and not get broken due to word-splitting by the shell.

For example to list all the *.csv files in the recursive paths

The option ** is to recurse through the sub-folders and *.csv is glob expansion to include any file of the extensions mentioned. Now for printing the actual files, just do

Using an array and doing a proper quoted expansion is the right way when used in shell scripts, but for interactive use, you could simply use ls with the glob expression as

This could very well be expanded to match multiple files i.e. file ending with multiple extension (i.e. similar to adding multiple flags in find command). For example consider a case of needing to get all recursive image files i.e. of extensions *.gif , *.png and *.jpg , all you need to is

This could very well be expanded to have negate results also. With the same syntax, one could use the results of the glob to exclude files of certain type. Assume you want to exclude file names with the extensions above, you could do

excludeResults=() excludeResults=(**/!(*.jpg|*.gif|*.png)) printf '%s\n' "$" 

The construct !() is a negate operation to not include any of the file extensions listed inside and | is an alternation operator just as used in the Extended Regular Expressions library to do an OR match of the globs.

Note that these extended glob support is not available in the POSIX bourne shell and its purely specific to recent versions of bash . So if your are considering portability of the scripts running across POSIX and bash shells, this option wouldn’t be right.

Читайте также:  Astra linux файл репозитория

Источник

Команда find в Linux – мощный инструмент сисадмина

Иногда критически важно быстро найти нужный файл или информацию в системе. Порой можно ограничиться стандартами функциями поиска, которыми сейчас обладает любой файловый менеджер, но с возможностями терминала им не сравниться.

Команда find – это невероятно мощный инструмент, позволяющий искать файлы не только по названию, но и по:

Данная команда будет очень полезна системным администраторам для:

Команда find в Linux производит поиск файлов и папок на основе заданных вами критериев и позволяет выполнять действия с результатами поиска.

Синтаксис команды find:

$ find directory-to-search criteria action
  • directory-to-search (каталог поиска) – это отправной каталог, с которой find начинает поиск файлов по всем подкаталогам, которые находятся внутри. Если не указать путь, тогда поиск начнется в текущем каталоге;
  • criteria (критерий) – критерий, по которым нужно искать файлы;
  • action (действие) – что делать с каждым найденным файлом, соответствующим критериям.

Поиск по имени

Следующая команда ищет файл s.txt в текущем каталоге:

  • . (точка) – файл относится к нынешнему каталогу
  • -name – критерии по которым осуществляется поиск. В данном случае поиск по названию файла.

В данном случае критерий -name учитывает только символы нижнего регистра и файл S.txt не появиться в результатах поиска. Чтобы убрать чувствительность к регистру необходимо использовать –iname.

$ find . -iname "s.txt" ./s.txt ./S.txt

Для поиска всех изображений c расширением .png нужно использовать шаблон подстановки *.png:

$ find . -name "*.png" ./babutafb.png ./babutafacebook.png ./Moodle2.png ./moodle.png ./moodle/moodle1.png ./genxfacebook.png

Можно использовать название каталога для поиска. Например, чтобы с помощью команды find найти все png изображения в каталоге home:

$ find /home -name "*.png" find: `/home/babuta/.ssh': Permission denied /home/vagrant/Moodle2.png /home/vagrant/moodle.png /home/tisha/hello.png find: `/home/tisha/testfiles': Permission denied find: `/home/tisha/data': Permission denied /home/tisha/water.png find: `/home/tisha/.cache': Permission denied

Если выдает слишком много ошибок в отказе разрешения, тогда можно добавить в конец команды – 2> /dev/null. Таким образом сообщения об ошибках будут перенаправляться по пути dev/null, что обеспечит более чистую выдачу.

find /home -name "*.jpg" 2>/dev/null /home/vagrant/Moodle2.jpg /home/vagrant/moodle.jpg /home/tisha/hello.jpg /home/tisha/water.jpg

Поиск по типу файла

Критерий -type позволяет искать файлы по типу, которые бывают следующих видов:

  • f – простые файлы;
  • d – каталоги;
  • l – символические ссылки;
  • b – блочные устройства (dev);
  • c – символьные устройства (dev);
  • p – именованные каналы;
  • s – сокеты;

Например, указав критерий -type d будут перечислены только каталоги:

$ find . -type d . ./.ssh ./.cache ./moodle

Поиск по размеру файла

Допустим, что вам необходимо найти все большие файлы. Для таких ситуаций подойдет критерий -size.

  • «+» — Поиск файлов больше заданного размера
  • «-» — Поиск файлов меньше заданного размера
  • Отсутствие знака означает, что размер файлов в поиске должен полностью совпадать.

В данном случае поиск выведет все файлы более 1 Гб (+1G).

$ find . -size +1G ./Microsoft_Office_16.29.19090802_Installer.pkg ./android-studio-ide-183.5692245-mac.dmg

Единицы измерения файлов:

Поиск пустых файлов и каталогов

Критерий -empty позволяет найти пустые файлы и каталоги.

$ find . -empty ./.cloud-locale-test.skip ./datafiles ./b.txt . ./.cache/motd.legal-displayed

Поиск времени изменения

Критерий -cmin позволяет искать файлы и каталоги по времени изменения. Для поиска всех файлов, измененных за последний час (менее 60 мин), нужно использовать -60:

$ find . -cmin -60 . ./a.txt ./datafiles

Таким образом можно найти все файлы в текущем каталоге, которые были созданы или изменены в течение часа (менее 60 минут).

Читайте также:  Изменить версию java linux

Для поиска файлов, которые наоборот были изменены в любое время кроме последнего часа необходимо использовать +60.

Поиск по времени доступа

Критерий -atime позволяет искать файлы по времени последнего доступа.

Таким образом можно найти файлы, к которым не обращались последние полгода (180 дней).

Поиск по имени пользователя

Опция –user username дает возможность поиска всех файлов и каталогов, принадлежащих конкретному пользователю:

$ find /home -user tisha 2>/dev/null

Таким образом можно найти все файлы пользователя tisha в каталоге home, а 2>/dev/null сделает выдачу чистой без ошибок в отказе доступа.

Поиск по набору разрешений

Критерий -perm – ищет файлы по определенному набору разрешений.

Поиск файлов с разрешениями 777.

Операторы

Для объединения нескольких критериев в одну команду поиска можно применять операторы:

Например, чтобы найти файлы размером более 1 Гбайта пользователя tisha необходимо ввести следующую команду:

$ find /home -user tisha -and -size +1G 2>/dev/null

Если файлы могут принадлежать не только пользователю tisha, но и пользователю pokeristo, а также быть размером более 1 Гбайта.

$ find /home \( -user pokeristo -or -user tisha \) -and -size +1G 2>/dev/null

Перед скобками нужно поставить обратный слеш «\».

Действия

К команде find можно добавить действия, которые будут произведены с результатами поиска.

  • -delete — Удаляет соответствующие результатам поиска файлы
  • -ls — Вывод более подробных результатов поиска с:
    • Размерами файлов.
    • Количеством inode.

    -delete

    Полезен, когда необходимо найти и удалить все пустые файлы, например:

    Перед удалением лучше лишний раз себя подстраховать. Для этого можно запустить команду с действием по умолчанию -print.

    -exec:

    Данное действие является особенным и позволяет выполнить команду по вашему усмотрению в результатах поиска.

    • command – это команда, которую вы желаете выполнить для результатов поиска. Например:
      • rm
      • mv
      • cp

      С помощью –exec можно написать альтернативу команде –delete и применить ее к результатам поиска:

      Другой пример использования действия -exec:

      $ find . -name "*.jpg" -exec cp <> /backups/fotos \;

      Таким образом можно скопировать все .jpg изображения в каталог backups/fotos

      Заключение

      Команду find можно использовать для поиска:

      • Файлов по имени.
      • Дате последнего доступа.
      • Дате последнего изменения.
      • Имени пользователя (владельца файла).
      • Имени группы.
      • Размеру.
      • Разрешению.
      • Другим критериям.

      С полученными результатами можно сразу выполнять различные действия, такие как:

      Команда find может сильно облегчить жизнь системному администратору, а лучший способ овладеть ей – больше практиковаться.

      image

      Источник

Оцените статью
Adblock
detector