遍历一个目录并检查它是否存在于另一个目录中
loop through a directory and check if it exist in another directory
我在目录 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 }')
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 }')
echo "file New Name $FILE$filedate "
匹配格式并循环遍历每个目录并使用 ls 我可以通过匹配大小和年份和月份找到最后一个版本
您需要在 A 和 B 目录中找到 comm打开的文件。
comm -12 \
<(cd A && find . -type f -maxdepth 1 | sort) \
<(cd B && find . -type f -maxdepth 1 | sort)
在线版本可在 tutorialspoint 获得。
- 它是如何工作的? find 列出 A 和 B 目录中的文件,
comm
仅显示两个输入中共有的 files/lines。 comm
需要对输入进行排序,这就是 | sort
的原因
- 不解析
ls
输出。 ls
用于漂亮的格式化输出。使用 find .
并解析它的输出。
你基本上可以使用diff
命令来比较文件和目录。 diff folderA folderB
我认为你真的不需要为此使用循环..
如果你真的想使用循环,你可能还想比较文件。
#!/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
此代码基于问题中的代码:
testdir='/home/oracle/ideatest/test/'
livedir='/home/oracle/ideatest/live/'
shopt -s nullglob # Globs that match nothing expand to nothing
shopt -s dotglob # Globs match files whose names start with '.'
for testpath in "$testdir"*
do
[[ -f $testpath ]] || continue # Skip non-files
testfile=${testpath##*/} # Get file (base) name
printf '%s\n' "$testfile"
livepath=${livedir}${testfile} # Make path to (possible) file in livedir
if [[ -f $livepath ]]
then
printf '%s\n' "$testfile"
echo "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
else
echo "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
fi
done
我在目录 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 }')
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 }')
echo "file New Name $FILE$filedate "
匹配格式并循环遍历每个目录并使用 ls 我可以通过匹配大小和年份和月份找到最后一个版本
您需要在 A 和 B 目录中找到 comm打开的文件。
comm -12 \
<(cd A && find . -type f -maxdepth 1 | sort) \
<(cd B && find . -type f -maxdepth 1 | sort)
在线版本可在 tutorialspoint 获得。
- 它是如何工作的? find 列出 A 和 B 目录中的文件,
comm
仅显示两个输入中共有的 files/lines。comm
需要对输入进行排序,这就是| sort
的原因
- 不解析
ls
输出。ls
用于漂亮的格式化输出。使用find .
并解析它的输出。
你基本上可以使用diff
命令来比较文件和目录。 diff folderA folderB
我认为你真的不需要为此使用循环..
如果你真的想使用循环,你可能还想比较文件。
#!/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
此代码基于问题中的代码:
testdir='/home/oracle/ideatest/test/'
livedir='/home/oracle/ideatest/live/'
shopt -s nullglob # Globs that match nothing expand to nothing
shopt -s dotglob # Globs match files whose names start with '.'
for testpath in "$testdir"*
do
[[ -f $testpath ]] || continue # Skip non-files
testfile=${testpath##*/} # Get file (base) name
printf '%s\n' "$testfile"
livepath=${livedir}${testfile} # Make path to (possible) file in livedir
if [[ -f $livepath ]]
then
printf '%s\n' "$testfile"
echo "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
else
echo "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"
fi
done