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

Commands tagged bash from sorted by
Terminal - Commands tagged bash - 715 results
ps -eo pmem,comm | grep chrome | cut -d " " -f 2 | paste -sd+ | bc
2014-01-03 15:33:16
User: Darkstar
Functions: cut grep paste ps
0

This command will show the sum total of memory used in gigabytes by a program that spawns multiple instances of itself. Replace chrome with whatever program's memory usage you are investigating. This command is rather useless on software that only spawns a single instance of itself.

alias ?=concalc
2014-01-02 01:46:44
User: boynux
Functions: alias
0

Same functionality without using bash functions.

for i in */; do echo $(find $i -type f -regextype posix-extended -regex ".*\.(mp3|ogg|wav|flac)" | wc -l) $i ; done
find . -type d -maxdepth 1 -print0 | xargs -0 -I{} sh -c 'find "{}" -type f | grep "ogg\|mp3\|wav\|flac$" | wc -l | tr -d "\n"; echo " {}"'
2013-12-22 13:40:29
User: dbrgn
Functions: echo find grep sh tr wc xargs
0

This lists the number of ogg/mp3/wav/flac files in each subdirectory of the current directory. The output can be sorted by piping it into "sort -n".

bind -P | grep -v "is not" | sed -e 's/can be found on/:/' | column -s: -t
2013-12-19 12:30:19
User: leni536
Functions: column grep sed
0

Shows all available keyboard bindings in bash. Pretty printing.

ls -d .??*
2013-12-17 19:06:30
User: pydave
Functions: ls
Tags: bash glob
2

You can omit the -d to see what's inside directories. In that case, you may want -a to see dotfiles inside those directories. (Otherwise you don't need -a since you're explicitly looking at them.)

rpm -qa --queryformat '%{SIZE}\n' | awk '{sum += $1} END {printf("Total size in packages = %4.1f GB\n", sum/1024**3)}'
2013-12-14 20:22:41
User: skytux
Functions: awk rpm
0

It is not the installed size in files, but the size of RPM packages.

grep -c "search_string" /path/to/file
2013-12-10 18:13:54
User: meatflag
Functions: grep
0

-c will count the number of times your search matches in the file.

alias ltmux="if tmux has-session -t $USER; then tmux attach -t $USER; else tmux new -s $USER; fi"
2013-12-10 14:46:28
Functions: alias
Tags: bash alias sh tmux
0

If a session with named the same as your username already exists, then attach to it, otherwise create it

for i in */; do echo run_command "${i}"; done
2013-11-25 18:33:26
User: mirams
Functions: echo
0

Change the "run_command" to whatever command you want, and remove the "echo" to run it once you are happy.

nc -kl 5432 -c 'echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n";echo "<p>How are you today?</p>"'
2013-11-12 14:00:11
User: gvitalie
Functions: echo
-1

-k, --keep-open will keep connection alive, and we could exclude using 'while true'

nc is such a powerful command, it could be used instead of any OS! :p

curl ipinfo.io/<ipaddress>
xmodmap -e "pointer = $(shuf -i 1-5 | tr '\n' ' ')"
2013-10-23 16:02:04
User: lynks
0

Randomly remaps the first 5 mouse buttons (left, middle, right, upscroll, downscroll) in X for some reasonably harmless trolling. Non-persistant unless added to ~/.xinitrc or similar. The 'shuf' command is present on most modern desktop distros.

To reverse use xmodmap -e "pointer = 1 2 3 4 5"

alias tasks='git grep -EI "TODO|FIXME"'
2013-10-21 23:54:46
User: lizardo
Functions: alias grep
Tags: bash git todo
0

"git grep" automatically excludes untracked files (e.g. compiler output) and files under .git directory. If no directory or file is given, it will recursively search through the current directory.

alias tasks='grep --exclude-dir=.git -rEI "TODO|FIXME" . 2>/dev/null'
2013-10-17 16:40:10
User: joepie91
Functions: alias
Tags: bash todo
5

Place this in your .bashrc (or run it once) to set the `tasks` alias. Next time you enter `tasks` into a terminal, it will give you a list of all TODO and FIXME comments in the current directory and child directories, giving you a quick overview of what you still have to do!

ps -p $$
_autoOptions() { local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "--fooOption --barOption -f -b" -- $cur) ) ;}; complete -F _autoOptions autoOptions
tstouch() { [[ $1 =~ $2 ]] && touch -t ${BASH_REMATCH[1]} $1; }
2013-10-01 20:00:34
User: bartonski
Functions: touch
Tags: bash touch
2

tstouch takes two arguments: a filename containing a timestamp, and an extended regular expression with the parenthesized section matching a timestamp of the form YYYYMMDDhhmm or YYYYMMDDhhmm.ss.

It then touches the file with that timestamp.

dpkg-query -Wf '${Installed-Size}\t${Status}\t${Package}\n' | sort -n | grep installed
(IFS=,; echo "${array[*]}")
2013-09-25 10:36:38
User: aspiers
Functions: echo
2

This type of join is clearly documented in the bash manual. Only the first character of IFS is used for the delimiter.

wget -q -O- http://example-podcast-feed.com/rss | grep -o "<enclosure[ -~][^>]*" | grep -o "http://[ -~][^\"]*" | xargs wget -c
2013-09-24 12:38:08
User: talha131
Functions: grep wget xargs
0

This script can be used to download enclosed files from a RSS feed. For example, it can be used to download mp3 files from a podcasts RSS feed.

for ff in directory; do numLines=`wc -l $ff`; numLines=$(echo $numLines | sed 's/ .*//g'); min=$(sort -nrk 1 $ff | tail -1); if [ $numLines -gt 100 ]; then echo $min >> minValues; fi;done;
for f in ./*.xls; do mv "$f" "${f%.*}.ods"; done
2013-09-17 01:41:56
User: evilsoup
Functions: mv
Tags: bash Linux
3

An entirely shell-based solution (should work on any bourne-style shell), more portable on relying on the rename command, the exact nature of which varies from distro to distro.

!:n
2013-09-15 03:41:13
User: hackerb9
10

Bash's history expansion character, "!", has many features, including "!:" for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. See the sample output for a few examples. For full details search for "^HISTORY EXPANSION" in the bash(1) man page.

 Note that this version improves on the previous function in that it handles arguments that include whitespace correctly.

ln -s /BASE/* /TARGET/
2013-09-12 18:47:35
User: thehitman
Functions: ln
Tags: bash
5

Simple and easy to remember, if it already exists then it just ignores it.