1820040 Members
3323 Online
109608 Solutions
New Discussion юеВ

file the same file name

 
SOLVED
Go to solution
hangyu
Regular Advisor

file the same file name

I have two set of files one at /tmp/dir1 , another one is /tmp/dir2 , both two directories has 1200 files , some of them are same file name in both directories , some are different file name , if I want to find out which files are the same files name , could suggest what can I do ? thx.


/tmp/dir1
file1
file2
file3
file4
file5



/tmp/dir2
file5
file6
file7
file8
file9

so I would like to find out "file5" as the file are persented at both directories.
6 REPLIES 6
Biswajit Tripathy
Honored Contributor

Re: file the same file name

for file in `ls -1 /tmp/dir1`
do
if [ -a /tmp/dir2/$file ]
then
echo $file
fi
done

- Biswajit
:-)
Vibhor Kumar Agarwal
Esteemed Contributor

Re: file the same file name

ls -1 /tmp/dir1 > list1
ls -1 /tmp/dir2 > list2

grep -f list1 list2 > common_files
Vibhor Kumar Agarwal
Bharat Katkar
Honored Contributor
Solution

Re: file the same file name

Hi,
Vibhor solution looks much easy to use.
Just to put it in script e.g. "script1":

----------------------------
#!/usr/bin/ksh
ls -1 /tmp/dir1 > list1
ls -1 /tmp/dir2 > list2
grep -f list1 list2
-----------------------------

# chmod +x script1
# ./script1 dir1 dir2

Regards,

You need to know a lot to actually know how little you know
Bharat Katkar
Honored Contributor

Re: file the same file name

Sorry for multiple post ...but change mising in cut paste :)

----------------------------
#!/usr/bin/ksh
ls -1 $1 > list1
ls -1 $2 > list2
grep -f list1 list2
-----------------------------
You need to know a lot to actually know how little you know
Kasper Hedensted
Trusted Contributor

Re: file the same file name

Hi,

You can also use dircmp(1)
it will give you 2 lists:

1: a list of files that only exist in either dir1 or dir2

2: a list of files which exist in both dirs

Also it compares the files and will tell you if the files are different or the same

Cheers
Muthukumar_5
Honored Contributor

Re: file the same file name

You can do as,

1) diff -s /tmp/dir1 /tmp/dir2 | grep 'identical'

2) dircmp /tmp/test1 /tmp/test2 | grep 'same'

hth.
Easy to suggest when don't know about the problem!