简体   繁体   English

循环浏览目录中的所有文件?

[英]Loop through all files in a directory?

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

I'm trying to loop through a file to get each files information to be displayed separately. 我正在尝试遍历文件,以使每个文件的信息分开显示。 eg 例如

first file
name
size
date edited

option to go to next file. 选择转到下一个文件。

second file
name
size
date edited

and so on, at the moment though it's just displaying this: 依此类推,虽然目前仅显示以下内容:

file/dir/file1
file/dir/file2

edit: 编辑:

right, i've looked into this more and now have this. 对,我已经研究了更多,现在有了这个。

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

what this should do is get the total number of files in the directory, then loop through them. 这应该做的是获取目录中文件的总数,然后循环遍历它们。 The problem is i don't know how to get the file information to display on them. 问题是我不知道如何获取文件信息以在其上显示。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM