Mohamed Hussain
Mohamed Hussain

Reputation: 25

loop through a directory and check if it exist in another directory

I have a folder with 20000 files in directory A and another folder with 15000 file in another directory B i can loop through a directory using:

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

I need to loop through all the files in directory A and check if they exist in directory B

I tried the following but it doesn't seem to work:

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 " 

to match the format and loop through each dir and using ls i can find the last version by matching the size and the year and month

Upvotes: 2

Views: 1903

Answers (3)

pjh
pjh

Reputation: 8209

This code is based on the code in the question:

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

Upvotes: 1

Gonca Atabek
Gonca Atabek

Reputation: 36

You can basicly use diff command to compare the files and directories. diff folderA folderB I think you do not really need to use a loop for that..

If really you want to use a loop around, you may want to compare the files as well.

#!/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

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 141873

You need to find files that are common in both A and B directories.

comm -12 \
    <(cd A && find . -type f -maxdepth 1 | sort) \
    <(cd B && find . -type f -maxdepth 1 | sort)

Live version available at tutorialspoint.

  1. How it works? find's list the files in both A and B directories and comm displays only files/lines common in both inputs. comm needs input to be sorted, that's why | sort
  2. Don't parse ls output. ls is for nice, formatted output. Use find . and parse it's output.

Upvotes: 0

Related Questions