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.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
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:
Requirements:
Output: integer x , 1>=x
Input: hostname
Able to reproduce on the same host
Acceptable for output to be different among OSes (Solaris, Linux, BSD)
Useful for providing DayOfMonth splay in cron jobs. Capped at 28 for Febtober.
I find it useful when I want to add another crontab entry and I need to specify the appropriate PATH.
I give ''whichpath'' a list of programs that I use inside my script and it gives me the PATH I need to use for this script.
''whichpath'' uses associative array, therefore you should have Bash v4 in order to run it.
See sample output.
Another function to stick into your .bashrc
This spits out the time two minutes in the future, but already formatted for pasting into your crontab file for testing without any thought required on your part. Frequently things don't work the way you expect inside a crontab job, and you probably want to find out now that your $PATH is completely different inside of cron or other global variables aren't defined. So this will generate a date you can use for testing now, and then later you can change it to run at 5:37 am on a Sunday evening.
If everything validates, there's no output. Can be handy to run on a cron job set up to email output.
I use this command, within a cron job, to kill XMMS after a certain amount of time. This command returns the PID used by XMMS, and gets passed to the kill command. Another alternative would be ps aux | grep xmms | grep -v grep | awk '{ print $2 }' | xargs kill
This one-liner is for cron jobs that need to provide some basic information about a filesystem and the time it takes to complete the operation. You can swap out the di command for df or du if that's your thing. The |& redirections the stderr and stdout to the mail command.
How to configure the variables.
TOFSCK=/path/to/mount
FSCKDEV=/dev/path/device
or
FSCKDEV=`grep $TOFSCK /proc/mounts | cut -f1 -d" "`
MAILSUB="weekly file system check $TOFSCK "
Changes your desktop background image in gnome. Update the directory to wherever you keep your wallpapers. I like to create a sub-directory in my Wallpaper folder called "cycle" that I use to define the wallpapers I wish to loop in cron. ex:
gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$(find ~/Wallpapers/cycle -type f | shuf -n1)"
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 .
e.g @reboot
I use it to send me an alert message on our prod hosts to send an alert message when the system reboots .
@reboot zaman uptime | echo `uptime` | mail -s "`uname -n` got rebooted" me@myhost.com
The simplest way to do it.
Works for me, at least. (Why are the variables being set?)
we don't need to export variables to set a env to a command, we may do this before the command directly
You can write a script that does this :
remind <minutes> [<message>]
Checks if a web page has changed. Put it into cron to check periodically.
Change http://www.page.de/test.html and mail@mail.de for your needs.
This command will log the output of your simple cronjobs to syslog, and syslog will take it from there. Works great for monitoring scripts which only produce simple output.
Advantages:
* This can be used by regular users, without modifying system files like /etc/syslog.conf
* Reduce cron spam to root@localhost (Please stop spaming the sysadmins)
* Uses common tools like syslog (and logrotate) so that you don't need to maintain yet another krufty logfile.
* Still ensures that the output is logged somewhere, for posterity. Perhaps it's stored the secure, central syslog server, for example.
* Seems to work fine on Ubuntu, CentOS, FreeBSD & MacOSX
This ran on a ubuntu box using espeak for speaking text with the bash shell. On a mac you should use 'say'. Also you can change your alarm interval and your snooze interval which are currently 8 hours and 1 minute. I would run this via cron yet it's easier to disable if you run it as a command like this :P
Can be installed in the root crontab if you want it to update your motd.
If not on ubuntu you need to change /usr/share/cowsay/cows/* to the location of your cow files.
if you need to install cron jobs in a given time range.
I've wanted this for a long time, finally just sat down and came up with it. This shows you the sorted output of ps in a pretty format perfect for cron or startup scripts. You can sort by changing the k -vsz to k -pmem for example to sort by memory instead.
If you want a function, here's one from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
aa_top_ps(){ local T N=${1:-10};T=${2:-vsz}; ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -${T} -A|sed -u "/^ *PID/d;${N}q"; }
A cronjob command line to email someone when a webpages homepage is updated.
Simple way to backup your LDAP entries: put this line on your crontab.
The -n switch identifies the dbnum you want to backup (alternatively you can use -b suffix. Check man slapcat for your personal switches)
Debian-specific but very useful as cron files are prone to very subtle gotchas
The above command will set the GID bit on all directories named .svn in the current directory recursively. This makes the group ownership of all .svn folders be the group ownership for all files created in that folder, no matter the user.
This is useful for me as the subversion working directory on my server is also the live website and needs to be auto committed to subversion every so often via cron as well as worked on by multiple users. Setting the GID bit on the .svn folders makes sure we don't have a mix of .svn metadata created by a slew of different users.
Reports all local partitions having more than 90% usage.
Just add it in a crontab and you'll get a mail when a disk is full.
(sending mail to the root user must work for that)