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

Commands by olorin from sorted by
Terminal - Commands by olorin - 9 results
echo 'hardstatus alwayslastline " %d-%m-%y %c:%s | %w"' >> $HOME/.screenrc; screen
2011-02-16 08:04:56
User: olorin
Functions: echo
4

Configures screen to always display the clock in the last line (has to be configured only once).

After that you not only have got the possibility to detach sessions and run them in background, but also have got a nice clock permanently on your screen.

script -t /tmp/mylog.out 2>/tmp/mylog.time; <do your work>; <CTRL-D>; scriptreplay /tmp/mylog.time /tmp/mylog.out
2011-01-19 07:16:30
User: olorin
Functions: script
23

If you provide the option -t to the script command and redirect stderr into a file, the timing information on what is going on on the terminal, is also stored.

You can replay the session via the scriptreplay command, where you can also provide a speedup factor (see the man page for details).

Great for demonstration purposes ...

ps aux | grep [p]rocess-name
2009-08-13 05:44:45
User: olorin
Functions: grep ps
61

As an alternative to using an additional grep -v grep you can use a simple regular expression in the search pattern (first letter is something out of the single letter list ;-)) to drop the grep command itself.

grep -r . /sys/class/net/eth0/statistics
2009-08-05 08:20:39
User: olorin
Functions: grep
Tags: Linux
4

Within /proc and /sys there are a lot of subdirectories, which carry pseudofiles with only one value as content. Instead of cat-ing all single files (which takes quite a time) or do a "cat *" (which makes it hard to find the filename/content relation), just grep recursively for . or use "grep . /blabla/*" (star instead of -r flag).

For better readability you might also want to pipe the output to "column -t -s : ".

ps -o comm= -p $(ps -o ppid= -p $$)
2009-08-03 07:41:21
User: olorin
Functions: ps
4

Get the name of the parent command. This might be helpful, if you need to react on that information. E. g. a script called directly via ssh has got sshd as parent, manually invoked the parent process will probably be bash

/sbin/kexec -l /boot/$KERNEL --append="$KERNELPARAMTERS" --initrd=/boot/$INITRD; sync; /sbin/kexec -e
2009-08-03 07:36:49
User: olorin
Tags: Linux boot kexec
21

If you are doing some tests which require reboots (e. g. startup skripts, kernel module parameters, ...), this is very time intensive, if you have got a hardware with a long pre-boot phase due to hardware checks.

At this time, kexec can help, which only restarts the kernel with all related stuff.

First the kernel to be started is loaded, then kexec -e jumps up to start it.

Is as hard as a reboot -f, but several times faster (e. g. 1 Minute instead of 12 on some servers here).

lomount -diskimage /path/to/your/backup.img -partition 1 /mnt/foo
4

Instead of calculating the offset and providing an offset option to mount, let lomount do the job for you by just providing the partition number you would like to loop mount.

diff <(echo "$a") <(echo "$b")
2009-07-15 07:26:23
User: olorin
Functions: diff echo
25

You got some results in two variables within your shell script and would like to find the differences? Changes in process lists, reworked file contents, ... . No need to write to temporary files. You can use all the diff parameters you'll need. Maybe anything like $ grep "^>"

is helpful afterwards.

time dd if=/dev/zero of=blah.out oflag=direct bs=256M count=1
2009-07-15 07:17:32
User: olorin
Functions: dd time
0

Let dd use direct I/O to write directly to the disk without any caching. You'll encounter very different results with different block sizes (try with 1k, 4k, 1M, ... and appropriate count values).