linux - 如何链接 'mimetype -b' 和 'find' 命令以获取同一 csv 中的文件名和文件类型?

标签 linux bash find mime-types

我想从目录结构中获取文件名、创建日期、修改日期和文件 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/

相关文章:

php - 查找并替换数组中的重复项

regex - 通过 Perl 更改文件名

linux - 系统启动后直接从linux内核运行程序

linux - 我正在尝试在终端中 curl X Post,但我的文件太大

python - 读取 Unicode 文件时出现 UnicodeDecodeError

regex - 使用 find 和正则表达式查找以月份数字开头的文件名

linux - 我如何编译此代码以在 Linux 上使用 LD_PRELOAD?

bash - 根据 "git diff",我如何改进我的重置 Git 文件的解决方案,这些文件显示为已修改但没有更改?

bash - 在 bash 中遍历文件时保留空格

linux - ffmpeg 添加所有音轨到视频转换(mkv)