~/linux-reading-files

Reading Files

CPTS Notes6 min read

cat, less, head and tail print files. cut and awk pick columns, sort and uniq order and dedupe lines, and paste joins them. Chain them with | to turn scan output and system files into lists.

cat, less, head, and tail

Print a whole file:

cat /etc/passwd # whole file to the screen

Page through a long file (q quits, / searches):

less /var/log/auth.log # scroll through the file

First or last lines of a file:

head -n 20 file # first 20 lines
tail -n 20 file # last 20 lines

Follow a file as it grows:

tail -f /var/log/syslog # print new lines as they arrive
Flag Meaning
-n Number of lines to print
-f Keeps printing lines as the file grows

cut

Prints selected fields or characters from each line.

Usernames from /etc/passwd:

cut -d: -f1 /etc/passwd # field 1, split on colons

Usernames and home folders:

cut -d: -f1,6 /etc/passwd # fields 1 and 6

Port numbers from nmap’s 22/tcp open ssh lines:

grep "open" scan.nmap | cut -d/ -f1 # text before the first slash

The first 200 characters of each line (long minified lines):

grep -rn "api_key" /var/www 2>/dev/null | cut -c1-200 # api_key hits, cut to 200 characters
Flag Meaning
-d Field delimiter (default is TAB)
-f Fields to print: 1, 1,6, 2-4, 3-
-c Characters to print, same list format as -f

cut treats each delimiter as a field break, and two spaces in a row make an empty field. Squeeze the spaces first with tr -s (see Editing Files), or use awk.

awk

Splits each line into columns. These examples use it for columns only.

The first column (split on spaces and tabs):

awk '{print $1}' access.log # column 1

Usernames from /etc/passwd:

awk -F: '{print $1}' /etc/passwd # column 1, split on colons

Users with a login shell, and their home folders:

awk -F: '$NF ~ /sh$/ {print $1, $6}' /etc/passwd # shell ends in sh: user and home

Regular users (UID 1000 and up):

awk -F: '$3 >= 1000 {print $1}' /etc/passwd # users with UID 1000 or higher

The last column of each line:

awk '{print $NF}' file.txt # last column
Flag Meaning
-F Field separator (default is any run of spaces and tabs)
Variable Meaning
$1, $2 … Column 1, column 2 …
$0 The whole line
$NF The last column

awk splits on any run of spaces by default. Use it over cut on output with aligned columns, such as ps aux or ss -tulpn.

sort and uniq

sort orders lines. uniq deletes or counts repeated lines next to each other.

Sort and drop duplicates (dedupe a wordlist):

sort -u users.txt -o users.txt # dedupe the file in place

Sort numbers and IP addresses:

sort -n ports.txt # numeric order
sort -V hosts.txt # IP order

Count each value, most frequent first:

awk '{print $1}' access.log | sort | uniq -c | sort -rn | head # top 10 values by count

Sort /etc/passwd by UID:

sort -t: -k3 -n /etc/passwd # field 3 as a number

Lines that appear more than once, or only once:

sort users.txt | uniq -d # repeated lines
sort users.txt | uniq -u # unique lines
Flag Meaning
sort -u Drop duplicate lines
sort -n Sort as numbers
sort -r Reverse the order
sort -V Version sort: puts 10.0.0.3 before 10.0.0.20
sort -t Field separator for -k
sort -k n Sort on field n
sort -o file Write the result to file (it can be the input file)
uniq -c Prefix each line with its count
uniq -d Print only the repeated lines
uniq -u Print only the lines that appear once

uniq compares only neighbouring lines. Run sort first.

paste

Joins lines side by side or into one line.

Join all lines into one comma list (ports for nmap -p):

paste -sd, ports.txt # all lines as one comma list

Pair two lists line by line as user:pass:

paste -d: users.txt passwords.txt # line n of each file, joined by a colon
Flag Meaning
-s Join all lines of one file into a single line
-d Delimiter between joined items (default is TAB)

Pipelines

Open ports from a grepable nmap scan as one comma list, then scan them in depth:

# open ports as a comma list
ports=$(grep -oE '[0-9]+/open' scan.gnmap | cut -d/ -f1 | sort -un | paste -sd,)
nmap -sC -sV -p$ports $TARGET # deep scan of those ports

-sC runs the default scripts and -sV reads service versions.

Live hosts from a ping sweep into a target list:

# up hosts from the sweep, saved to hosts.txt
nmap -sn 10.129.2.0/24 -oG - | grep "Status: Up" | awk '{print $2}' | tee hosts.txt

-sn pings without a port scan, and -oG - writes grepable output to the pipe.

Users with a login shell as a wordlist:

grep -E 'sh$' /etc/passwd | cut -d: -f1 | tee users.txt # login-shell users to users.txt

user:hash pairs from /etc/shadow for cracking (locked accounts left out):

sudo awk -F: '$2 ~ /^\$/ {print $1":"$2}' /etc/shadow > hashes.txt # hashes that start with $

Merge wordlists, lowercase, and dedupe:

cat list1.txt list2.txt | tr 'A-Z' 'a-z' | sort -u > merged.txt # one lowercase list, no duplicates

Password strings in a web root, one short line per hit:

grep -rniE 'pass(word)?\s*=' /var/www 2>/dev/null | cut -c1-200 | sort -u # pass= and password= assignments

grep is in Searching Files, and tr and tee in Editing Files. More leads to search for are in Local Enumeration and Privesc Primer.