我想从目录结构中获取文件名、创建日期、修改日期和文件 mime 类型。我制作了一个脚本,内容如下:
#!/bin/bash
output="file_list.csv"
## columns
echo '"File name";"Creation date";"Modification date";"Mime type"' > $output
## content
find $1 -type f -printf '"%f";"%Tc";"%Cc";"no idea!"\n' >> $output
这给了我令人鼓舞的结果:
"File name";"Creation date";"Modification date";"Mime type"
"Exercice 4 Cluster.xlsx";"ven. 27 mars 2020 10:35:46 CET";"mar. 17 mars 2020 19:14:18 CET";"no idea!"
"Exercice 5 Bayes.xlsx";"ven. 27 mars 2020 10:36:30 CET";"ven. 20 mars 2020 16:18:54 CET";"no idea!"
"Exercice 3 Régression.xlsx";"ven. 27 mars 2020 10:36:46 CET";"mer. 28 août 2019 17:21:10 CEST";"no idea!"
"Archers et Clustering.xlsx";"ven. 27 mars 2020 10:37:34 CET";"lun. 16 mars 2020 14:12:05 CET";"no idea!"
...
但我错过了一件重要的事情:如何获取文件 mime-types ?如果我可以在使用“find”命令找到的每个文件上链接命令“mimetype -b”,并将其写入方便的列中,那就太好了。
提前致谢,
西里尔
最佳答案
您可以尝试使用 -exec
find命令的选项,其中括号{}
代表当前文件的名称。
然后,您可以在附加到现有文件时删除新行:据我所知,默认行为会自动将新内容附加到新行,因此不需要 \n
。
最后,您希望在 mimetype 之后有一个结束引号,因此您不仅应该使用 -b
选项,还应该使用 --output-format
其一,这将使您能够更好地控制要显示的内容。
因此,脚本的第三个命令应如下所示:
find $1 -type f -printf '"%f";"%Tc";"%Cc";"' -exec mimetype --output-format %m\" {} \; >> $output
关于linux - 如何链接 'mimetype -b' 和 'find' 命令以获取同一 csv 中的文件名和文件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62800345/