gpt4 book ai didi

linux - 遍历一个目录并检查它是否存在于另一个目录中

转载 作者:太空狗 更新时间:2023-10-29 12:09:22 24 4
gpt4 key购买 nike

我在目录 A 和另一个文件夹中有一个包含 20000 个文件的文件夹在另一个目录 B 中有 15000 个文件,我可以循环访问一个目录使用:

DIR='/home/oracle/test/forms1/'

for FILE in "$DIR"*.mp
do
filedate=$( ls -l --time-style=+"date %d-%m-%Y_%H-%M" *.fmx |awk '{print $8 $7}')
echo "file New Name $FILE$filedate "
# echo "file New Name $FILE is copied "
done

我需要遍历目录 A 中的所有文件并检查它们是否存在于目录B中

我尝试了以下方法,但它似乎不起作用:

testdir='/home/oracle/ideatest/test/'
livedir='/home/oracle/ideatest/live/'

for FILET in "$testdir" #
do
testfile=$(ls $FILET)
echo $testfile

for FILEL in "$livedir"
do
livefile=$(ls $FILEL)

if [ "$testfile" = "$livefile" ]
then
echo "$testfile"
echo "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
else
echo "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
fi
done
done

i'am trying to fix the result of years of bad version control we have that very oly script that send a form to live enviorment but every time it's compiled and sent the live version is named like (testform.fmx) but in test dir there is like 10 files named like (testform.fmx01-12-2018) (testform.fmx12-12-2017)(testform.fmx04-05-2016) as a reuslt we lost track of the last source sent to live enviroment that's why i created this

filedate=$( ls -l --time-style=+"date %d-%m-%Y_%H-%M" *.fmx |awk 
'{print $8 $7}')
echo "file New Name $FILE$filedate " 

匹配格式并循环遍历每个目录并使用 ls 我可以通过匹配大小和年月找到最后一个版本

最佳答案

您基本上可以使用diff 命令来比较文件和目录。 差异文件夹A文件夹B我认为你真的不需要为此使用循环..

如果你真的想使用循环,你可能还想比较文件。

#!/bin/bash
DIR1="/home/A"
DIR2="/home/B"
CmpCmn=/usr/bin/cmp
DiffCmn=/usr/bin/diff

for file1 in $DIR1/*; do #Get the files under DIR1 one by one
filex=$(basename $file1) #Get only the name ofthe ile
echo "searching for $filex"
$DiffCmn $filex $DIR2 #Check whether the file is under DIR2 or not
if [ $? -ne 0 ]
then
echo " No file with $filex name under $DIR2 folder"
else
echo " $filex exists under $DIR2"
$CmpCmn $file1 $DIR2/$filex #Compare if the files are exactly same
if [ $? -ne 0 ]
then
echo " $filex is not same"
else
echo " $filex is the same"
fi
fi
done

关于linux - 遍历一个目录并检查它是否存在于另一个目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52603713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com