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,925 results
function sshdel { perl -i -n -e "print unless (\$. == $1)" ~/.ssh/known_hosts; }
2009-02-03 16:20:50
User: xsawyerx
Functions: perl
-1

sometimes you got conflicts using SSH (host changing ip, ip now belongs to a different machine) and you need to edit the file and remove the offending line from known_hosts. this does it much easier.

Use history -S in your .logout file
2009-02-07 10:53:14
User: vijucat
-1

That's the key part.

I got this from http://www.macosxhints.com/article.php?story=20070715091413640. See that article for other other, more basic, tcsh-specific history-related settings.

echo "sed -e"|perl -pe 's/sed -e/perl -pe/'
2009-02-16 18:39:06
User: drossman
Functions: echo
-1

Replace sed regular expressions with perl patterns on the command line.

The sed equivalent is: echo "sed -e"|sed -e 's/sed -e/perl -pe/'

cat file | tr -d "\n"
2010-12-02 09:22:02
User: uzsolt
Functions: cat file tr
-1

This command deletes the "newline" chars, so its output maybe unusable :)

$grep -hIr -m 1 em:name ~/.mozilla/firefox/*.default/extensions|sed 's#\s*##'|tr '<>=' '"""'|cut -f3 -d'"'|sort -u
2010-05-24 08:03:53
User: raj77_in
Functions: sed sort tr
-1

with grep for em:name rather than name, you will get much better result.

wc -L files
zcat log.tar.gz | grep -a -i "string"
2011-09-14 17:20:43
User: dycropts
Functions: grep zcat
-1

Useful in system where log files are compressed for archival purposes

find . -type f -print0 | xargs -0r awk '/^\xEF\xBB\xBF/ {print FILENAME} {nextfile}'
trash <file>
2012-11-22 18:05:11
User: bkmeneguello
Tags: bash trash rm
-1

# put into .bashrc

function trash() {

if [ -z "$*" ] ; then

echo "Usage: trash filename"

else

local TRASH="${HOME}/.local/share/Trash"

if [ ! -d "$TRASH/files" ]; then mkdir -p "$TRASH/files"; fi

if [ ! -d "$TRASH/info" ]; then mkdir -p "$TRASH/info"; fi

local IFS_BKP=$IFS

IFS='

'

for FILE in $@ ; do

local BASE=$( basename "$FILE" )

local TRASH_NAME="$BASE"

local COUNTER=1

while [ -e "$TRASH/files/$TRASH_NAME" ]; do

COUNTER=`expr $COUNTER + 1`

TRASH_NAME="$BASE.$COUNTER"

done

local FULL_PATH=$( readlink -f "$FILE" )

local DATE=$( date +%Y-%m-%dT%H:%M:%S )

mv "$FULL_PATH" "$TRASH/files/$TRASH_NAME"

if [ $? -eq 0 ]; then

echo "[Trash Info]

Path=$FULL_PATH

DeletionDate=$DATE" > "$TRASH/info/$TRASH_NAME.trashinfo"

fi

done

IFS=$IFS_BKP

fi

}

grep -w /media/KINGSTON /proc/mounts | cut -d " " -f
curl ifconfig.me
cat dump.sql | sed -n -e '/Table structure for table .table1./,/Table structure for table .table2./p'
2012-11-22 23:54:04
User: infojunkie
Functions: cat sed
Tags: mysql sed
-1

Given a dump.sql file, extract table1 creation and data commands. table2 is the one following table1 in the dump file. You can also use the same idea to extract several consecutive tables.

awk 'BEGIN{ORS=""}NR!=1&&FNR==1{print "\n"}{print}END{print "\n"}' *.txt
2010-05-24 19:22:35
User: recursiverse
Functions: awk
-1

Problem: you want to output one line per file. you can't just 'tr -d' because you want one line per file and you don't want to use a loop.

Solution: use awk to print each line without the record separator and a newline after each file.

source <(egrep '^#define EX_.*' /usr/include/sysexits.h | sed -e 's/#define/declare -r/g' | sed 's/\//#/g' | sed -e 's/\s\{1,\}/ /g' | sed -e 's/ \([0-9]\)/\=\1/'g )
2012-09-21 13:30:37
User: fishdaemon
Functions: egrep sed
-1

Use meaningful exit codes

change "source" to "cat" to view output instead of assigning

plink lyu0@mysshserver -pw 123456
gst-launch v4l2src
2009-03-23 05:45:00
User: samliu
-1

Cheese or other webcam software not working? Try this.

dpkg -l |grep i386 | awk '{ print "apt-get -y remove --purge "$2 }' | sh
(echo -e '\x06\x00\x00\x00\x00\x00\x01\x01\x00'; sleep 1)|nc -c $host 25565
ls | grep -vi pattern
scselect <location>
2011-01-26 03:56:14
User: gwchamb
-1

Use this command to change your network location from the OSX command line.

sudo rpm -if "http://rpm_server/rpm_repo/this-app.rpm"
strings -1 <file>
2012-11-23 11:33:25
User: Testuser_01
Functions: strings
-1

Use this like the cat command with the additional feature to strip out unprintable characters from the input, newlines will stay.

find . -maxdepth 1 -type f| xargs sha1sum | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done
gnome-open .
2010-10-01 13:16:00
User: pahnin
-1

when working under a cli sometime you need to list the files with ls

but u can open gnome file browser with the command 'gnome-open .' under current directory

curl -D - -X POST -H 'Content-type: text/xml' -d @XML http://remote_server:8080/web-service/soap/WSName
2011-05-23 08:25:24
User: igorfu
-1

Use curl to invoke remote web service, where the file 'XML' is the soap payload