Hide

What's this?

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/

Get involved!

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.

Hide

Stay in the loop…

Follow the Tweets.

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

Subscribe to the feeds.

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 11,930 results
while :; do xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$(find <image-directory> -type f -iregex '.*\.\(bmp\|gif\|jpg\|png\)$' | sort -R | head -1)"; sleep 30m; done
2009-04-30 03:09:52
Functions: sleep
Tags: bash find xfce
2

Change your wallpaper every thirty minutes (or however long you like, I suppose) to a randomly selected image in a directory and subdirectories. Bear in mind this is not safe to use if anyone else has write access to your image directory.

sudo vol_id -u /dev/sda1
2009-04-30 01:40:58
User: agentk
Functions: sudo
3

Shows the UUID of a filesystem or partition that can be used in kernel root options and in fstab.

Run it without the -u option to generate more information. eg:

~/ sudo vol_id /dev/sda2

ID_FS_USAGE=other

ID_FS_TYPE=swap

ID_FS_VERSION=2

ID_FS_UUID=27fca13d-97b7-4d28-882c-6d03353f0a82

ID_FS_UUID_ENC=27fca13d-97b7-4d28-882c-6d03353f0a82

ID_FS_LABEL=

ID_FS_LABEL_ENC=

xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s <image-file>
2009-04-30 01:40:52
Tags: xfce
3

Simply changes the wallpaper of xfce4 from the command line. Not for multiple displays.

w | egrep -v '(load|FROM)' | awk '{print $2}' | sed 's/^/tty/' | awk '{print "echo \"The Matrix has you...\" >> /dev/" $1}' | bash
2009-04-29 22:04:56
User: copremesis
Functions: awk egrep sed
-12

This works just like write or wall ... cept one thing the sender is anonymous ... if you really want to drive everyone insane replace echo \"The Matrix has you...\" with cat /dev/urandom

nice one to do on April fool's day

mkdir `date | sed 's/[: ]/_/g'`
2009-04-29 21:28:45
User: copremesis
Functions: mkdir sed
-4

use today's time stamp to make a unique directory for today or an hour ago ...

shopt -s histappend
2009-04-29 19:49:53
4

If histappend options is set in bash, the file .bash_history will not be overwrite and history list is append to it.

touch /etc/nologin
2009-04-29 19:43:14
User: udim
Functions: touch
Tags: login
2

Also with optional message:

echo "no login for you" > /etc/nologin

(This doesn't affect your current X session - you're already logged in!)

pinfo
2009-04-29 19:08:37
9

pinfo package provide a nice info alternative based on ncurses.

ps auxf | grep httpd | grep -v grep | grep -v defunct | awk '{sum=sum+$6}; END {print sum/1024}'
ac -p
2009-04-29 16:24:07
2

Works on CentOS ad OpenBSD too, display time of accounts connection on a system, -p option print individual user's statistics.

(acroread &);sleep 2;gdb /opt/Adobe/Reader8/Reader/intellinux/bin/acroread `pidof ld-linux.so.2`
getfacl /mydir
gethostip 208.69.34.230 -x
gconftool-2 -t string -s /desktop/gnome/background/picture_filename <path_to_image>
2009-04-29 11:19:29
User: cammarin
2

You can use this in a script which rotates wallpapers from a directory at each login.

cuebreakpoints <cue file> | shnsplit -o <lossless audio type> <audio file>
2009-04-29 10:42:57
User: cammarin
4

Do you have an entire album in a unique file and want to split it in individual tracks? If you also have the cue file you can do it!

Packages for Debian-based systems users:

* cuetools shntool

* FLAC (.flac): flac

* WavPack (.wv): wavpack

* Monkey's Audio (.ape): libmac2 monkeys-audio (deb http://www.debian-multimedia.org sid main)

NOTE: "sid" packages are unstable, but I didn't have problems with them. If you prefer, use the "stable" version repository.

To transfer the tags, you can use this (works with .flac, .ogg and .mp3):

cuetag sample.cue split-track*.flac
notify-send ["<title>"] "<body>"
2009-04-29 10:05:20
User: cammarin
45

The title is optional.

Options:

-t: expire time in milliseconds.

-u: urgency (low, normal, critical).

-i: icon path.

On Debian-based systems you may need to install the 'libnotify-bin' package.

Useful to advise when a wget download or a simulation ends. Example:

wget URL ; notify-send "Done"
txt2regex
2009-04-29 04:00:22
User: bwoodacre
Tags: regex
8

txt2regex can be interactive or noninteractive and generates regular expressions for a variety of dialects based on user input. In interactive mode, the regex string builds as you select menu options. The sample output here is from noninteractive mode, try running it standalone and see for yourself. It's written in bash and is available as the 'txt2regex' package at least under debian/ubuntu.

_llines=100; while [ $_llines -gt 1 ]; do echo $_llines; _llines=$(($_llines-1)); done
2009-04-29 00:19:45
User: romulusnr
Functions: echo
-2

You've opened a terminal window and you've connected off to a remote host that didn't pick up your terminal size, and all your curses and paging apps are screwed up as a result. You need to quickly determine how many lines are in your current terminal view (to feed into "stty rows X").

sed -r 's/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g' file.txt
2009-04-28 22:44:45
User: atoponce
Functions: sed
11

Useful for switching over someone else's coding style who uses camelCase notation to your style using all lowercase with underscores.

find . -path ./mnt -prune -o -path ./lost+found -prune -o -path ./sys -prune -o -path ./proc -prune -o -print | cpio -pumd /destination && mkdir /destination/mnt/ && mkdir /destination/proc && mkdir /destination/sys
2009-04-28 22:14:45
User: nutria
Functions: cpio find mkdir
Tags: find cpio
1

Clone linux installation.

find . | cpio -pumdv /destination
2009-04-28 22:08:30
User: nutria
Functions: cpio find
Tags: find cpio
3

Copy every file from current directory to destination preserving modification time.

echo "screen -DR" >> ~/.bash_profile
2009-04-28 20:45:37
User: sud0er
Functions: echo
Tags: screen
11

From screen's manpage: "Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author's favorite."

Toss this in your ~/.bash_profile so that you never have that "oh crap" moment where you wanted to run something in screen and didn't.

shred targetfile
2009-04-28 19:57:43
User: sud0er
Functions: shred
5

GNU shred is provided by the coreutils package on most Linux distribution (meaning, you probably have it installed already), and is capable of wiping a device to DoD standards.

You can give shred any file to destroy, be it your shell history or a block device file (/dev/hdX, for IDE hard drive X, for example). Shred will overwrite the target 25 times by default, but 3 is enough to prevent most recovery, and 7 passes is enough for the US Department of Defense. Use the -n flag to specify the number of passes, and man shred for even more secure erasing fun.

Note that shredding your shell history may not be terribly effective on devices with journaling filesystems, RAID copies or snapshot copies, but if you're wiping a single disk, none of that is a concern. Also, it takes quite a while :)

sudo pmset -a hibernatemode 1
sudo installer -pkg /Volumes/someapp/someapp.mpkg -target /
2009-04-28 19:43:56
User: sud0er
Functions: sudo
Tags: osx
4

Installing most OSX apps is just a matter of dropping it in /Applications, either GUI-wise or with cp -r. However, many packages are distributed in "mpkg" format, and those have to be installed with an installer. If you don't want to go to the trouble of firing up VNC to install an mpkg, you can use the "installer" command.

This will install an application from a .mpkg it to /Applications system-wide. To install a program for just one user, replace "-target /" with "-target username".