🖥️comm

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the comm command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                 ██████╗ ██████╗ ███╗   ███╗███╗   ███╗
#                ██╔════╝██╔═══██╗████╗ ████║████╗ ████║
#                ██║     ██║   ██║██╔████╔██║██╔████╔██║
#                ██║     ██║   ██║██║╚██╔╝██║██║╚██╔╝██║
#                ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║
#                 ╚═════╝ ╚═════╝ ╚═╝     ╚═╝╚═╝     ╚═╝
                                                       
                                                       
                                                       
                                                      
#Sorts and compare 2 files line by line
comm -12 <(sort -u File1) <(sort -u File2)

comm -12 <(sort names1) <(sort names2)
# Determine what lines two different files have in common. The comm program requires sorted files.

comm -3 <(ls -1 dir1) <(ls -1 dir2)
# Compare the contents of 2 dirs. Show only 2 columns, each for files unique to the directory.

# What speakers are not on the attendees list.
comm -23 <(sort speaker.emails |tr A-Z a-z |uniq) <(sort attendee.emails |tr A-Z a-z |uniq) 

comm -3 <(ls olddir) <(ls newdir) 
# Compare the contents of 2 directories by showing only 2 columns, one for the files unique to each directory.

comm -13 ItemsListtoAvoid.txt AllItemsList.txt > ItemsDifference.txt
# Generate a list of items from a couple of items lists A and B, getting (B - A ) set

comm -13 <(sort emails-registered.txt) <(sort emails-paid.txt) 
# Display a list of email addresses that have paid (supressing column 1 and 3), but not registered. Make sure input it sorted for each list using process output redirection with <( sort file )

comm -23 <(ls -1 wiki/extensions/) <(ls -1 mediawiki-1.31.1/extensions) # Figure out what mediawiki extensions are in the current extensions directory that do not exist in the default distribution.

comm -12 <(sort names1) <(sort names2) 
# Determine what lines two different files have in common. The comm program requires sorted files.

comm -13 <(sort emails-registered.txt) <(sort emails-paid.txt) 
# Display a list of email addresses that have paid (supressing column 1 and 3), but not registered. Make sure input it sorted for each list using process output redirection with <( sort file )

# Print the lines of file2 that are missing in file1
comm -23 file2 file1

# Explanation: The POSIX-standard comm utility can do this also.

    # -2 suppresses lines from the second argument (file1)
    # -3 suppresses lines appearing in both files

# Limitations: Assumes that file1 and file2 are already sorted. If they are not, you can use process substitution to do so:
comm -23 <(sort file2) <(sort file1)
        # Process substitution is a bash-specific feature (also available in zsh but with a different syntax).
 

# utput an arbitrary number of open TCP or UDP ports in an arbitrary range
comm -23 <(seq "$FROM" "$TO") <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n "$HOWMANY"
# Explanation: Originally published (by me) on unix.stackexchange.com. comm is a utility that compares sorted lines in two files. It outputs three columns: lines that appear only in the first file, lines that only appear in the second one and common lines. By specifying -23 we suppress the latter columns and only keep the first one. We can use this to obtain the difference of two sets, expressed as a sequence of text lines. I learned about comm here. The first file is the range of ports that we can select from. seq produces a sorted sequence of numbers from $FROM to $TO. The result is piped to comm as the first file using process substitution. The second file is the sorted list of ports, that we obtain by calling the ss command (with -t meaning TCP ports, -a meaning all - established and listening - and -n numeric - do not try to resolve, say, 22 to ssh). We then pick only the fourth column with awk, which contains the local address and port. We use cut to split address and port with the : delimiter and keep only the latter (-f2). ss also output an header, that we get rid of by grepping for non-empty sequences of numbers that are no longer than 5. We then comply with comm his requirement by sorting numerically (-n) and getting rid of duplicates with uniq.

# Now we have a sorted list of open ports, that we can shuffle to then grab the first "$HOWMANY" ones with head -n. Example: Grab the three random open ports in the private range (49152-65535)
comm -23 <(seq 49152 65535) <(ss -tan | awk '{print $4}' | cut -d':' -f2 | grep "[0-9]\{1,5\}" | sort | uniq) | shuf | head -n 3
        54930
        57937
        51399
# Notes
    # switch -t with -u in ss to get free UDP ports instead.
    # drop shuf if you are not interested in grabbing a random port

# alle zeilen von second-file.tesxt welche nicht maatchen mit irgend einere zeile aus der first-file.txt
comm -23 second-file-sorted.txt first-file-sorted.txt

#==============================##==============================#
# CMD comm						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░