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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 12,334 results
rsync -a --progress -e 'ssh -p 2200 -i .vagrant/machines/default/virtualbox/private_key' [email protected]:/vagrant/vm/old_timecapsule_backup /Volumes/2TB/
2015-12-17 15:28:12
User: brainstorm
Functions: rsync
1

That is, after running `vagrant ssh-config` to determine ports and ip's:

vagrant ssh-config

Host default

HostName 127.0.0.1

User vagrant

Port 2200

UserKnownHostsFile /dev/null

StrictHostKeyChecking no

PasswordAuthentication no

IdentityFile /Users/romanvg/tmp/.vagrant/machines/default/virtualbox/private_key

IdentitiesOnly yes

LogLevel FATAL

for f in `git status | grep new | awk '{print $3}'`; do git reset HEAD $f ; done
lc() { od="$1"; nd="$2"; of=$3; nf=$4; cp -rl "$od" "$nd"; find $nd -type f -iname \*$of -print -execdir ffmpeg -i {} -loglevel error -q:a 6 {}.$nf \; -execdir rm {} +; find $nd -type f -iname \*.$of.$nf -execdir rename "s/$of.$nf/$nf/" {} +; }
2015-12-16 20:16:01
User: snipertyler
Functions: cp find rename rm
0

Library Convert

Usage

lc Old_Directory New_DIrectory Old_Format New_Format

lc ~/Music ~/Music_ogg mp3 ogg

This will convert all files in the old directory to the new directory from the old format to the new format. It will leave the original library alone. The converted library will retain folder structure.

s3cmd du s3://bucket-name | awk '{print $0/1024/1024/1024" GB"}'
jmap -dump:live,format=b,file=o.hprof pid ; jstack -l pid > /oracle/ora_app1/p.tdump
cat < /dev/tcp/74.125.224.40/80
docker inspect --format "{{ .Name }} # {{ .NetworkSettings.IPAddress }} # {{ .NetworkSettings.Ports }}" $(docker ps -q) | tr -s '#' '\t'
while true; do printf "\e[38;5;$(($(od -d -N 2 -A n /dev/urandom)%$(tput colors)))m.\e[0m"; done
2015-11-24 15:21:27
User: atoponce
Functions: printf
Tags: urandom
11

Looks best in an 80x24 256-color terminal emulator.

git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
2015-11-23 21:35:57
User: flatcap
Functions: nl sed
Tags: git sed nl git-log
1

Print a git log (in reverse order) giving a reference relative to HEAD.

HEAD (the current revision) can also be referred to as HEAD~0

The previous revision is HEAD~1 then HEAD~2 etc.

.

Add line numbers to the git output, starting at zero:

... | nl -v0 | ...

.

Insert the string 'HEAD~' before the number using sed:

... | sed 's/^ \+/&HEAD~/'

.

Thanks to bartonski for the idea :-)

o=0; git log --oneline | while read l; do printf "%+9s %s\n" "HEAD~${o}" "$l"; o=$(($o+1)); done | less
pdf2txt myfile.pdf | grep mypattern
2015-11-23 17:46:22
User: grinob
Functions: grep
Tags: pipe grep pdf
2

This is a good alternative to pdf2text for Ubuntu. To install it:

sudo apt-get install python-pdfminer

SRC="/source/folder"; TRG="/target/folder/"; tar cf - "$SRC" | pv -s $(du -sb "$SRC" | cut -f1) | tar xf - -C "$TRG"
dockexecl() { docker exec -i -t $(docker ps -l -q) bash ;}
docker inspect --format "{{ .NetworkSettings.IPAddress }}" $(docker ps -q)
find <mydir> -type f -exec rename 's/<string1>/<string2>/g' {} \;
2015-11-19 17:51:13
User: thrifus
Functions: find rename
Tags: find replace
0

This version works on OS X (if you have installed `rename`)

echo "$(( $(( $(grep 'physical id' '/proc/cpuinfo' | uniq | wc -l) * $(grep 'core id' '/proc/cpuinfo' | wc -l) )) * 2 + 1 ))"
2015-11-14 20:44:39
User: snorf
Functions: echo
Tags: uniq
0

shell order of operation example which calculates:

x = number of physical CPU's

y = number of cores per CPU

2(x * y) + 1 = CPU load limit

awk 'BEGIN{srand()} match($0, /DELTA=([0-9]+);/, a) {w[i++]=a[1]} END {print w[int(rand()*i)]}' file.name
2015-11-13 17:56:34
User: jkirchartz
Functions: awk
Tags: awk regex random
0

seed the random number generator,

find all matches in a file

put all matches from the capture group into an array

return a random element from the array

perl -C -MHTML::Entities -pe 'decode_entities($_);'
2015-11-13 15:07:54
User: fnl
Functions: perl
0

The only command-line version of this task I could come up with that properly handles UTF-8 input, too. (Yes, I know how crazy it is to have UTF-8 with HTML entity escapes, but that stuff exists... sadly...; Just check Twitter.)

find <mydir> -type f -exec sed -i 's/<string1>/<string2>/g' {} \;
echo "Gold price is" $(wget https://rate-exchange-1.appspot.com/currency\?from=XAU\&to=USD -q -O - | jq ".rate") "USD"
2015-11-11 14:20:06
User: lordtoran
Functions: echo wget
Tags: wget finance jq
2

Returns the current price of a troy ounce of gold, in USD. Requires the "jq" JSON parser.

Ctrl + Alt
2015-11-10 22:08:57
User: knoppix5
11

Hold 'Ctrl' + 'Alt' key while selecting rectangular text area of the screen with

left mouse button.

Should work in any terminal screen (xterm, konsole, ...) under X, if not

then try with 'Ctrl' + 'Shift' + 'Alt' or two-combination of these.

netstat -np | grep -v ^unix
2015-11-09 17:22:30
User: UnklAdM
Functions: grep netstat
6

I often have to google this so I put it here for quick reference.

echo 'echo /etc/games/fortune > ~/mailsignature.txt' >> .bashrc
2015-11-07 15:17:12
User: dededede
Functions: echo
0

In Thunderbird open the settings for your email account, mark the checkbox for 'Attach the signature from a file instead' and use the filename '~/mailsignature.txt'

Now every time when you open a terminal you see the fortune displayed in the terminal and the mail signature gets regenerated.

command ${MYVAR:+--someoption=$MYVAR}
2015-11-04 19:47:24
User: pdxdoughnut
Functions: command
1

See "Parameter Expansion" in the bash manpage. They refer to this as "Use Alternate Value", but we're including the var in the at alternative.

[ -n "$REMOTE_USER" ] || read -p "Remote User: " -er -i "$LOGNAME" REMOTE_USER
2015-10-30 17:08:17
User: pdxdoughnut
Functions: read
Tags: read input
2

The read command reads input and puts it into a variable. With -i you set an initial value. In this case I used a known environment variable.