Есть ли команда, которая позволяет узнать, содержит ли файл что-то?
Я попробовал параметр ls 'size (ls -lsh), но он не показывает мне пустые файлы, потому что файлы / папки не имеют размера.
Вы можете использовать test с -f и ! -s, которые возвратят 0 в случае существующего пустого обычного файла или 1 в противном случае:
-f FILE
FILE exists and is a regular file
-s FILE
FILE exists and has a size greater than zero
test -f file -a ! -s file && printf 'File exists and is empty.\n'
[d1 ] Или используя более распространенный синтаксис: [ -f file -a ! -s file ] && printf 'File exists and is empty.\n'
$ touch empty
$ printf '\n' >non_empty
$ test -f file -a ! -s empty && printf 'File exists and is empty.\n'
File exists and is empty.
$ test -f file -a ! -s non_empty && printf 'File exists and is empty.\n'
$
Вы можете добавить функцию к ~/.bashrc для удобства:
is_empty() { test -f file -a ! -s file && printf 'File exists and is empty.\n'; }
Или используя более распространенный синтаксис:
is_empty() { [ -f file -a ! -s file ] && printf 'File exists and is empty.\n'; }
$ touch empty
$ printf '\n' >non_empty
$ is_empty empty
File exists and is empty.
$ is_empty non_empty
$
Вы также можете использовать stat:
$ touch file
$ stat --format '%F' file
regular empty file
$ echo 'a' > file
$ stat --format '%F' file
regular file
... или это:
find /dir/to/file -maxdepth 1 -name file_name -empty