16 Cool Ass Random Linux Stuff

$ echo “Hello world”
Hello world

Urfix has compiled a sweet ass list of random stuff you can do from the command line. Hope you enjoy.

Fry: Make up some feelings and tell her you have them.

1 Multiple variable assignments from command output in BASH

read day month year <<< $(date +'%d %m %y')

This version uses read instead of eval.

2 Generate an XKCD #936 style 4 word password

shuf -n4 /usr/share/dict/words | tr -d '\n'

4 random words are better than one obfuscated word
http://xkcd.com/936/

3 All IP connected to my host

netstat -lantp | grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -u

find all computer connected to my host through TCP connection.

4 Ask for a password, the passwd-style

read -s -p"Password: " USER_PASSWORD_VARIABLE; echo

You can ask repeatedly for a non-blank password using this function:
function read_password() {
while [ ! -n “$USER_PASSWORD” ]; do
read -s -p”Password: ” USER_PASSWORD
if [ ! -n “$USER_PASSWORD” ]; then
echo “ERROR: You must specify a valid password, please try again”
fi
echo
done
}
Also you can set a time out (in seconds) to write the password
read -t 10 -s -p”Password: ” USER_PASSWORD_VARIABLE
if [ ! $? -eq 0 ]; then
echo “Time out!”
fi

5 Check syntax for all PHP files in the current directory and all subdirectories

find . -name \*.php -exec php -l "{}" \;

6mkdir & cd into it as single command

mkdir /home/foo/doc/bar && cd $_

The biggest advantage of this over the functions is that it is portable.

7 Merge PDFs into single file

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf input1.pdf input2.pdf ...

8 Create a QR code image in MECARD format

qrencode -o myqr.png 'MECARD:N:Daprophet,Isaiah;TEL:8881234567;EMAIL:isaiah.daprophet@urfix.com;;'

Add the QR code image on your webpage, business card ., etc, so people can scan it and quick add to their Contact Address Book. Tested on iPhone with QRreader.

9 Chmod all directories (excluding files)

find public_html/ -type d -exec chmod 755 {} +

+ at the end means that many filenames will be passed to every chmod call, thus making it faster. And find own {} makes sure that it will work with spaces and other characters in filenames.

10 List only the directories

find . -maxdepth 1 -type d | sort

11 copy with progress bar – rsync

rsync -rv   --progress

-r for recursive (if you want to copy entire directories)
src for the source file (or wildcards)
dst for the destination
–progress to show a progress bar

12 Find all files of a type and copy them elsewhere while keeping intact their full directory structure using find and cpio

find . -iname "*.flac" | cpio -pdm /Volumes/Music/FLAC

.flac is the filetype.
/Volumes/Music/FLAC is the destination.

13 Share a screen session

screen -x 

14 Backup entire system through SSH

ssh -C USER@HOST tar -c --exclude /proc --exclude /sys / | tar -x

15 Find the package a command belongs to on debian-based distros

apt-file search iostat

16 View files opened by a program on startup and shutdown

sudo lsof -rc command >> /tmp/command.txt

Run this before you run a command in order to see what the command does as it starts.
The -c flag is useful here as the PID is unknown before startup.
All config files, libraries, logs, ports, etc used by the command as it starts up, (and shuts down) will be captured at 1s intervals and written to a file.
Useful for debugging etc.

Leave Comment

Your email address will not be published. Required fields are marked *

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