栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

有一个文件夹包含相同名称但文件不同的文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

有一个文件夹包含相同名称但文件不同的文件

if [ "$file_hash" == "$a" ];
将哈希与文件名进行比较。你需要类似的东西

if [ "$file_hash" == $(md5sum "$a" | cut -d ' ' -f 1) ];

计算目标文件夹中每个文件的哈希值。

此外,在当前版本中,您的for循环仅运行一次;你需要像

for a in $destination_folder/*

获取该文件夹中的所有文件,而不仅仅是文件夹名称。

根据您的修改,解决方案看起来像

#!/bin/bash -xvfile=$1destination_folder=$2file_hash=`md5sum "$file" | cut -d ' ' -f 1`# test that the filename exists in the destination dirif [[ -f $destination_folder/$file ]] ; then    dest_hash=$(md5sum "$destination_folder/$file" | cut -d ' ' -f 1)    # test that the hash is the same    if [[ "$file_hash" == $curr_hash ]] ; then        cp "$file.JPG" "$destination_folder/$file.JPG"    else         # do nothing    fielse    # destination does not exit, copy file    cp "$file.JPG" "$destination_folder/$file"fi

这不能确保没有重复项。它只是确保具有相同名称的不同文件不会相互覆盖。

#!/bin/bash -xvfile=$1destination_folder=$2file_hash=`md5sum "$file" | cut -d ' ' -f 1`# test each file in destinationfor a in $destination_folder/*do   curr_hash=$(md5sum "$a" | cut -d ' ' -f 1)   if [ "$file_hash" == $curr_hash ];    then       # an identical file exists. (maybe under another name)       # do nothing       exists=1       break   fidoneif [[ $exists != 1 ]] ; then   if [[ -f $destination_folder/$file ]] ; then       cp "$file.JPG" "$destination_folder/$file.JPG"   else        cp "$file.JPG" "$destination_folder"   fifi

未经测试。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号