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

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