如何在 Linux 中使用 md5sum 创建递归文件列表并输出到 csv [英] How do I create a recursive file list with md5sum in Linux and output to csv

查看:48
本文介绍了如何在 Linux 中使用 md5sum 创建递归文件列表并输出到 csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出 Ubuntu 目录和子目录中的文件(最好使用 md5sum),并将结果输出到 csv 文件.我希望输出采用以下格式.

文件名、文件路径、文件大小(字节)、创建日期时间(dd/mm/yyyy hh:mm:ss)、修改日期时间(dd/mm/yyyy hh:mm:ss)、md5sum

我玩过 ls 命令,但似乎可以得到正确的输出.有没有更好的方法来做到这一点?

谢谢

解决方案

创建以下脚本,为给定的文件路径参数输出 CSV 行:

#!/bin/bash设置 -eu文件路径=$1qfilepath=${filepath//\\/\\\\} # 引用反斜杠.qfilepath=${qfilepath//\"/\\\"} # 引用双引号.file=${qfilepath##*/} # 删除路径.stats=($(stat -c "%s %W %Y""$filepath"))大小=${stats[0]}ctime=$(date --date @${stats[1]}"+'%d/%m/%Y %H:%M:%S')mtime=$(date --date @${stats[2]}"+'%d/%m/%Y %H:%M:%S')md5=$(md5sum <$文件路径")md5=${md5%% *} # 删除破折号.printf '%s"、%s"、%s、%s、%s、%s\n' \$文件"$qfilepath"$size"$ctime"$mtime"$md5

现在调用它

find/path/to/dir -type f -exec ~/csvline.sh {} \;

请注意,文件系统通常不支持创建时间.

I would like to list the files (ideally with an md5sum) within a directory and subdirectories in Ubuntu and output the results to a csv file. I would like the output to be in the following format.

File Name, File Path, File Size (bytes), Created Date Time (dd/mm/yyyy hh:mm:ss), Modified Date Time (dd/mm/yyyy hh:mm:ss), md5sum

I have played around with the ls command but can seem to get the output correct. Is there a better way to do this?

Thanks

解决方案

Create the following script that outputs a CSV line for a given filepath argument:

#!/bin/bash
set -eu

filepath=$1
qfilepath=${filepath//\\/\\\\}   # Quote backslashes.
qfilepath=${qfilepath//\"/\\\"}  # Quote doublequotes.

file=${qfilepath##*/}            # Remove the path.

stats=($(stat -c "%s %W %Y" "$filepath"))
size=${stats[0]}
ctime=$(date --date @"${stats[1]}" +'%d/%m/%Y %H:%M:%S')
mtime=$(date --date @"${stats[2]}" +'%d/%m/%Y %H:%M:%S')

md5=$(md5sum < "$filepath")
md5=${md5%% *}                   # Remove the dash.

printf '"%s","%s",%s,%s,%s,%s\n' \
    "$file" "$qfilepath" "$size" "$ctime" "$mtime" $md5

Now call it with

find /path/to/dir -type f -exec ~/csvline.sh {} \;

Note that the creation time is often not supported by the file system.

这篇关于如何在 Linux 中使用 md5sum 创建递归文件列表并输出到 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆