commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
This command will play back each keystroke in a session log recorded using the script command. You'll need to replace the ^[ ^G and ^M characters with CTRL-[, CTRL-G and CTRL-M. To do this you need to press CTRL-V CTRL-[ or CTRL-V CTRL-G or CTRL-V CTRL-M.
You can adjust the playback typing speed by modifying the sleep.
If you're not bothered about seeing each keypress then you could just use:
The '[r]' is to avoid grep from grepping itself. (interchange 'r' by the appropriate letter)
Here is an example that I use a lot (as root or halt will not work):
while (ps -ef | grep [w]get); do sleep 10; done; sleep 60; halt
I add the 'sleep 60' command just in case something went wrong; so that I have time to cancel.
Very useful if you are going to bed while downloading something and do not want your computer running all night.
This is a more accurate way to watch the progress of a dd process. The $DDPID=$! is needed so that you don't get the PID of the sleep. The sleep 1 is needed because in my testing at least, if you run kill -USR1 against dd too quickly, it will kill it off instead of display the status. So you need to wait a second, probably so that it can configure itself to trap the USR1 signal.
The following command will clone usb stick inside /dev/sdc to /dev/sdd
Double check you got the correct usb sticks (origional-clone)with fdisk -l.
Displays a scrolling banner which loops until you hit Ctrl-C to terminate it.
Make sure you finish your banner message with a space so it will loop nicely.
This command can be set as an alias in ~/.bashrc
For example (in my case) I have :
alias watchDl='while [ "$(ls $filePart)" != "" ]; do sleep 5; done; mpg123 /home/.../warning.mp3" '
Then I just need to
- initialize filePart (e.g. filePart="*bigFile*rar.part")
- launch watchDl
If the remote doesn't export its desktop (eg fluxbox, blackbox etc) then you need to run a x11vnc server there and a vncviewer at the local end. This command does the lot for you - it assumes that you can 'ssh' to the box without a password and that x11vnc is installed at the remote end.
The above code is just an example of printing on the same line, hit Ctrl + C to stop
When using echo -ne "something\r", echo will:
- print "something"
- dont print a new line (-n)
- interpret \r as carriage return, going back to the start of the line (-e)
Remember to print some white spaces after the output if your command will print lines of different sizes, mainly if one line will be smaller than the previous
Edit from reading comments: You can achieve the same effect using printf (more standardized than echo): while true; do printf "%-80s\r" "$(date)"; sleep 1; done
Stops when the (last) dd process exits.
Strip my code to:
wmctrl -o 0,0 # autorotates to the first face. In fact [0-1279],0
wmctrl - 1280,0 # goes to the second face
wmctrl -o 2560,0 # goes to the third face, and so on.
# Use multiples of the horizontal display resolution.
My example work for 1280x800 display, been 1280 the number of interest.
Tweak the number, try a biiiig one and see your cube spinning...
I put a complex example to show how fun things can be, even for my ademco and paradox alarm central network advisor interface xpto etc. It rotates two faces, print the alarm message, and goes back tho where it was.
Tested on BIGLINUX 4.2, equivalent to ubuntu LTS hardy.
Do not forget to activate 3D efects ( compiz cube )
Remounts a usb disk /dev/sdb, without having to physically remove and reinsert. (Gnome desktop)
Check out the usage of 'trap', you may not have seen this one much. This command provides a way to schedule commands at certain times by running them after sleep finishes sleeping. In the example 'sleep 2h' sleeps for 2 hours. What is cool about this command is that it uses the 'trap' builtin bash command to remove the SIGHUP trap that normally exits all processes started by the shell upon logout. The 'trap 1' command then restores the normal SIGHUP behaviour.
It also uses the 'nice -n 19' command which causes the sleep process to be run with minimal CPU.
Further, it runs all the commands within the 2nd parentheses in the background. This is sweet cuz you can fire off as many of these as you want. Very helpful for shell scripts.
Very useful for interactive scripts where you would like to return the terminal contents to its original state before the script was run. This would be similar to how vi exits and returns you to your original terminal screen.
Save and clear the terminal contents with:
Execute some commands, then restore the saved terminal contents with:
Mostly for Norwegians, but easily adoptable to others. Very handy if you are brainstorming for a new domainname.
Will only display the available ones..
You can usually do this better with dig, but if you dont have dig, or the TLD only have an online service to check with, this will be usefull..
A little bash daemon
Log a command's votes,
gnuplot -persist <(echo "plot 'votes' with lines")
Very useful in shell scripts because you can run a task nicely in the background using job-control and output progress until it completes.
Here's an example of how I use it in backup scripts to run gpg in the background to encrypt an archive file (which I create in this same way). $! is the process ID of the last run command, which is saved here as the variable PI, then sleeper is called with the process id of the gpg task (PI), and sleeper is also specified to output : instead of the default . every 3 seconds instead of the default 1. So a shorter version would be sleeper $!;
The wait is also used here, though it may not be needed on your system.
echo ">>> ENCRYPTING SQL BACKUP"
gpg --output archive.tgz.asc --encrypt archive.tgz 1>/dev/null &
PI=$!; sleeper $PI ":" 3; wait $PI && rm archive.tgz &>/dev/null
Previously to get around the $! not always being available, I would instead check for the existance of the process ID by checking if the directory /proc/$PID existed, but not everyone uses proc anymore. That version is currently the one at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html but I plan on upgrading to this new version soon.
This will log your internet download speed.
You can run
gnuplot -persist <(echo "plot 'bps' with lines")
to get a graph of it.
[Note: This command needs to be run as root].
If you are downloading something large at night, you can start wget as a normal user and issue the above command as root. When the download is done, the computer will automatically go to sleep. If at any time you feel the computer should not go to sleep automatically(like if you find the download still continuing in the morning), just create an empty file called nosleep in /tmp directory.
Rainbow, instead of greys
I wanted to keep a backup of my company database server on my local homeserver. After I found maatkit to sync databases, everything except security seemed fine. SSH takes care of that part.