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 |
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 -md md5 -in clear.txt -out secret.txt |
openssl aes-256-cbc -a -salt -in clear.txt -out secret.txt
# or (say)
openssl des3 -md md5 -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 -md md5 -in secret.txt -out clear.txt |
openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
# or (say)
openssl des3 -d -md md5 -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 |
# 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 |
# 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 |
$ 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 |
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' |
find . -name "*" -print | xargs sed -i 's/foo/bar/g'
Single file (old sed style)
sed 's/foo/bar/g' foo.txt > bar.txt |
sed 's/foo/bar/g' foo.txt > bar.txt