Linux Tricks

Audio

ffmpeg is a very powerful command-line tool for working with audio and video. It can convert formats, resize/compress video, extract audio, change codecs, trim clips, combine streams, inspect media files, and much more.

Install on Mac:

brew install ffmpeg

Convert WAV to MP3

ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 output.mp3

Encoding conversion

Convert file from Windows-1252 to UTF-8:

iconv -f WINDOWS-1252 -t UTF-8 input.txt > output.txt  
# Notify satan that someone needs to burn in hell

Encryption

Using age

Better than openssl for daily use, fewer mistakes

age is a modern command-line tool for encrypting files. It was designed as a simpler, safer alternative to using low-level OpenSSL commands or GPG for everyday file encryption.

Install on Mac:

brew install age

Encrypt:

age -p -o secret.age clear.txt

Decrypt:

age -d -o clear.txt secret.age

Using OpenSSL

Encrypt:

# Better legacy OpenSSL usage, but prefer age for new usage
openssl aes-256-cbc -a -salt -pbkdf2 -iter 200000 -md sha256 -in clear.txt -out secret.txt
# BAD DON'T USE
openssl aes-256-cbc -a -salt -in clear.txt -out secret.txt
# BAD DON'T USE (say)
openssl des3 -md md5 -in clear.txt -out secret.txt

Decrypt:

# Better legacy OpenSSL usage, but prefer age for new usage
openssl aes-256-cbc -d -a -salt -pbkdf2 -iter 200000 -md sha256 -in secret.txt -out clear.txt
# BAD DON'T USE
openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
# BAD DON'T USE (say)
openssl des3 -d -md md5 -in secret.txt -out clear.txt

Check whether a remote TCP port is reachable

Corporate firewalls often block outgoing traffic on specific ports (e.g. SSH 22). You can check this with the nc command. There could be other reasons the port is not reachable of course.

# 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

Example with files called IMG_101.jpg, renamed to PHOTO_101.jpg:

for f in IMG*.jpg; do
    mv -- "$f" "PHOTO${f#IMG}"
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):

# Linux
find . -type f -print0 | xargs -0 sed -i 's/foo/bar/g'
# Mac/BSD
find . -type f -print0 | xargs -0 sed -i '' 's/foo/bar/g'

Replace text in a file and write the result to a new file:

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.