Notes‎ > ‎


Clear the garbage on your screen after trying to view binary files or such
$ echo ^v^o
$ reset

Press ctrl+r in bash to reverse search command history; ctrl+r again to cycle
$ ^r

Erase a word
$ ^w

Runs a command with the last argument of the previous command
$ cmd !$

Run a previous command in including all params but with sudo
$ sudo !!

Runs the previous command replacing the last argument
$ !:- new_argument

Change to last working directory
$ cd -

View the system console remotely
# fold -w 80 /dev/vcs1

Return alom console

Return to the baytech console
; 5 times

Colorize log files
$ tail -f /var/log/messages | ccze -A
$ tail -f FILE | perl -pe 's/KEYWORD/\e[1;31;43m$&\e[0m/g'

Colorize grep in less
$ grep --color=always | less -R

Bash ternary operator
$ true && { echo sucess; } || { echo failed; }
[[ test_condition ]] && { if_true_do_this; and this; } || { otherwise_do_that; }

Validates and pretty-prints the json content from the url 
$ curl -s "" | python -m json.tool | less -R

Using process substitution, we can 'trick' tee into sending a command's STDOUT to an arbitrary number of commands. The last command (command4) in this example will get its input from the pipe
$ some_command | tee >(command1) >(command2) >(command3) ... | command4

Destroy the MBR
# dd if=/dev/zero of=/dev/sda bs=446 count=1

Disable fsck on critical servers
# tune2fs -c 0 -i 0 /dev/mapper/VolGroup00-LogVol00

Extract file direct from URL without saving to disk
$ wget -qO - "" | tar zxvf -

Renames files with spaces to underscores
$ for file in *;do mv $file ${file// /_};done

Edit remote files
$ vim scp://username@host//path/to/somefile

Remote and local diff
$ vimdiff scp://username@host//path/to/file /path/to/file

Share file through HTTP port 80
$ nc -w 5 -v -l -p 80 < file.ext

Tail a logfile over the network on port 1234
$ tail -F /var/log/messages | nc -l 1234

Kills the process locking the file
$ fuser -k filename

Compares directories recursively
$ diff -urp dir1/ dir2/

Diff output of two commands
$ diff <(tail -10 file1) <(tail -10 file2)

Split long file in files with same number of lines
$ split -l LINES -d file.txt output_prefix

Converting camelCaseWords to under_score_words and vice versa
$ sed 's/\([A-Z]\)/_\l\1/g' file.txt | sed 's/^_\([a-z]\)/\1/g'
$ sed 's/_\([a-z]\)/\u\1/g' file.txt | sed 's/^\([a-z]\)/\u\1/g'

Download from Rapidshare Premium using wget
$ wget -c -t 1 --load-cookies ~/.cookies/rapidshare

Delete files from a directory 30 days old efficiently and takes care of spaces in filenames
$ find /path -type f -print0 | xargs -0 -r rm

Delete mass files quickly in linux
$ mkdir /tmp/test && rsync --delete-before -a -H -v --progress --stats /tmp/test/ target/

Check permissions of each directory to a file
$ namei -l /path/to/file.txt

Cat backwords
$ tac file

Randomize lines in file
$ shuf file.txt

Create empty file of given size
$ fallocate -l 1G test.img

Greps for a process minus the grep line
$ ps -ef | grep [p]rocessname

Gets the number of lightweight processes
$ ps -o nlwp `pgrep <processname>`

Keep programs running after leaving ssh session
$ nohup ./ &

Run a command for a limited time
$ timeout 10s ./

Assign a process to a CPU
# taskset -c <cpu_id> -p <pid>

Find the process causing high throughput
# iftop <- tool to find the connection with top throughput
# netstat -ap <- get the process that has the connection open
# netstat -anp <- in case our hostname from iftop has no forward lookup, we'll get the ip

Check resources' usage of command
$ /usr/bin/time -v ls

Free memory on a linux host
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

Flush swap patition
# swapoff -a
# swapon -a

Change passwd non-interactive
echo 'newpasswd' | passwd --stdin user

Find a PATTERN and create betweenPATTERNhere
$ sed -e 's/pattern/between&here/g' filename