遍历目录中的所有文件?

时间:2015-01-07 15:07:09

标签: linux bash shell loops sh

FILES="$DIRECTORY"/*
for f in $FILES
do
echo "file $f: "
#ls "$f"
done

我正在尝试遍历文件以使每个文件信息单独显示。 e.g。

first file
name
size
date edited

选择转到下一个文件。

second file
name
size
date edited

等等,此刻虽然只是显示了这个:

file/dir/file1
file/dir/file2

编辑:

对,我已经对此进行了更多调查,现在已经有了这个。

total=$(find $DIRECTORY -type f | wc -l)
f="1"
while [ $f -lt $total ]
do
echo "file $f: "
stat %n $f
f=$[$f+1]
done

这应该做的是获取目录中的文件总数,然后循环遍历它们。问题是我不知道如何在其上显示文件信息。

file 0:
stat: cannot stat ‘%n’: No such file or directory
stat: cannot stat ‘0’: No such file or directory
file 1:
stat: cannot stat ‘%n’: No such file or directory
stat: cannot stat ‘1’: No such file or directory

1 个答案:

答案 0 :(得分:0)

如果您只想要名称,大小和mtime,请尝试:

find "$DIRECTORY" -maxdepth 1 -type f -exec sh -c 'stat -c "%n %s %y" "$0"' {} \;
相关问题