Compare script using awk and bash I try to create compare script using awk and another command and running successful.
But I think the script I create it's to long.
Is there anyone can shorten my script below ?
After plan A shorten the code than, plan B is :
 1. I want eliminated a lot temp file (.txt), only need `lengkap.txt`
 2. Put command in variable if can

It's my pleasure if anyone can help me
Below is the code that I create based searching and trying.
#!/bin/bash

### Path Folder who will be compare ###
path1=/home/rio/apps1
path2=/home/rio/apps2

### Find all filename and convert to MD5 ###
find $path1 -type f | xargs md5sum &gt; checksums.md5
find $path2 -type f | xargs md5sum &gt; checksums2.md5

### Compare to find different folder ###
awk 'NR==FNR{c[$1]++;next};c[$1] == 0' checksums.md5 checksums2.md5 &gt; hasil1.txt
awk 'NR==FNR{c[$1]++;next};c[$1] == 0' checksums2.md5 checksums.md5 &gt; hasil2.txt

### Merge result of compare ###
awk '{print $0}' hasil1.txt hasil2.txt &gt; perbedaan.txt

### Filter Just Filename Difference ###
cat perbedaan.txt | awk '{print $2}' &gt; hasilperbedaan.txt

### File about result compare (just filename) ###
cekhasil=/home/rio/hasilperbedaan.txt

### Check if File result compare empty or not ###
if [ -s &quot;$cekhasil&quot; ]
then 
   echo &quot; file exists and is not empty &quot;
    ### Find All filename and date, after that put as we want ###
    find $path1 -type f -ls | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }' &gt; filedate1.txt
    find $path2 -type f -ls | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }' &gt; filedate2.txt

    ### Compare to get the date of filename ###
    awk 'A[$1]++' hasilperbedaan.txt filedate1.txt &gt; pre_hasil1.txt
    awk 'A[$1]++' hasilperbedaan.txt filedate2.txt &gt; pre_hasil2.txt
    ### Merge result of compare with date ###
    awk '{print $0}' pre_hasil1.txt pre_hasil2.txt &gt; lengkap.txt
else
   echo &quot; file does not exist, or is empty &quot;
fi
Well, it took time to figure out what your program is doing. So I did shorten it in two phases.
First phase: I removed all duplicate temporary files, and used a pipe when a temp file was used once.
STEP1: you just make a checksum of all files in $path1 and $path2
### Find all filename and convert to MD5 ###
find $path1 -type f | xargs md5sum &gt; checksums.md5
find $path2 -type f | xargs md5sum &gt; checksums2.md5

You don't need 2 temp files, as path is included in filename. So you can replace this with a single find. I use sort to be able to use uniq afterwards:
find &quot;$path1&quot; &quot;$path2&quot; -type f | xargs md5sum  | sort &gt; cksum.md5

STEP2: you find unique checksums in both files, and get the corresponding filenames.
### Compare to find different folder ###
awk 'NR==FNR{c[$1]++;next};c[$1] == 0' checksums.md5 checksums2.md5 &gt; hasil1.txt
awk 'NR==FNR{c[$1]++;next};c[$1] == 0' checksums2.md5 checksums.md5 &gt; hasil2.txt

### Merge result of compare ###
awk '{print $0}' hasil1.txt hasil2.txt &gt; perbedaan.txt

### Filter Just Filename Difference ###
cat perbedaan.txt | awk '{print $2}' &gt; hasilperbedaan.txt

As we have a checksum-sorted file, we just filter with uniq, and get filenames.
Note: As we use a MD5 checksum (128 bits), length is 128bits / 8bits * 2hex=32
uniq -u -w32 cksum.md5 | awk '{print $2}' &gt; &quot;$cekhasil&quot;

If you prefer awk to uniq, just compare previous record's $1 with current's.
STEP 3: you match found entries with a new search (find) in the two source paths.
if [ -s &quot;$cekhasil&quot; ]
then
    echo &quot; file exists and is not empty &quot;
    ### Find All filename and date, after that put as we want ###
    find $path1 -type f -ls | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }' &gt; filedate1.txt
    find $path2 -type f -ls | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }' &gt; filedate2.txt

    ### Compare to get the date of filename ###
    awk 'A[$1]++' hasilperbedaan.txt filedate1.txt &gt; pre_hasil1.txt
    awk 'A[$1]++' hasilperbedaan.txt filedate2.txt &gt; pre_hasil2.txt
    ### Merge result of compare with date ###
    awk '{print $0}' pre_hasil1.txt pre_hasil2.txt &gt; lengkap.txt
else
    echo &quot; file does not exist, or is empty &quot;
fi

Here, it is better to avoid new full find, and just loop over filenames.
if [ -s &quot;$cekhasil&quot; ]
then
    echo &quot; file exists and is not empty &quot;
    while read -r fn ; do
        ls -dils &quot;$fn&quot; | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }'
    done &lt; &quot;$cekhasil&quot; &gt; lengkap2.txt

else
    echo &quot; file does not exist, or is empty &quot;
fi

Alltogether, the script becomes:
#!/bin/bash

path1=/home/br/dev/tools/bash
path2=/home/br/dev/tools/bash2
outfile=hasilperbedaan.txt

find &quot;$path1&quot; &quot;$path2&quot; -type f | xargs md5sum  | sort &gt; cksum.md5

uniq -u -w32 cksum.md5 | awk '{print $2}' &gt; &quot;$outfile&quot;

if [ -s &quot;$outfile&quot; ]; then
    echo &quot; file exists and is not empty &quot;
    while read -r fn ; do
        ls -dils &quot;$fn&quot; | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }'
    done &lt; &quot;$output&quot; &gt; lengkap2.txt

else
    echo &quot; file does not exist, or is empty &quot;
fi

At this point, we can notice the 2 remaining temp files are used only once. We just get rid of them, and just pipe all together to get the output file.
    #!/bin/bash
    
    path1=/home/br/dev/tools/bash
    path2=/home/br/dev/tools/bash2
    outputfile=&quot;lengkap-v2.txt&quot;
    
    find &quot;$path1&quot; &quot;$path2&quot; -type f | xargs md5sum  | sort |
        uniq -u -w32 | awk '{print $2}' |
        while read -r fn ; do
            ls -dils &quot;$fn&quot; | awk '{print $11&quot; &quot;$8&quot; &quot;$9&quot; &quot;$10 }'
        done &gt; &quot;$outputfile&quot;
    [[ -s &quot;$outputfile&quot; ]] &amp;&amp; echo &quot; file exists and is not empty &quot; ||
            echo &quot; file does not exist, or is empty &quot;

Notes:

I don't understand your second question: &quot;Put command in variable if
can&quot;, so I skip that one.
You should never use the output of find (except with -print0 options) or ls as input of any command. But it is out of scope for this answer.