A nice chunk of useful tips for unix:
Bash
- In bash,
ctrl-r
searches your command history as you type. - Input from the commandline as if it were a file by replacing
command < file.in
withcommand <<< "some input text"
: ^
is a sed-like operator to replace chars from last command:
ls docs; ^docs^web^
is equal tols web
.
The second argument can be empty.!!:n
selects the nth argument of the last command, and!$
the last arg:ls file1 file2 file3; cat !!:1-2
shows all files and cats only 1 and 2.nohup ./long_script &
to leave stuff in background even if you logout.cd -
to change to the previous directory you were working on.Ctrl-x Ctrl-e
opens an editor to work with long or complex command lines.- Add
set editing-mode vi
in your ~/.inputrc to use the vi keybindings for bash and all readline-enabled applications (python, mysql, etc).
Pseudo aliases for commonly used long commands
function lt() { ls -ltrsa "$@" | tail; }
function psgrep() { ps axuf | grep -v grep | grep "$@" -i --color=auto; }
function fname() { find . -iname "*$@*"; }
Tools
htop
instead oftop
.ranger
is a nice console file manager for vi fans.- Use
apt-file
to see which package provides that file you`re missing. dict
is a commandline dictionary.trash-cli
sends files to the trash instead of deleting them forever.sort | uniq
to check for duplicate lines.echo start_backup.sh | at midnight
starts a command at the specified time.- Pipe any command over
column -t
to nicely align the columns. diff --side-by-side fileA.txt fileB.txt | pager
to see a nice diff.- learn to use
pushd
to save time navigating folders. - run jobs in parallel easily:
ls \*.png | parallel -j4 convert {} {.}.jpg
.
Networking
python -m SimpleHTTPServer 8080
shares all the files in the current folder over HTTP, port 8080.ssh -R 12345:localhost:22 server.com "sleep 1000; exit"
forwards server.com's port 12345 to your local ssh port, even if you machine is not externally visible on the net. Now you canssh localhost -p 12345
from server.com and you will log into your machine.sleep
avoids getting kicked out from server.com for inactivity.socat TCP4-LISTEN:1234,fork TCP4:192.168.1.1:22
forwards your port 1234 to another machine`s port 22. Very useful for quick NAT redirection.lsof -i
monitors network connections in real time.iftop
shows bandwith usage per connection.nethogs
shows the bandwith usage per process.- Pipe a compressed file over ssh to avoid creating large temporary .tgz files:
tar cz folder/ | ssh server "tar xz"
or even better, usersync
. - ssmtp can use a Gmail account as SMTP and send emails from the command line:
echo "Hello, User!" | mail user@domain.com
Configure your /etc/ssmtp/ssmtp.conf:
root=<email>
mailhub=smtp.gmail.com:587
rewriteDomain=
hostname=smtp.gmail.com:587
UseSTARTTLS=YES
UseTLS=YES
AuthUser=<email>
AuthPass=<password>
AuthMethod=LOGIN
FromLineOverride=YES
-~-
(CC) by-nc, Carles Fenollosa carles.fenollosa@bsc.es
Retrieved from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
Last modified: lun 11 mar 2013 05:13:37 CET
Taken from http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt