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:
or "Execute a command with a timeout"
Run a command in background, sleep 10 seconds, kill it.
! is the process id of the most recently executed background command.
You can test it with:
find /& sleep10; kill $!
Need to have rc iso pre-downloaded before running command.
Tested with 9.10 release. Choose whatever torrent client you prefer.
This command takes a snapshot of the open files for a PID 1234 then waits 10 seconds and takes another snapshot of the same PID, it then displays the difference between each snapshot to give you an insight into what the application is doing.
Will track your mouse and save it to a file.
You can use gnuplot to graph it:
gnuplot -persist <(echo "unset key;unset border;unset yzeroaxis;unset xtics;unset ytics;unset ztics;plot './mouse-tracking' with points lt 1 pt 6 ps variable")
You don't want the -ar parameters in this case. The man page for BASH_BUILTINS(1) states:
"-a option means to remove or mark all jobs" and
"-r option without a jobspec argument restricts operation to running jobs"
In this case we are supplying the process id of the job to disown so neither of these should be used.
Just after you type enter, you have 3 seconds to switch window, then "texthere" will be "typed" in the X11 application that has focus. Very useful to beat your score at games such as "How fast can you type A-Z".
Press a key automatically via xvkbd.
Prerequisites: module Pod::Webserver installed. You can install it typing:
sudo perl -MCPAN -e 'install Pod::Webserver'
You can replace elinks with your fav browser. For FF:
podwebserver& sleep 2; firefox -remote 'openurl( http://127.0.0.1:8020/, new-tab )'
If you have Firefox open, this will pop-up the index web in a new tab.
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..