Linux 循环浏览目录中的所有文件?
我试图循环浏览一个文件,以使每个文件的信息分别显示。e、 gLinux 循环浏览目录中的所有文件?,linux,bash,shell,loops,sh,Linux,Bash,Shell,Loops,Sh,我试图循环浏览一个文件,以使每个文件的信息分别显示。e、 g FILES="$DIRECTORY"/* for f in $FILES do echo "file $f: " #ls "$f" done 选择转到下一个文件 first file name size date edited 以此类推,尽管目前它只是在展示: second file name size date edited 编辑: 对,我已经研究了很多,现在有了这个 file/dir/file1 file/dir/file2
FILES="$DIRECTORY"/*
for f in $FILES
do
echo "file $f: "
#ls "$f"
done
选择转到下一个文件
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
查看
人员统计
$[…]
已过时;使用$(…)
代替。或者,如果find
支持-printf
与所有GNU产品一起使用:find“$DIRECTORY”-maxdepth 1-type f-printf“%f%s%t\n”
。
find "$DIRECTORY" -maxdepth 1 -type f -exec sh -c 'stat -c "%n %s %y" "$0"' {} \;