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:
Similarly for last wake time:
sysctl -a | grep waketime
pauses exactly long enough to wake at the top of the hour
Cleaned up and silent with &>/dev/null at the end.
Execute commands serially on a list of hosts. Each ssh connection is made in the background so that if, after five seconds, it hasn't closed, it will be killed and the script will go on to the next system.
Maybe there's an easier way to set a timeout in the ssh options...
For use when you can't use "watch" (user-defined functions, aliases). This isn't mine - its an alternate posted in the comments by flatcap, and is the shortest and easiest to remember.
Displays an animated hourglass for x amount of seconds
the "delay" utility is an invaluable tool for me. with gnu-screen it allows you to schedule something and have it run and output to the current terminal, unlike "at".
You can also use it like "sleep" with seconds and also with date:
delay until 13:33 friday && echo test
(author: Tom Rothamel)
this will open a new tab in firefox for every line in a file
the sleep is removable but i found that if you have a large list of urls 50+, and no sleep, it will try to open all the urls at once and this will cause them all to load a lot slower, also depending on the ram of your system sleep gives you a chance to close the tabs before they overload your ram, removing & >2/dev/null will yield unpredictable results.
Countdown clock - Counts down from $MIN minutes to zero.
I let the date command do the maths.
This version doesn't use seq.
Cycles continuously through a string printing each character with a random delay less than 1 second. First parameter is min, 2nd is max. Example: 1 3 means sleep random .1 to .3. Experiment with different values. The 3rd parameter is the string. The sleep will help with battery life/power consumption.
cycle 1 3 $(openssl rand 100 | xxd -p)
Fans of "The Shining" might get a kick out of this:
cycle 1 4 ' All work and no play makes Jack a dull boy.'
No need to be root to do that. Relies on UPower (previously known as DeviceKit-Power).
It suspends to RAM: you always need your batteries for the RAM but it saves time as there is no need to slowly archive everything on your hard disk.
It works fine with me but if anyone has a nicer way, please contribute.
Another way of counting the line output of tail over 10s not requiring pv.
Cut to have the average per second rate :
tail -n0 -f access.log>/tmp/tmp.log & sleep 10; kill $! ; wc -l /tmp/tmp.log | cut -c-2
You can also enclose it in a loop and send stderr to /dev/null :
while true; do tail -n0 -f access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null
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 $!
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.
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 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.