How do I compare file names in two directories in shell script?

前端 未结 5 834
有刺的猬
有刺的猬 2021-01-31 05:39

Right now, this is what my code looks like:

#!/bin/bash

Dir1=$1
Dir2=$2

for file1 in $Dir1/*; do
    for file2 in $Dir2/*; do
        if [[ $file1 == $file2 ]]         


        
5条回答
  •  太阳男子
    2021-01-31 06:02

    If you want to know what's common to two directories then this is another way with much less coding.

    #!/bin/bash
    
    comm -12 <(ls -F $1) <(ls -F $2)
    

    See man comm for more information about the comm utility.

提交回复
热议问题