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,488 results
lspci -vv | grep 'Ethernet\|Serial' | awk 'NR == 1{ printf $1 } NR == 2 { print " mac " $7 }' | sed ?e 's/-/:/g' -e 's/:f[ef]:f[ef]//g' -e 's/01:00.0/eth0/g' -e 's/01:00.1/eth1/g' -e 's/01:00.2/eth2/g' -e 's/01:00.3/eth3/g' > /etc/iftab && ifrename
2014-03-01 20:07:18
User: PROJAK_SX
Functions: awk grep lspci printf sed
0

for redhat systems works sometimes :S tested on dell poweredge r7+ systems

esxcli network ip interface ipv4 set -i vmk1 -I 10.27.51.143 -N 255.255.255.0 -t static
df -h --total | awk 'NR==1; END{print}'
cat /proc/cpuinfo | grep BogoMIPS | uniq | sed 's/^.*://g' | awk '{print($1 / 4) }'
locate *\\.php|xargs grep --color=always -i -5 "namespace\s.*\W"|less
2014-02-28 13:52:15
Functions: grep locate xargs
0

Another way to view some code by keyword and basic regular expression

htop -p `pgrep firefox`
git tag rel--`date +%Y-%m-%d--%H-%M-%S` -m "(%) rel: stage"
find . -name "*.txt" | xargs -n 1 perl -pi -w -e "s/text([0-9])/other\$1/g;"
2014-02-28 06:38:38
User: kennethjor
Functions: find perl xargs
0

Does a search and replace across multiple files with a subgroup replacement.

arecord -q -f cd -d 1 recvol.wav;sox recvol.wav -n stat 2>&1|grep RMS|grep amplitude|cut -d"." -f2|cut -c 1-2>recvol;echo $((`cat recvol`+1))>recvol;rec -t wav - silence 1 0.1 `cat recvol` -1 1.0 `cat recvol`%|lame -s 44.1 -a -v - >record.mp3
2014-02-27 23:23:55
User: geaplanet
Functions: arecord cd cut echo grep stat
0

It find out the mic recording level at the moment of run the command and if a noise level is higher it starts to record an mp3 file. The resulting file will have only the sounds not the silences.

sudo update-grub
2014-02-27 19:50:25
User: Anshik
Functions: sudo
-5

sudo gedit /etc/default/grub

This would open the Grub configuration file. In this file we have to edit the line

GRUB_CMDLINE_LINUX=""

to

GRUB_CMDLINE_LINUX="acpi_backlight=vendor"

for i in $(find . -regex '.*\/C.*\.cpp'); do svn mv `perl -e 'my $s=$ARGV[0]; $s=~m/(.*\/)C(.*)/; print "$s $1$2"' "$i"`; done
eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
2014-02-27 14:44:27
User: CanOrhan
Functions: cut eval grep tail
0

Connects to the last adb connection in history.

pacman -Qdt -q | xargs pacman --noconfirm -R
2014-02-27 05:17:57
User: b_t
Functions: xargs
1

-Qdt Lists dependencies/packages which are no longer required by any packages

-q Output only package name (not the version number)

-R Remove package(s)

Rest is self-explanatory.

I just started out with Arch - so if there is any better/standard method to achieve the same - please suggest.

convert http://imgs.xkcd.com/comics/now.png -negate /tmp/now.png ; DISPLAY=:0.0 awsetbg -c /tmp/now.png
2014-02-26 15:37:15
User: hybris
Tags: wallpaper xkcd
0

Get http://xkcd.com/now image, inverts colors (I prefer dark backgrounds) and set it as wallpaper.

You can replace the "convert" part by "wget -q http://imgs.xkcd.com/comics/now.png -O /tmp/now.png" if you prefer light backgrounds.

The DISPLAY variable is here to work as a cron job.

tput rmam
2014-02-26 07:06:37
User: kennyld
Functions: tput
Tags: bash ksh
0
tput rmam

will disable line wrapping so that long lines are truncated to width of the terminal ($COLUMNS).

tput smam

will re-enable wrapping.

I've always used tput in bash scripts but I guess it works on the command line too.

Doesn't work in all terminals.

See http://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html

find . -type d| while read i; do echo $(ls -1 "$i"|wc -m) $(du -s "$i"); done|sort -s -n -k1,1 -k2,2 |awk -F'[ \t]+' '{ idx=$1$2; if (array[idx] == 1) {print} else if (array[idx]) {print array[idx]; print; array[idx]=1} else {array[idx]=$0}}'
2014-02-25 22:50:09
User: knoppix5
Functions: awk du echo find ls read sort wc
0

Very quick! Based only on the content sizes and the character counts of filenames. If both numbers are equal then two (or more) directories seem to be most likely identical.

if in doubt apply:

diff -rq path_to_dir1 path_to_dir2

AWK function taken from here:

http://stackoverflow.com/questions/2912224/find-duplicates-lines-based-on-some-delimited-fileds-on-line

cat cachedump.txt | awk -F : '{print $2":"$1}'
ssh -NL 12345:localhost:631 username@remote_server
catmandu convert JSON to YAML < file.json > file.yaml
2014-02-25 08:27:05
0

You need to install the Catmandu Perl module via `cpanm Catmandu`

printf -- " -e %s" ${ARRAY[*]}
2014-02-25 03:34:12
User: SEJeff
Functions: printf
1
DOCKER_APP_VARS=(DATABASE_USER=dbuserro, DATABASE_PASSWORD=maipass)

[jeff@omniscience container] (master)$ echo docker run $(printf -- " -e %s" ${DOCKER_APP_VARS[*]}) -name 12factorapp mattdm/fedora-small

docker run -e DATABASE_USER=dbuserro, -e DATABASE_PASSWORD=maipass -name 12factorapp mattdm/fedora-small

Note that the printf method by itsself doesn't include a newline (\n), so you'll need to embed it into an echo statement or something that does.

edrv() { N=${1:-edrv}; truncate -s ${2:-256m} $N.img && L=$(losetup -f) && losetup $L $N.img && cryptsetup luksFormat --batch-mode $L && cryptsetup luksOpen $L $N && mkfs.vfat /dev/mapper/$N -n $N; cryptsetup luksClose $N; echo losetup -d $L to unmount; }
2014-02-24 01:38:21
User: snipertyler
Functions: echo losetup
2

You need to be root to do this. So check the command before running it.

You enter the same password for

Enter LUKS passphrase:

Verify passphrase:

Enter passphrase for /dev/loopn:

___

You can then copy the .img file to somewhere else.

Loop it it with losetup -f IMAGENAME.img and then mount it with a file manager (eg nemo) or run mount /dev/loopn /media/mountfolder

Acts similar to a mounted flash drive

watch -d -n 2 iptables -nvL
2014-02-23 16:35:03
User: xxdesmus
Functions: iptables watch
0

This will highlight (with a box over it) any changes since the last refresh.

ps -eo pmem,comm | grep application-name
2014-02-23 13:21:29
User: Darkstar
Functions: grep ps
0

Displays memory usage for individual instances of an application that spawns multiple instances of itself. This command also works on single process applications.

alias ...="awk '{fflush(); printf \".\"}' && echo \"\""
2014-02-22 22:20:22
User: lgarron
Functions: alias
6

If you're running a command with a lot of output, this serves as a simple progress indicator.

This avoids the need to use `/dev/null` for silencing. It works for any command that outputs lines, updates live (`fflush` avoids buffering), and is simple to understand.

sed -i '1iI am a new line' file.txt
2014-02-22 14:36:57
User: bbates
Functions: sed
0

You can use \n in your inserted data to insert multiple lines.

The leading number is the position in the file where you want the insert, so in this case a '1' indicates the top of the file.