Collections of Cool Linux Commands
Command HistoryRuns previous command but replacing
^foo^bar
Really useful for when you have a typo in a previous command. Also, arguments default to empty so if you accidentally run: echo "no typozs", you can correct it with ^z
^foo^bar replaces first instance of foo. If you want to replace multiple instances of a string, then you should use: !!:gs/foo/bar
Check command history, but avoid running it
!whatever:p
!whatever will search your command history and execute the first command that matches 'whatever'. If you don't feel safe doing this put :p on the end to print without executing.
Run the last command as root
sudo !!
Make sudo forget password instantly
sudo -K
Execute a command at a given time
echo "ls -lah >/foo/bar/ls.txt" |at 07:00
Execute a command without saving it in the history
Prepending one or more spaces to your command won't be saved in history, useful for pr0n or passwords on the commandline.
Salvage a broken terminal
reset
stty sane
If you bork your terminal by sending binary data to STDOUT or similar, you can get your terminal back using this command
Running scripts after a reboot for non-root users.
$ @reboot
Sometimes we may want to run a script when a system reboots. We can simply do this by just scheduling the script using vixie cron with the @reboot option
File && Directory
Prevent accidents and test your command with echo
echo rm *
Empty a file
> file.txt
List the size (in human readable form) of all sub folders from the current location
du -h --max-depth=1
Like top, but for files
watch -d -n 2 'df; ls -FlAt;'
Remove all but one specific file
rm -f !(survivior.txt)
Quickly backup or copy a file with bash
cp filename{,.bak}
SSH - Remote Server
Copy ssh keys to user@host to enable password-less ssh logins.
ssh-copy-id user@host
Compare a remote file with a local file
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
vimdiff
vimdiff scp://[user@]host1/
Start a tunnel from some machine's port 80 to your local post 2001
ssh -N -L2001:localhost:80 somemachine
Run command on a group of nodes in parallel
echo `date` | pee "ssh host1" "ssh host2" "ssh host3"
Network
Lists all listening ports together with the PID of the associated process
netstat -tlnp
List all open ports and their owning executables
lsof -i -P | grep -i "listen"
Download
Download an entire website
wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com
-p parameter tells wget to include all files, including images.
-e robots=off you don't want wget to obey by the robots.txt file
-U mozilla as your browsers identity.
--random-wait to let wget chose a random number of seconds to wait, avoid get into black list.
Other Useful wget Parameters:
--limit-rate=20k limits the rate at which it downloads files.
-b continues wget after logging out.
-o $HOME/wget_log.txt logs the output
The default recursion depth is now 5, which is often enough to download entire sites. However, if you want to be sure, add a '-l 0' to remove the recursion depth limit.
Utilities
Serve current directory tree at http://$HOSTNAME:8000/
python -m SimpleHTTPServer
To use a different port:
python -m SimpleHTTPServer 8080
twistd -n web --path .
python -m smtpd -n -c DebuggingServer localhost:1025
Capture video of a Linux desktop
ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg
ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg
Create a single PDF from multiple images with ImageMagick
$ convert *.jpg output.pdf
mtr, better than traceroute and ping combined
mtr google.com
Find out my Linux distribution name and version
cat /etc/*-release
cat /proc/version
lsb_release -a
Miscs
Display currently mounted filesystems in nice layout
mount | column -t
A very simple and useful stopwatch
time read (ctrl-d to stop)
Quick access to the ascii table.
man ascii
Clear the terminal screen
ctrl+l, clear
Pretend to be busy in office to enjoy a cup of coffee
$ for i in `seq 0 100`;do timeout 6 dialog --gauge "Install..." 6 40 "$i";done
cat /dev/urandom | hexdump -C | grep "ca fe"
Just make some data scrolling off the terminal.
4 Websites to Learn Cool Linux Command Line Tricks
shell-fu
command-line-fu
snipt
good coders code, great reuse
Resources
http://www.commandlinefu.com