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

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,144 results
tar pzcvf /result_path/result.tar.gz /target_path/target_folder
2009-11-10 11:17:00
User: CafeNinja
Functions: tar
0

The command as given would create the file "/result_path/result.tar.gz" with the contents of the target folder including permissions and sub- folder structure.

chsec -f /etc/security/lastlog -a "unsuccessful_login_count=0" -s 'aix user'
cat /proc/acpi/battery/BAT0/state
echo "text" | od -t x1
2010-07-14 14:53:25
User: max_allan
Functions: echo od
Tags: perl hex ascii
0

Just use "od" and it can also dump in decimal or octal.

(use -t x1 and not just -x or it confuses the byte order)

There is a load of other formatting options, I'm not sure if you can turn off the address at the start of the line.

awk '( $1 == "MemTotal:" ) { print $2/1048576 }' /proc/meminfo
ls -r | ?{-not $_.psiscontainer} | group extension | select name, count, @{n='average'; e={($_.group | measure -a length).average}} | ft -a @{n='Extension'; e={$_.name}}, count, @{n='Average Size (KB)'; e={$_.average/1kb}; f='{0:N2}'}
2012-03-13 17:58:10
User: brianpeiris
Functions: ls
Tags: PowerShell
0

Here's an annotated version of the command, using full-names instead of aliases. It is exactly equivalent to the short-hand version.

# Recursively list all the files in the current directory.

Get-ChildItem -Recurse |

# Filter out the sub-directories themselves.

Where-Object { return -not $_.PsIsContainer; } |

# Group the resulting files by their extensions.

Group-Object Extension |

# Pluck the Name and Count properties of each group and define

# a custom expression that calculates the average of the sizes

# of the files in that group.

# The back-tick is a line-continuation character.

Select-Object `

Name,

Count,

@{

Name = 'Average';

Expression = {

# Average the Length (sizes) of the files in the current group.

return ($_.Group | Measure-Object -Average Length).Average;

}

} |

# Format the results in a tabular view, automatically adjusted to

# widths of the values in the columns.

Format-Table -AutoSize `

@{

# Rename the Name property to something more sensible.

Name = 'Extension';

Expression = { return $_.Name; }

},

Count,

@{

# Format the Average property to display KB instead of bytes

# and use a formatting string to show it rounded to two decimals.

Name = 'Average Size (KB)';

# The "1KB" is a built-in constant which is equal to 1024.

Expression = { return $_.Average / 1KB };

FormatString = '{0:N2}'

}

for wrd in `cat file2` ; do sed -i .bk "/^$wrd/d" file1; done
2012-04-30 06:08:12
User: divyamistry
Functions: sed
0

Given that file1 has bunch of lines (i.e. TSV file with first column as row titles), and file2 has bunch of words (i.e. row titles that are in file1), this command takes every word from file2, and removes every line in file1 that starts with that word.

for ((i=65;i<91;i++)); do printf "\\$(printf '%03o' $i) "; done
edit-notime () { FILE=$1; TMP=`mktemp /tmp/file-XXXXXX`; cp -p $FILE $TMP; $EDITOR $TMP; touch -r $FILE $TMP; cp -p $TMP $FILE; rm -f $TMP; }
2012-10-31 00:54:19
User: jecxjoopenid
Functions: cp rm touch
0

Copies file to a temporary location, edit and set to real file's time stamp then copy back. Assumes access to /tmp and has $EDITOR, but can be replaced with better values.

ffmpeg -i input.mkv -vcodec copy -acodec copy -absf aac_adtstoasc output.m4v
BEGIN=`date`; rsync -avxW /home/ /backups/home ; echo "Begin time: $BEGIN" ; echo "End time..: `date`"
2013-07-06 08:24:45
User: ryanchapman
Functions: echo rsync
Tags: rsync
0

Copies a directory structure from /home/ to /backups/home (notice that the destination does not have a trailing slash)

psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db>
2013-12-11 15:39:56
User: cuberri
0

This command drops all the tables of the 'public' schema from the database.

First, it constructs a 'drop table' instruction for each table found in the schema, then it pipes the result to the psql interactive command.

Useful when you have to recreate your schema from scratch in development for example.

I mainly use this command in conjunction with a similar command which drop all sequences as well.

Example :

psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop sequence \"' || relname || '\" cascade;' from pg_class where relkind='S'" | psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db>

See it scripted here : https://gist.github.com/cuberri/6868774#file-postgresql-drop-create-sh

ffmpeg -i $video -c:v prores -profile:v 2 -c:a copy ${video}.mov
gs -dNOCACHE -dNOPAUSE -dBATCH -dSAFER -sDEVICE=epswrite -dEPSCrop -sOutputFile=out.eps in.pdf
2009-02-05 20:06:40
User: Boojum
Functions: gs
0

Use this to turn a page with snippets of equations into vector paths that Adobe Illustrator can handle without choking on font embedding and substitution issues. Good for keeping fonts consistent when labeling charts and diagrams. Also good for embedding formulas into diagrams.

IPADDR=`ifconfig eth0 | grep -i inet | awk -F: '{print $2}'| awk '{print $1}'`
2009-02-25 22:58:19
User: rockon
Functions: awk grep
0

Useful in scripts while you just need an IP address in a variable.

svn status | egrep '^(M|A)' | egrep -o '[^MA\ ].*$'
2009-03-27 05:18:24
User: isaacs
Functions: egrep
Tags: svn
0

This is useful for piping to other commands, as well:

svn status | egrep '^(M|A)' | egrep -o '[^MA\ ].*$' | xargs $EDITOR
IP=$(nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F# '{print $1}' | tail -n 1 ); R=3$((RANDOM%6 + 1)); PS1="\n\[\033[1;37m\]\u@\[\033[1;$R""m\]\h^$IP:\[\033[1;37m\]\w\$\[\033[0m\] "
2010-10-20 07:29:14
User: rubo77
Functions: awk grep nslookup tail
0

this adds a random color to your prompt and the external ip.

useful if you are using multiple mashines with the same hostname.

find files/ -type f | while read line; do if [ $((i++%100)) -eq 0 ]; then mkdir $((++folder)); fi; cp $line $folder/; done
xpath () { xmllint --format --shell "$2" <<< "cat $1" | sed '/^\/ >/d' }
2011-10-05 07:45:16
User: sharfah
Functions: sed
0

This function uses xmllint to evaluate xpaths.

Usage: xpath /some/xpath XMLfile

curl -c cookie.txt -d username=hello -d password=w0r1d http://www.site.com/login
for i in `ls *.anki`; do sqlite3 $i "select (cards.question || '||' || cards.answer) from cards, facts where cards.factid=facts.id and facts.tags like '%mytag%';" >> mytag.csv; done
2012-03-13 19:03:20
Tags: CSV anki
0

Produces a CSV file containing fronts/backs of cards with specified tag ("mytag" above). This command pulls these cards from different card databases, and allows them to be merged into one (by importing the resulting CSV file).

The CSV file is not directly produced; instead of commas, "||" are inserted. In your editor of choice, modify the resulting file to put quotes around the text before || and after ||, then change || to a comma (for every line).

mplayer <(tail -fc +0 <filename>)
2012-04-30 07:28:42
User: gtmanfred
Functions: tail
0

It will just start playing and will not stop at the point that it has downloaded up to when the video starts

cat ~/.bash_history | perl -lane 'if($F[0] eq "sudo"){$hash{$F[1]}++}else{$hash{$F[0]}++};$all++;END {@top = map {[$_, $hash{$_}]} sort {$hash{$b}<=>$hash{$a}} keys %hash;printf("%10s%10d%10.2f%%\n", $_->[0],$_->[1],$_->[1]/$all*100) for @top[0..9]}'
MP3TAG_DECODE_UTF8=0 mp3info2 -p "%a - %l\n" -R . | sort | uniq
gpg --export 0xDEADBEEF | gpg --list-packets | grep -Pzao ':signature packet:.*\n\t.*sigclass 0x20(\n\t.*)*'
2013-03-05 14:11:11
User: claudius
Functions: gpg grep
Tags: GPG revocation
0

Shows a sigclass 0x20 (Key revocation) signature packet on a key, including all subpackets. Subpacket 2 is the date of revocation, subpacket 26 the relevant policy, subpacket 29 the reason of revocation (cf. http://rfc.askapache.com/rfc4880/rfc4880.html#section-5.2.3.23 ) and subpacket 16 the issuer of the revocation certificate (usually should be the same as the revoked key).