bash: перебор файлов в каталоге

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

for file in /etc/config/*; do wc -l $file; stat -c %s $file; done

Можно использовать и find для рекурсивного поиска и дополнительных фильтров:

for file in `find /etc -type f -name "*.conf"`
do
   wc -l $file;
   stat -c %s $file;
done

Если выполняется не более одного действия над файлом, можно обойтись без цикла:

find /etc -type f | xargs wc -l

Если в именах файлов есть пробелы, то добавляем к find параметр -print0:

find /etc -type f -print0 | xargs -0 wc -l

Во всех примерах вычисляется количество строк в файлах.

This entry was posted in Разное and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>