我想打印所选目录中的所有文件。我想格式化要在列标题下显示的文件。但是,当我尝试运行以下代码时,我在控制台中遇到错误。
list_files()
{
FILES=/home/student/.junkdir/
echo "Listing files in Junk Directory"
format="%8s%10s%10s $-s\n"
printf "$format" "Filename" "Size(Bytes)" "Type"
printf "$format" "--------" "-----------" "----"
for listed_file in $FILES; do
file_name=$(du $listed_file | awk '{print $2}')
file_size=$(du $listed_file | awk '{print $1}')
file_type=$(file $listed_file | cut -d ' ' -f2-)
printf "$format" $file_name $file_size $file_type
done
}
这是输出
Listing files in Junk Directory
FilenameSize(Bytes) Type hBs
------------------- ---- hBs
du: cannot access ‘/home/student/.junkdir/*’: No such file or directory
du: cannot access ‘/home/student/.junkdir/*’: No such file or directory
ERROR: cannot open hBs
`/home/student/.junkdir/*' (No such hBs
file ordirectory) hBs