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,583 results
cut -d: -f1 /etc/passwd | sort
2009-02-16 21:42:34
User: neW1
Functions: cut
0

Easily list all users

grep -v "^\($\|#\)" <filenames>
2009-02-16 21:40:09
User: raphink
Functions: grep
2

Filter comments and empty lines in files. I find this very useful when trying to find what values are actually set in a very long example config file.

I often set an alias for it, like :

alias nocomment='grep -v "^\($\|#\)"'

cd !$
2009-02-16 21:33:14
User: raphink
Functions: cd
1

!$ recalls the last argument of the previous command. This is very useful when you have to operate several operations on the same file for example.

awk -F'^"|", "|"$' '{ print $2,$3,$4 }' file.csv
2009-02-16 21:32:46
User: SiegeX
Functions: awk
7

The $2, $3, $4 fields are arbitrary but note that the first field starts from $2 and the last field is $NF-1. This is due to the fact that the leading and trailing quotes are treated as field delimiters.

kquitapp plasma
2009-02-16 21:27:16
User: raphink
Tags: KDE
-2

KDE4 is great, but still a bit buggy, and sometimes plasma requires to be restarted. Instead of quitting it with "killall plasma", which might loose your preferences (widgets, etc.), kquitapp will cleanly quit it. Tip: you can type this in the "Alt+F2" window, and then type "plasma" in Alt+F2 again to restart plasma (be patient though...).

diff dir1 dir2 | diffstat
2009-02-16 21:21:16
User: raphink
Functions: diff
0

See which files differ in a diff, and how many changes there are. Very useful when you have tons of differences.

echo *
2009-02-16 21:20:13
User: grep
Functions: echo
2

I know its not much but is very useful in time consuming scripts (cron, rc.d, etc).

perl -pi -e 's:^V^M::g' <filenames>
2009-02-16 21:17:40
User: starchox
Functions: perl
1

That "^M" is Ctrl-M, which is a carriage return, and is not needed in Unix file systems.

Where ^V is actually Ctrl-V and ^M is actually Ctrl-M (you must type these yourself, don't just copy and paste this command). ^V will not be displayed on your screen.

cd /usr/ports; grep -F "`for o in \`pkg_info -qao\` ; \ do echo "|/usr/ports/${o}|" ; done`" `make -V INDEXFILE` | \ grep -i \|ports@freebsd.org\| | cut -f 2 -d \|
2009-02-16 21:07:35
User: grep
Functions: cd cut grep
-1

only works for freeBSD where ports are installed in /usr/ports

credit to http://wiki.freebsd.org/PortsTasks

sed '1!G;h;$!d'
sed 's/$//'
sed 's/$/<ctrl+v><ctrl+m>/'
2009-02-16 20:53:05
User: SiegeX
Functions: sed
4

The ctrl+v,ctrl+m portion represents key presses that you should do. If you do it successfully you should see a ^M character appear.

sed '/./,$!d'
sed '1,55d'
2009-02-16 20:36:25
User: grep
Functions: sed
-4

deletes first 55 lines; change the 55 to whatever number

newhostname=$(hostname | awk -F. '{print $1 "." $2}'); ipaddress=$(nslookup `hostname` | grep -i address | awk -F" " '{print $2}' | awk -F. '{print $3 "." $4}' | grep -v 64.142);PS1="[`id -un`.$newhostname.$ipaddress]"' (${PWD}): '; export PS1
2009-02-16 20:11:53
User: simardd
-4

changes the PS1 to something better than default.

[username.hostname.last-2-digits-of-ip] (current directory)

sed 's/$'"/`echo \\\r`/"
gpg --encrypt --recipient 'Foo Bar' foo.txt
2009-02-16 19:58:13
User: mariusz
Functions: gpg
4

gpg command to encrypt a file on the command line.

gpg --output foo.txt --decrypt foo.txt.pgp
2009-02-16 19:56:19
User: mariusz
Functions: gpg
5

gpg command to decrypt a previously encrypted file on the command line.

Can be optionally made into an alias:

alias decrypt='gpg --output foo.txt --decrypt foo.txt.pgp'

find path/to/folder/ -type f -print0 | xargs -0 -n 1 md5sum | awk '{print $1}' | sort | md5sum | awk '{print $1}'
2009-02-16 19:39:37
User: mcover
Functions: awk find md5sum sort xargs
-2

For quick validation of folder's file-contents (structure not taken into account) - I use it mostly to check if two folders' contents are the same.

netstat -putona
2009-02-16 19:14:35
User: starchox
Functions: netstat
6

-p PID and name of the program

-u on a UDP port.

-t also TCP ports

-o networking timer

-n numeric IP addresses (don't resolve them)

-a all sockets

echo "<?php phpinfo(); ?>" | php > phpinfo.txt
echo "SELECT * FROM table; " | mysql -u root -p${MYSQLROOTPW} databasename | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > outfile.csv
2009-02-16 18:55:18
Functions: echo sed
8

This command converts a MySQL query directly into a .csv (Comma Seperated Value)-file.

smbcacls //server/sharename file -U username
2009-02-16 18:52:14
User: drossman
Functions: file
1

You can read, add, delete and modify Windows permissions from Linux using smbcacls from the smb-client package.

df / | awk '{print $1}' | grep dev | xargs tune2fs -l | grep create
2009-02-16 18:45:03
User: Kaio
Functions: awk df grep tune2fs xargs
9

Very useful set of commands to know when your file system was created.

for i in `df -P |grep string|cut -f2 -d%|cut -c2-100`; do umount -l -f $i;done
2009-02-16 18:42:00
User: Kaio
Functions: umount
1

Sometimes, you have a lot of NFS in the server and you can't or shouldn't use umount -a. Whis this command, you only umount the fs related to the 'string'