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 tagged Linux from sorted by
Terminal - Commands tagged Linux - 225 results
aptitude remove $(dpkg -l|awk '/^ii linux-image-2/{print $2}'|sed 's/linux-image-//'|awk -v v=`uname -r` 'v>$0'|sed 's/-generic//'|awk '{printf("linux-headers-%s\nlinux-headers-%s-generic\nlinux-image-%s-generic\n",$0,$0,$0)}')
2010-12-11 11:38:15
User: __
Functions: awk sed
7

Note the double space: "...^ii␣␣linux-image-2..."

Like 5813, but fixes two bugs: [1]This leaves the meta-packages 'linux-headers-generic' and 'linux-image-generic' alone so that automatic upgrades work correctly in the future. [2]Kernels newer than the currently running one are left alone (this can happen if you didn't reboot after installing a new kernel).

I'm bummed that this took 228 characters. I'd like to see a simpler version.

tar -zxvf file.tar.gz -C /tmp
2010-11-17 14:26:14
User: rdc
Functions: tar
Tags: Linux tar unzip
2

A *.tar.gz file needs to be unzipped & then untarred. Previously I might have unzipped first with

gunzip -d file.tar.gz

and then untarred the result with

tar -xvf file.tar

(Options are extract, verbose, file)

Using the -z (decompress) option on tar avoids the use of gzip (or gunzip) first.

Additionally the -C option will specify the directory to extract to.

for I in `/sbin/lspci |awk '/VGA/{print $1}'`;do /sbin/lspci -v -s $I;done
lspci -v -s `lspci | awk '/VGA/{print $1}'`
2010-10-26 17:45:14
User: infinull
Functions: awk lspci
Tags: Linux hardware
1

probably only works if you have one graphics card.

used this: http://www.cyberciti.biz/faq/howto-find-linux-vga-video-card-ram/

as reference

can be expanded, for example:

lspci -v -s `lspci | awk '/VGA/{print $1}'` | sed -n '/Memory.*, prefetchable/s/.*\[size=\([^]]\+\)\]/\1/p'

will just get the amount of prefetchable memory

compare to:

lshw -C display

which does not give the size (it does give byte ranges and you could calculate the size from that, but that's a pain)

Also uses a command which is not standard on linux; wheras lspci is a core utility provided by most systems

inotifywait -mrq -e CREATE --format %w%f /path/to/dir | while read FILE; do chmod g=u "$FILE"; done
2010-10-21 23:36:02
User: dooblem
Functions: chmod read
6

Listens for events in the directory. Each created file is displayed on stdout. Then each fileline is read by the loop and a command is run.

This can be used to force permissions in a directory, as an alternative for umask.

More details:

http://en.positon.org/post/A-solution-to-the-umask-problem%3A-inotify-to-force-permissions

Check the Description below.
2010-10-07 04:22:32
User: hunterm
-1

The command was too long for the command box, so here it is:

echo $(( `wget -qO - http://i18n.counter.li.org/ | grep 'users registered' | sed 's/.*\<font size=7\>//g' | tr '\>' ' ' | sed 's/<br.*//g' | tr ' ' '\0'` + `curl --silent http://www.dudalibre.com/gnulinuxcounter?lang=en | grep users | head -2 | tail -1 | sed 's/.*<strong>//g' | sed 's/<\/strong>.*//g'` ))

This took me about an hour to do. It uses wget and curl because, dudalibre.com blocks wget, and wget worked nicely for me.

wget -qO - http://i18n.counter.li.org/ | grep 'users registered' | sed 's/.*\<font size=7\>//g' | tr '\>' ' ' | sed 's/<br.*//g' | tr ' ' '\0'
sudo hwinfo --framebuffer
2010-10-03 14:45:02
User: hute37
Functions: sudo
4

look at /boot/grub/menu.lst for somethig like:

## additional options to use with the default boot option, but not with the

## alternatives

## e.g. defoptions=vga=791 resume=/dev/hda5

## defoptions=vga=795

# defoptions=vga=873

## altoption boot targets option

## multiple altoptions lines are allowed

## e.g. altoptions=(extra menu suffix) extra boot options

## altoptions=(recovery) single

# altoptions=(verbose mode) vga=775 debug

# altoptions=(console mode) vga=ask

# altoptions=(graphic mode) quiet splash

# altoptions=(recovery mode) single

vga=(decimal value) is framebuffer mode

wget -O - 'https://USERNAMEHERE:PASSWORDHERE@mail.google.com/mail/feed/atom' --no-check-certificate
2010-09-26 14:47:13
User: PLA
Functions: wget
-3

I use this command in my Conky script to display the number of messages in my Gmail inbox and to list the from: and subject: fields.

sudo ls -l $(eval echo "/proc/{$(echo $(pgrep java)|sed 's/ /,/')}/fd/")|grep log|sed 's/[^/]* //g'|xargs -r tail -f
2010-07-30 18:20:00
User: vutcovici
Functions: echo eval grep ls sed sudo tail xargs
-1

Tail all logs that are opened by all java processes. This is helpful when you are on a new environment and you do not know where the logs are located. Instead of java you can put any process name. This command does work only for Linux.

The list of all log files opened by java process:

sudo ls -l $(eval echo "/proc/{$(echo $(pgrep java)|sed 's/ /,/')}/fd/")|grep log|sed 's/[^/]* //g'
for i in *.flac; do gst-launch filesrc location="$i" ! flacdec ! wavenc ! filesink location="${i%.flac}.wav"; done
2010-07-17 22:48:22
5

Takes all .flac directories, feeds them into a simple transcode pipeline to spit out .wavs with the same name (but correct extension).

lsof +L1
2010-07-14 17:21:01
User: dopeman
Tags: Linux
14

I have come across a situation in the past where someone has unlinked a file by running an 'rm' command against it while it was still being written to by a running process.

The problem manifested itself when a 'df' command showed a filesystem at 100%, but this did not match the total value of a 'du -sk *'.

When this happens, the process continues to write to the file but you can no longer see the file on the filesystem. Stopping and starting the process will, more often than not, get rid of the unlinked file, however this is not always possible on a live server.

When you are in this situation you can use the 'lsof' command above to get the PID of the process that owns the file (in the sample output this is 23521).

Run the following command to see a sym-link to the file (marked as deleted):

cd /proc/23521/fd && ls -l

Truncate the sym-link to regain your disk space:

> /proc/23521/fd/3

I should point out that this is pretty brutal and *could* potentially destabilise your system depending on what process the file belongs to that you are truncating.

( ( while [ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] || [ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ]; do sleep 10; done; my-command > output.txt ) & )
2010-07-13 09:12:11
User: michelsberg
Functions: echo sleep
4

[ 2000 -ge "$(free -m | awk '/buffers.cache:/ {print $4}')" ] returns true if less than 2000 MB of RAM are available, so adjust this number to your needs.

[ $(echo "$(uptime | awk '{print $10}' | sed -e 's/,$//' -e 's/,/./') >= $(grep -c ^processor /proc/cpuinfo)" | bc) -eq 1 ] returns true if the current machine load is at least equal to the number of CPUs.

If either of the tests returns true we wait 10 seconds and check again. If both tests return false, i.e. 2GB are available and machine load falls below number of CPUs, we start our command and save it's output in a text file.

The ( ( ... ) & ) construct lets the command run in background even if we log out. See http://www.commandlinefu.com/commands/view/3115/ .

objdump -d ./PROGRAM|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'
net rpc -I ADDRESS -U USERNAME%PASSWORD service {stop|start} SVCNAME
2010-06-29 17:12:36
User: f0rk
4

Control (stop, start, restart) a Windows Service from a Linux machine which has the `net` command (provided by samba).

tar -czf ../header.tar.gz $(find . -name *.h)
2010-06-27 23:44:48
Functions: find tar
Tags: Linux tar
1

This is a shortcut to tar up all files matching a wildcard. Tar doesn't have the --include (apparently).

aptitude remove ?and(~i~nlinux-(im|he) ?not(~n`uname -r`))
2010-06-11 22:57:09
User: dbbolton
2

A little aptitude magic. Note: this will remove images AND headers. If you just want to remove images: aptitude remove ?and(~i~nlinux-im ?not(~n`uname -r`))

I used this in zsh without any problems. I'm not sure how other shells will interpret some of the special characters used in the aptitude search terms. Use -s to simulate.

aptitude purge linux-image | grep ^i | grep -v $(uname -r)
perl -e 'chomp($k=`uname -r`); for (</boot/vm*>) {s/^.*vmlinuz-($k)?//; $l.="linux-image-$_ ";} system "aptitude remove $l";'
aptitude remove $(dpkg -l|egrep '^ii linux-(im|he)'|awk '{print $2}'|grep -v `uname -r`)
2010-06-10 21:23:00
User: dbbolton
Functions: awk egrep grep
8

This should do the same thing and is about 70 chars shorter.

while inotifywait -r -e MODIFY dir/; do make; done;
while true; do inotifywait -r -e MODIFY dir/ && make; done;
2010-06-04 17:07:03
User: fain182
15

Uses inotifywait from inotify-tools ( http://wiki.github.com/rvoicilas/inotify-tools/ ), that is compatible only with linux.

Usefull when you work with files that have to be compiled.. latex, haml, c..

cowsay `fortune` | toilet --metal -f term
2010-06-03 21:48:54
-6

Get colorful fortunes dictated by an ASCII cow. For full enjoyment you'll need to have color setup enabled for your terminal.

alias a=" killall rapidly_spawning_process"; a; a; a;
2010-05-20 02:33:28
User: raj77_in
Functions: alias
Tags: Linux unix kill
3

if you dont want to alias also then you can do

killall rapidly_spawning_process ; !! ; !! ; !!

killall rapidly_spawning_process ; killall rapidly_spawning_process ; killall rapidly_spawning_process
2010-05-20 00:26:10
Functions: killall
Tags: Linux unix kill
-2

Use this if you can't type repeated killall commands fast enough to kill rapidly spawning processes.

If a process keeps spawning copies of itself too rapidly, it can do so faster than a single killall can catch them and kill them. Retyping the command at the prompt can be too slow too, even with command history retrieval.

Chaining a few killalls on single command line can start up the next killall more quickly. The first killall will get most of the processes, except for some that were starting up in the meanwhile, the second will get most of the rest, and the third mops up.