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

All commands from sorted by
Terminal - All commands - 10,556 results
zmv '* *' '$f:gs/ /_'
git stash branch testchanges
svn log -v --xml > log.xml; zorba -q 'let $log := doc("log.xml")/log/logentry return for $author in distinct-values($log/author) order by $author return concat($author, " ", sum(count($log[author=$author]/paths/path)), "
")' --serialize-text
2013-03-22 11:17:10
User: langec
Functions: return
0

This saves Subversion's log output as XML and then runs an XQuery over it. This is standard XQuery 1.0 and should therefore also work with other XQuery processors. I have tested it with Zorba (http://www.zorba-xquery.com). XQilla (http://xqilla.sourceforge.net) also does it, but you'd have to save the query to a file and then execute "xqilla filename.xq".

The query first finds all distinct authors and then, for each author, sums up the number of paths they have changed in each commit. This accounts for commits of multiple changes at once.

The indenting space in all lines from the second one seems to be due to a bug in Zorba.

^foo^bar^:&
2013-03-22 11:04:20
User: bucket99
0

To replace foo by bar, but not execute, do ^foo^bar^:p

To replace all foo by bar, but not execute, do ^foo^bar^:&:p

useradd -m -s /bin/bash -p $(mkpasswd --hash=SHA-512 password) username
2013-03-22 10:46:03
User: miko
Functions: useradd
0

This command line creates a new user with home directory, using the command "useradd". The command "mkpasswd" lets you encrypt the password e.g. with SHA-512 method. This line ensures that your password is written encrypted in /etc/shadow" so you can log in with the new user.

echo "aplay ring.wav" | at now + 25 min
2013-03-22 04:27:57
User: liancheng
Functions: at echo
0

The Pomodoro Technique is a time management method developed by Francesco Cirillo in the late 1980s. The technique uses a timer to break down periods of work into 25-minute intervals called 'Pomodori' (from the Italian word for 'tomatoes') separated by short breaks.

You need to prepare a short .wav file (the "ring.wav" in the sample command line). This command will trigger aplay to play ring.wav 25 minutes from now on, which can be used as a poor man's pomodoro timer.

for f ([0-9].txt) zmv $f '${(l:1::0:)}'$f
2013-03-22 01:53:42
User: khayyam
Tags: zsh
0

zsh: add leading zero ... altogether pointless, as there can only be a maximum of 10 'single digit' files, and so a maximum of 10 files the command can act on. Padding further zeros will produce '0010', '001' and so break sequance. The only proper method is to itterate the numbers like so:

i=1; for f (*) zmv $f '${(l:3::0:)$((++i))}'.txt

but this has the unfortunate side effect of incrementing the values by 1 ... which may not be desirable.

(($1 > 0)) && echo "var is a number"
2013-03-22 01:15:00
User: khayyam
Functions: echo
0

calculate if "$1" is a number ... decimals included :)

chmod u+x **/*.sh
ls -Sh **/*(.Lm+100) | tail -5
2013-03-21 20:22:11
User: khayyam
Functions: ls tail
Tags: tail ls zsh
1

zsh: list of files sorted by size, greater than 100mb, head the top 5. '**/*' is recursive, and the glob qualifiers provide '.' = regular file, 'L' size, which is followed by 'm' = 'megabyte', and finally '+100' = a value of 100

sudo port selfupdate ; echo '---------' ; sudo port upgrade outdated
zmv '(*).txt' '$1.csv'
2013-03-21 02:28:30
User: khayyam
Tags: zsh
0

Requires zsh. You might also do the following:

for (*.txt) mv $i ${i/.txt/.csv}

or I imagine the following will work in bash:

for i in "*.txt" ; do mv $i ${i/.txt/.csv} ; done

i=1; for f (*.jpg) zmv $f '${(l:3::0:)$((++i))}'$f
2013-03-21 01:51:28
User: khayyam
Tags: zsh
0

zsh example using a 'for' loop and arithmetic expression:

files matching the pattern '*.jpg' are renamed with a 3 digit prefix, keeping the previous filename and suffix.

lsof -ni TCP
2013-03-20 22:51:16
User: tsener
Tags: lsof
1

also could specify port number:

lsof -ni TCP:80

ack --type=php <string>
2013-03-20 20:27:31
User: khayyam
Tags: ack
0

The 'sample output' shows the file(s) found and the line at which '' was found (for the example I used the 'string'.)

find -type f | xargs ls -1tr
nc -l <port-number>
2013-03-20 15:25:57
User: cybertoast
-1

Simple way to test if a port is available to the public. Run this command on the "server" and run a `telnet host-ip port-number` on the client. Test by sending strings to the server, which will be displayed in the server terminal.

rpm -qa --queryformat '%{size} %{name}-%{version}-%{release}\n' | sort -k 1,1 -rn | nl | head -16
2013-03-19 21:10:54
User: mpb
Functions: head nl rpm sort
0

Interesting to see which packages are larger than the kernel package.

Useful to understand which RPMs might be candidates to remove if drive space is restricted.

count=0;while IFS= read -r -d '' line; do echo "${line#* }"; ((++count==5)) && break; done < <(find . -type f -printf '%s %p\0' | sort -znr)
2013-03-19 17:19:26
User: sharfah
Functions: echo find read sort
Tags: sort find head,
-4

This command is more robust because it handles spaces, newlines and control characters in filenames. It uses printf, not ls, to determine file size.

alias clearscrollback='clear;printf %b "\033[3J"'
for FILE in *.conf; do cp $FILE{,.`date +%Y%m%dt%M:%H:%S`}; done
hello, too
alias cls='clear;ls'
2013-03-19 12:18:47
User: opexxx
Functions: alias
Tags: ls clear
0

Clear the screen and list file

find . -type f -exec ls -s {} \; | sort -n -r | head -5
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
2013-03-19 11:45:47
User: opexxx
Functions: alias
0

Add an "alert" alias for long running commands