Reputation: 2095
I have a requirement to compare files available in a directory and the below code is working fine only when I mention the file names (file1 file2 file3 file4) individually to the filelist
#!/bin/bash
filelist=(file1 file2 file3 file4)
# Outer for loop
for (( i=0; i<${#filelist[@]} ; i+=1 )) ; do
# Inner for loop
for (( j=i+1; j<${#filelist[@]} ; j+=1 )) ; do
echo "Unique between ${filelist[i]}" "${filelist[j]}" > unique${filelist[i]}${filelist[j]}.txt
echo -e "Unique in ${filelist[i]}" >> unique${filelist[i]}${filelist[j]}.txt
# Will produce unique lines in 'file i' when comparing 'file i' and 'file j'
join -v 1 <(sort ${filelist[i]}) <(sort ${filelist[j]}) >> unique${filelist[i]}${filelist[j]}.txt
echo -e "Unique in ${filelist[j]}" >> unique${filelist[i]}${filelist[j]}.txt
# Will produce unique lines in 'file j' when comparing 'file i' and 'file j'
join -v 2 <(sort ${filelist[i]}) <(sort ${filelist[j]}) >> unique${filelist[i]}${filelist[j]}.txt
done
done
But what exactly I want is to assign the files that are available in a directory to the filelist directly. Any working solution please?
Upvotes: 1
Views: 220
Reputation: 143
If your running the script from current directory you can use following code
fileArr=( $(ls) ) #list the files and store in array
echo ${#fileArr[@]}
echo "${fileArr[@]}"
for i in "${fileArr[@]}"
do
echo $i #read the fil
done
Edited : Without using ls
shopt -s nullglob
fileArr1=(*)
fileArr2=(file*)
fileArr3=(dir/*)
echo ${#fileArr1[@]}
echo "${fileArr1[@]}"
for i in "${fileArr1[@]}"
do
echo $i
done
echo ${#fileArr2[@]}
echo "${fileArr2[@]}"
for i in "${fileArr2[@]}"
do
echo $i
done
echo ${#fileArr3[@]}
echo "${fileArr3[@]}"
for i in "${fileArr3[@]}"
do
echo $i
done
Upvotes: 0
Reputation: 5753
Assuming your script work on current directory, use this:-
filelist=(*)
It will get all files in directory via globbing. Don't use ls
.
All the answers here except mine are wrong. (Out of first 3 till now.)
Simple test:
mkdir -p /tmp/test && cd test && touch List\ s && filelist=($(/bin/ls)) && shuf -e "${filelist[@]} | wc -l
mkdir -p /tmp/test && cd test && touch List\ s && filelist=(*) && shuf -e "${filelist[@]} | wc -l
It should give 1 in both cases. But it won't.
Upvotes: 0
Reputation: 416
try this code:
#!/bin/bash
#set basedir to . if no dir is given
if [ -n "$1" ]; then basedir=$1; else basedir=$(pwd);fi
filelist=($(/bin/ls $basedir))
# Outer for loop
for (( i=0; i<${#filelist[@]} ; i+=1 )) ; do
# Inner for loop
for (( j=i+1; j<${#filelist[@]} ; j+=1 )) ; do
echo "Unique between $basedir/${filelist[i]}" "$basedir${filelist[j]}" > $basedir/unique${filelist[i]}${filelist[j]}.txt
echo -e "Unique in $basedir/${filelist[i]}" >> $basedir/unique${filelist[i]}${filelist[j]}.txt
# Will produce unique lines in 'file i' when comparing 'file i' and 'file j'
join -v 1 <(sort $basedir/${filelist[i]}) <(sort $basedir/${filelist[j]}) >> $basedir/unique${filelist[i]}${filelist[j]}.txt
echo -e "Unique in $basedir/${filelist[j]}" >> $basedir/unique${filelist[i]}${filelist[j]}.txt
# Will produce unique lines in 'file j' when comparing 'file i' and 'file j'
join -v 2 <(sort $basedir/${filelist[i]}) <(sort $basedir/${filelist[j]}) >> $basedir/unique${filelist[i]}${filelist[j]}.txt
done
done
The folder should not contain directories. But its working ether. The unique files will created in folder given to the script.
Upvotes: 1