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 using command from sorted by
Terminal - Commands using command - 48 results
any command | sed "s/^/\[`date +"%Y%m%d%H%M%S"`]/"
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.

command -v <command>
ssh() { [ $TERM == screen ] && (screen -X title "${1##*@}"; command ssh "$@"; screen -X title '';exit;) || command ssh "$@"; }
2013-06-03 12:31:05
User: djkadu
Functions: command screen ssh
0

By adding this to your bashrc, when SSH'ing to a server while screen is active it will change the window tittle to the name of the server you going to.

sudo apt-get <apt-get command and options> --print-uris -qq | sed -n "s/'\([^ ]\+\)' \([^ ]\+\) \([^ ]\+\) MD5Sum:\([^ ]\+\)/wget -c \1/p" > dowload_deb_list.txt
command 3>&1 1>&2 2>&3 | tee -a file
2012-10-30 15:40:08
User: netkill
Functions: command tee
0

Appends output to the file, some systems require the -a to do this.

command 3>&1 1>&2 2>&3 | tee file
2012-10-30 10:53:21
User: hute37
Functions: command tee
1

taken from

http://www.unix.com/shell-programming-scripting/158311-how-tee-stderr.html

"

What does it mean?

The redirection operator n>&m makes file descriptor n to be a copy of file descriptor m. So, whe are:

- Opening a new file descriptor, 3, that is a copy of file descriptor 1, the standard output;

- Making file descriptor 1 a copy of file descriptor 2, the standard error output;

- Making file descriptor 2 to be a copy of file descriptor 3 (the "backup" of the standard output)

in a short: we swapped the standard output and the standard error output.

"

vim -p $(complicated command to list the files you want)
2012-09-28 10:18:30
Functions: command vim
Tags: vim,xargs
0

see: https://groups.google.com/forum/?fromgroups=#!topic/vim_use/e3Er8x_P8jg

cd () { command cd $1 && ls ; }
func "*" call command run "uname -i"
2012-07-23 13:32:32
User: tduvally
Functions: command
0

Using "func" to run any command and receive output from all your systems. Default output from func is normally meant for further script parsing, but is generally readable. This example shows system arch, but any command can be used and will run as typed.

command <<< word
2012-02-29 03:14:54
Functions: command
Tags: bash stdin
8

Don't do this:

echo word | command

Using a bash "here strings" and "here documents" look leeter than piping echo into the command. Also prevents subshell execution. Word is also expanded as usual.

command > >(tee stdout.log) 2> >(tee stderr.log >&2)
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
2011-10-06 20:37:54
Functions: command tee
Tags: vim sudo tee
6

Calls sudo tee like all the other lines, but also automatically reloads the file.

Optionally you can add

command Wq :execute ':W' | :q

and

command WQ :Wq

to make quitting easier

command -v bash
2011-09-26 10:17:41
User: atoponce
Functions: command
Tags: which command
4

it is generally advised to avoid using which(1) whenever possible. which(1) is usually a csh(1) script, or sometimes a compiled binary. It's output is highly variable from operating system to operating system, so platform independent scripts could become quite complicated with the logic. On HP-UX 10.20, for example, it prints "no bash in /path /path /path ..."; on OpenBSD 4.1, it prints "bash: Command not found."; on Debian (3.1 through 5.0 at least) and SuSE, it prints nothing at all; on Red Hat 5.2, it prints "which: no bash in (/path:/path:...)"; on Red Hat 6.2, it writes the same message, but on standard error instead of standard output; and on Gentoo, it writes something on stderr. And given all these differences, it's still variable based on your shell. This is why POSIX is king. See http://mywiki.wooledge.org/BashFAQ/081 for more ways on avoiding which(1).

ps ewwo command PID | tr ' ' '\n' | grep \=
myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link"; }
2011-09-13 11:02:27
User: keymon
Functions: cd command dirname echo ls
0

This is a equivalent to the GNU ' readlink' tool, but it supports following all the links, even in different directories.

An interesting alternative is this one, that gets the path of the destination file

myreadlink() { [ ! -h "$1" ] && echo "$1" || (local link="$(expr "$(command ls -ld -- "$1")" : '.*-> \(.*\)$')"; cd $(dirname $1); myreadlink "$link" | sed "s|^\([^/].*\)\$|$(dirname $1)/\1|"); }
command >&-
sudo lsof -rc command >> /tmp/command.txt
2011-08-03 20:19:53
User: zlemini
Functions: command sudo
6

Run this before you run a command in order to see what the command does as it starts.

The -c flag is useful here as the PID is unknown before startup.

All config files, libraries, logs, ports, etc used by the command as it starts up, (and shuts down) will be captured at 1s intervals and written to a file.

Useful for debugging etc.

cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
2011-06-24 08:48:13
User: flatcap
Functions: cd command set
Tags: cd test set
0

Move efficiently between directories.

.

This command adds a couple of extra features to cd, without affecting normal use.

CDPATH use is also unaffected. It introduces and environment variable CDDIR which is used as an alternate home directory.

.

Note: I don't want to alter $HOME because then all my dot files will move.

.

Examples:

.

cd dir

Change directory to "dir" (using CDPATH if necessary)

.

cd dir/file.txt

Change directory to "dir" (containing folder of "file.txt")

This allows you to cut'n'paste, or use

.

CDDIR is unset

cd

Change directory to $HOME

.

CDDIR=/home/flatcap/work

cd

Change directory to /home/flatcap/work

.

For convenience, put the command, and the following, in your .bashrc or .bash_profile

export CDDIR="/home/flatcap/work"

alias cdd="CDDIR=$(pwd)"

command & echo $!
2011-06-08 18:16:38
User: Mahrud
Functions: command echo
-2

Actually $! is an internal variable containing PID of the last job in background.

More info: http://tldp.org/LDP/abs/html/internalvariables.html#PIDVARREF

Using $! for job control:

possibly_hanging_job & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
awk '{command="echo "$2"|md5sum" ;command | getline $2; close(command);sub(/[[:blank:]].*/,"",$2); print $0}'
ps ho command $$
Command in description (Your command is too long - please keep it to less than 255 characters)
2011-02-03 08:25:42
User: __
Functions: command less
0
yt2mp3(){ for j in `seq 1 301`;do i=`curl -s gdata.youtube.com/feeds/api/users/$1/uploads\?start-index=$j\&max-results=1|grep -o "watch[^&]*"`;ffmpeg -i `wget youtube.com/$i -qO-|grep -o 'url_map"[^,]*'|sed -n '1{s_.*|__;s_\\\__g;p}'` -vn -ab 128k "`youtube-dl -e ${i#*=}`.mp3";done;}

squeezed the monster (and nifty ☺) command from 7776 from 531 characters to 284 characters, but I don't see a way to get it down to 255. This is definitely a kludge!

Command in description (Your command is too long - please keep it to less than 255 characters)
2011-02-01 17:36:32
User: m1cawber
Functions: command less
0
yt-mp3chanrip() { for count in 1 51 101 151 201 251 301; do for i in $(curl -s http://gdata.youtube.com/feeds/api/users/"$1"/uploads\?start-index="$count"\&max-results=50 | grep -Eo "watch\?v=[^[:space:]\"\'\\]{11}" | uniq); do ffmpeg -i $(wget http://youtube.com/"$i" -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" | sed -n '/^fmt_url_map/,/videoplayback/p' | sed -e :a -e '$q;N;5,$D;ba' | tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/') -vn -ab 128k "$(youtube-dl -e http://youtube.com/"$i").mp3"; done; done; unset count i; }

create the function and run with

yt-mp3chanrip YoutubeUsername

Great for channels like ukfDrumAndBass that only post music. No more need for third party browser plugins or websites that only convert one vid one at a time. It'll convert and save to CWD up to 300 of a user's videos to mp3s, one at a time. To increase, just increment the $count pattern. This is a concoction from commands #7718 and #7752, so it uses ffmpeg wget, curl, sed, and youtube-dl -- youtube-dl is only used to get the title of the video which it uses to name the mp3 file. You can use a different naming method if you want and the function should still work.

command &!