Linux Tricks

Encoding conversion

Convert file from Windows-1252 to UTF-8:

iconv -f WINDOWS-1252 -t UTF-8
# Notify satan that someone needs to burn in hell

Encryption

Encrypt:

openssl aes-256-cbc -a -salt -in clear.txt -out secret.txt
# or (say)
openssl des3 -in clear.txt -out secret.txt

Decrypt:

openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
# or (say)
openssl des3 -d -in secret.txt -out clear.txt

Check if port is open in firewall

Corporate firewalls often block outgoing traffic on specific ports (e.g. SSH 22). You can check this with the nc command.

# nc -zv domain/IP port
nc -zv skipperkongen.dk 80
nc -zv sdf.org 22

Will write "Connection to skipperkongen.dk port 80 [tcp/http] succeeded!" if you can access port 80 on skipperkongen.dk from your machine.

Mass rename files

Works if filenames are "regular", i.e. without spaces etc.

# example with files called IMG_101.jpg, renamed to PHOTO_101.jpg
for f in IMG*.jpg; do mv $f $(echo $f | sed -e 's/IMG/PHOTO/g'); done

Create a file using cat

$ cat > myfile.txt
foo
bar
baz
^D
$ cat myfile.txt
foo
bar
baz

Format JSON on commandline with Python

echo '{"a":42, "b": {"x": 127}}' | python -mjson.tool

Search and replace

Multiple files (source):

find . -name "*" -print | xargs sed -i 's/foo/bar/g'

Single file (old sed style)

sed 's/foo/bar/g' foo.txt > bar.txt

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.