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,492 results
quickscript () { filename="$1"; history | cut -c 8- | sed -e '/^###/{h;d};H;$!d;x' | sed '$d' > ${filename:?No filename given} }
2014-02-09 12:19:29
User: joedhon
Functions: cut sed
1

In order to write bash-scripts, I often do the task manually to see how it works. I type ### at the start of my session.

The function fetches the commands from the last occurrence of '###', excluding the function call. You could prefix this with a here-document to have a proper script-header.

Delete some lines, add a few variables and a loop, and you're ready to go.

This function could probably be much shorter...

rsync -a -v --delete sending_directory target_directory
any command | while read line; do echo "[`date -Iseconds`] $line"; done
2014-02-07 22:27:29
User: ayosec
Functions: command echo read
2

Useful to add a timestamp to every line printed to stdout.

You can use `-Ins` instead of `-Iseconds` if you want more precision.

xbacklight -set 50
2014-02-07 12:03:57
User: gabe240
1

I'm not sure what apt this is, but it seems to work on most X screens, an is useful for saving power, and not straining your eyes

for i in $(sudo lsof -Pni4 | awk -F: '/LISTEN/ { print substr($2,1,3) }'); do nc -z $(hostname) $i; done
2014-02-06 22:02:53
User: zlemini
Functions: awk sudo
0

Check if your open ports as listed by lsof are actually accepting connections using netcat.

find * -regextype posix-extended -regex '.*\.(ext_1|ext_2)' -exec cp {} copy_target_directory \;
/dev/disk/by-id/ata!(*part*)
2014-02-06 15:33:05
User: enrique
Tags: Ubuntu
0

Requires that bash has extglob enabled: shopt -s extglob

YYYY=2014; MM=02; for d in $(cal $MM $YYYY | grep "^ *[0-9]"); do DD=$(printf "%02d" $d); echo $YYYY$MM$DD; done
2014-02-06 11:31:57
User: fibo
Functions: cal echo grep printf
Tags: cal for loop
1

Edit YYYY and MM at the beginning of the command with the year and month you want.

Note that `DD=$(printf "%02d" $d)` will pad single digit integers with a leading zero.

Substitute `echo $YYYY$MM$DD` at the end of the line with the command you want to launch, for instance

script.pl --yyyymmdd $YYYY$MM$DD

pactl set-sink-mute 0 toggle
2014-02-06 04:50:17
User: anarcat
1

0 can also be replaced by the source name, e.g. alsa_output.pci-0000_00_1b.0.analog-stereo

while true; do cat "file"; done | nc -v -l 1337
2014-02-06 03:02:58
User: bknk
Functions: cat
0

A TCP server that keeps the same socket open, sending the contents of "file" repeatedly.

gnome-desktop-item-edit /usr/share/applications/ --create-new
cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 16; echo
2014-02-05 15:04:07
User: yakovlev
Functions: cat env head tr
0

Feel free to put this in your ~/.profile:

random(){ cat /dev/urandom | env LC_CTYPE=C tr -dc $1 | head -c $2; echo; }

Then use it to generate passwords:

random [:alnum:] 16

Or DNA sequences:

random ACGT 256
lsblk | grep <mountpoint>
for i in $(find . -mtime +30); do mv $i old/; done
2014-02-05 01:24:45
User: valferon
Functions: find mv
Tags: bash file
-3

Will move in that case every file in the current folder older than 30 days to the "old" folder

Replace "mv $i old/" by any command such as rm / echo to do something different.

curl ip.telize.com
2014-02-04 12:25:52
User: fcambus
0

ip.telize.com (Listen on both IPv4 and IPv6)

ip4.telize.com (Listen on IPv4 only)

ip6.telize.com (Listen on IPv6 only)

More information on : http://www.telize.com

npm ls -g|grep "^[&#9500;&#9492;]\(.\+\)\?[&#9516;&#9472;] "
sed -ru 's/(.)\1{4,}/\1/g'
2014-02-03 12:26:05
User: kryptylomese
Functions: sed
0

Piping a repeated character through the command will result in a single character only if there are more than 4 of them e.g.

echo "aaaaaa bbbbb cccc ddd" | sed -ru 's/(.)\1{4,}/\1/g'

the output will be "a b cccc ddd"

sed -ru 's/(..)\1{2,}/\1/g'
2014-02-03 12:18:31
User: kryptylomese
Functions: sed
1

This will remove repeated characters e.g.

echo "xtxtxtxt" | sed -ru 's/(..)\1{2,}/\1/g'

the output will just be "xt"

aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.LaunchTime + " " + .InstanceId' | sort -n
2014-02-03 07:59:47
Functions: sort
Tags: aws jq
0

Use the AWS CLI tools to generate a list instances, then pipe them to JQ to show only their launch time and instance id. Finally use sort to bring them out in runtime order. Find all those instances you launched months ago and have forgotten about.

find /path/to/somewhere -newermt "Jan 1"
2014-02-02 18:02:07
User: renich
Functions: find
Tags: find
2

This command uses -newerXY to show you the files that are modified since a specific date. I recommend looking for "-newerXY" on the manpage to get the specifics.

twistd -no web
2014-02-01 22:35:50
User: nkorth
-1

Install twistd first with

sudo apt-get install python-twistd-web
for i in xxxx*.mp4; do j=`echo $i | sed 's/ - \([0-9][0-9]\). / S1E\1 - /g'`; mv "$i" "$j"; done
2014-02-01 21:17:33
User: tomtom99
Functions: mv sed
0

Renames all files in the following format

xxxxxx - 34. yyyyy.mp4

to the following format

xxxxxx S1E34 - yyyyy.mp4

cat /etc/shadow
sudo passwd <username>
su <username>