0

I need to compare the contents of a file located in dir A with actual files in different directory. ex- directory A has a file test.txt , Item mentioned in test.txt and not present in directory B should be highlighted. im doing something like this but not working.. it is only searching last word from the file test.txt

#!/bin/sh
IFS=$'\n' dirA=$1 dirB=$2 
for x in $(cat < "$1"); do base_name="${x##/}" 
set -- "$dirB"/"$base_name"* 
  if [ -e "$1" ]; then 
    for y; do 
   echo "$base_name found in B as ${y##*/}" done 
  else 
     echo "$x not found in B" fi done.
2
  • I tested this program in my cygwin environment. It works well! Maybe it's bash setting problem?
    – JinChin
    Apr 11, 2019 at 9:54
  • @JinChin Notice that the user is not executing this with bash, and that the code does not include anything that is specific to that shell.
    – Kusalananda
    Apr 11, 2019 at 10:03

2 Answers 2

0

Using diff may do the trick

diff -crs Dir1 Dir2

It will show you if the files are existing, same or different

with a grep on filename might be what you are looking for

4
  • The idea seems to be to verify some sort of manifest over files that are supposed to exist, not to compare two directories.
    – Kusalananda
    Apr 11, 2019 at 9:16
  • i want to check file contents in another directory. example file 1 (located in /user/home) has a,b,c so i need to check if a,b,c is present as files in different folder (/etc)
    – Roops23
    Apr 11, 2019 at 9:17
  • @Kusalananda , yes that is correct, l'm looking for manifest over files that are supposed to exist
    – Roops23
    Apr 11, 2019 at 9:20
  • Ok, sorry, may be read a bit too fast. In that case you could use diff -crs ./file /path_to_file/DIr It also show you where are the differences
    – PhLinuX
    Apr 11, 2019 at 9:52
0
#!/bin/sh

manifest=$1
topdir=$2

while IFS= read -r name; do
    pathname="$topdir/$name"

    if [ -e "$pathname" ]; then
        printf 'Found: %s\n' "$pathname" >&2
    else
        printf 'Not found: %s\n' "$pathname" >&2
    fi
done <"$manifest"

This script takes a manifest file as its first command line argument, and some directory path as its second argument.

It reads lines from the manifest and tests to see if the pathnames corresponding to those lines exists under the given directory.

Would you only want to test the base name of each name read from the file, then use

#!/bin/sh

manifest=$1
topdir=$2

while IFS= read -r name; do
    pathname="$topdir/$( basename "$name" )"

    if [ -e "$pathname" ]; then
        printf 'Found: %s\n' "$pathname" >&2
    else
        printf 'Not found: %s\n' "$pathname" >&2
    fi
done <"$manifest"

Related:

8
  • what is pathname="$topdir/$( basename "$name" )" in the above code.. it is showing opposite result Not found:/Users/dir2/a Not found: /Users/dir2/b Not found: /Users/dir2/c , but a ,b, c are present in dir2 as txt files.
    – Roops23
    Apr 11, 2019 at 9:49
  • @RoopakMurty The line creates a new value that is the directory path that is was given followed by the base name of the line read from the file. If the script says that /Users/dir2/a does not exist, well, then that pathname does not exist. The type of file is not taken into consideration. If you could describe a bit more, then maybe I would understand what you mean.
    – Kusalananda
    Apr 11, 2019 at 9:55
  • @RoopakMurty Are you saying that /Users/dir2/a exists? Do you have spaces at the end of the lines in the file that you pass to the script? In that case, delete those spaces.
    – Kusalananda
    Apr 11, 2019 at 9:57
  • yes a is a text file under /Users/dir2 that exists, actually I'm using git bash in windows to run this script, is this a problem ?
    – Roops23
    Apr 11, 2019 at 10:25
  • installed cygwin, and it works now. :-)
    – Roops23
    Apr 11, 2019 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .