~/linux-editing-files

Editing Files

CPTS Notes4 min read

sed edits lines in a stream or in place, and tr replaces or deletes characters. tee writes output to a file while you watch it. touch creates files and sets their timestamps.

sed

Edits a stream line by line: substitute, delete, or print.

Replace every match on each line:

sed 's/http:/https:/g' urls.txt # http: to https: on every line

Set your IP in an exploit, in place, and keep a backup:

sed -i.bak 's/10\.10\.10\.10/10.10.14.5/g' exploit.py # swap the IP, keep exploit.py.bak

Print lines 10 to 20:

sed -n '10,20p' /var/log/syslog # lines 10 to 20 only

Delete comments and blank lines:

sed -E '/^\s*(#|$)/d' /etc/ssh/sshd_config # active settings only
Flag Meaning
-n Print nothing unless a p command says so
-i Edit the file in place. -i.bak keeps a copy as file.bak
-E Extended regex
Command Meaning
s/old/new/ Replace the first match on each line
s/old/new/g Replace every match on each line
10,20p Print lines 10 to 20
/regex/d Delete lines that match

A . in the pattern matches any character. Escape it as \. in IPs and hostnames.

tr

Replaces, deletes, or squeezes characters.

Lowercase a wordlist:

tr 'A-Z' 'a-z' < users.txt # uppercase to lowercase

Delete Windows line endings (the bad interpreter: /bin/bash^M error):

tr -d '\r' < script.sh > script_fixed.sh # strip carriage returns

Squeeze repeated spaces into one:

ps aux | tr -s ' ' | cut -d' ' -f1,2,11 # user, PID, and command

Turn a comma list into one item per line:

echo "22,80,445" | tr ',' '\n' # one port per line
Flag Meaning
-d Delete the listed characters
-s Squeeze each run of a repeated character into one

tee

Writes its input to the screen and to a file at the same time.

Save a scan while you watch it:

nmap -sC -sV $TARGET | tee scan.txt # print and save to scan.txt

Append to a file:

gobuster dir -u http://$TARGET -w common.txt | tee -a web.txt # print and append to web.txt

Add a vhost to /etc/hosts as root:

echo "$TARGET site.htb" | sudo tee -a /etc/hosts # map site.htb to the target
Flag Meaning
-a Append to the file and keep its content

A sudo echo redirect into /etc/hosts fails: your shell opens the file for the redirect as your own user. sudo tee -a opens it as root.

touch

Creates empty files and sets file timestamps.

Create files, or update the time of files that exist:

touch notes.txt users.txt # create both, or update their times

Copy the timestamps of another file:

touch -r /usr/bin/passwd /usr/local/bin/helper # helper gets the times of passwd

Set a date:

touch -d "2023-01-15 09:30" shell.php # date as a string
touch -t 202301150930 shell.php # date as a stamp

Change only the modification time:

touch -m -d "2023-01-15 09:30" shell.php # modification time only
Flag Meaning
-r file Use the timestamps of file
-d "date" Use a date string, such as "2023-01-15 09:30" or "yesterday"
-t stamp Use [[CC]YY]MMDDhhmm[.ss]
-a Change only the access time
-m Change only the modification time

touch cannot set the change time (ctime). stat still shows when you touched the file.