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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 12,342 results
function df_func { local dfts=$(ssh $1 "df -lP | tail -n +2 | sed 's/%//'"); echo $dfts | awk '$5 > 90 {exit 1}' > /dev/null; if [ $? == 1 ]; then echo -n "$1 "; echo $dfts | awk '$5 > 90 {printf "%s %d%%\n", $6, $5}'; fi }
find . -type f -printf '%T@ %TY-%Tm-%Td %TH:%TM:%.2TS %p\n' | sort -nr | head -n 5 | cut -f2- -d" "
2016-03-23 11:56:39
User: paulera
Functions: cut find head sort
6

The output format is given by the -printf parameter:

%T@ = modify time in seconds since Jan. 1, 1970, 00:00 GMT, with fractional part. Mandatory, hidden in the end.

%TY-%Tm-%Td %TH:%TM:%.2TS = modify time as YYYY-MM-DD HH:MM:SS. Optional.

%p = file path

Refer to http://linux.die.net/man/1/find for more about -printf formatting.

------------------------

sort -nr = sort numerically and reverse (higher values - most recent timestamp - first)

head -n 5 = get only 5 first lines (change 5 to whatever you want)

cut -f2- -d" " = trim first field (timestamp, used only for sorting)

------------------------

Very useful for building scripts for detecting malicious files upload and malware injections.

[ $[ $RANDOM % 6 ] == 0 ] && echo 'Bang!' || echo 'Click...'
2016-03-23 11:09:56
User: paulera
Functions: echo
Tags: bash fun
3

Shows "Bang!" in a chance of 1 out of 6, like in the original game with the gun (spin every round). Otherwise, echoes "Click...". If feeling brave you can also do:

[ $[ $RANDOM % 6 ] == 0 ] && echo 'Bang!' && a really killer command || echo 'Click...'
fortune -s | cowsay -f tux | lolcat -s 64
make your terminal interactive and fun
transfer() { basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g');curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile"|xsel --clipboard;xsel --clipboard ; }
2016-03-20 19:38:48
User: leftyfb
Functions: basename sed
2

Requires:

curl

xsel

access to the internet(http://transfer.sh)

This is an alias utilizing the transfer.sh service to make sharing files easier from the command line. I have modified the alias provided by transfer.sh to use xsel to copy the resulting URL to the clipboard.

The full modified alias is as follows since commandlinefu only allows 255 characters:

transfer() { if [ $# -eq 0 ]; then echo "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" |xsel --clipboard; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" |xsel --clipboard ; fi; xsel --clipboard; }

rsync -avxHAXW --info=progress2 /old-disk /new-disk/
awk -F= '/^(psk|id)/{print $2}' /etc/NetworkManager/system-connections/"$(iwgetid -r)"
exa -glhrSuU -s size --group-directories-first -@ | less -R
2016-02-27 17:19:43
User: wires
Functions: less size
Tags: ls color exa
-1
exa

nothing fancy

`ls` alternative `exa`, with most info printed and passed through less with the `-R` (raw) option, to preserve colour output https://github.com/ogham/exa

You can add or remove `-@` to print extended attributes for files that have them.

echo /etc/*_ver* /etc/*-rel*; cat /etc/*_ver* /etc/*-rel*
2016-02-19 12:12:38
User: sxiii
Functions: cat echo
2

Just run this command and it will printout all the info available about your current distribution and package management system.

blank=$(mktemp -d); rsync --delete "$blank/" "bigdir/"; rmdir "$blank"
2016-02-15 11:23:15
User: Natureshadow
Functions: mktemp rmdir rsync
Tags: rsync rm
4

rsync'ing an empty directory over a directory to be deleted recursively is much faster than using rm -rf, for various reasons. Relevant only for directories with really a lot of files.

egrep -v '^\s*($|#)' $(git grep -l '#!/bin/.*sh' *) | wc -l
2016-02-15 11:15:48
User: Natureshadow
Functions: egrep grep wc
Tags: git grep count code
0

Uses git grep for speed, relies on a valid she-bang, ignores leading whitespace when stripping comments and blank lines

ps -eo pmem,comm | grep java | awk '{sum+=$1} END {print sum " % of RAM"}'
2016-02-10 09:00:56
User: bugmenot
Functions: awk grep ps sum
5

This command will add up RAM usage of all processes whose name contains "java" and output the sum of percentages in HRF. Also, unlike the original #15430, it wont fail on processes with a usage of >9.9%.

Pleases note that this command wont work reliably in use cases where a significant portion of processes involved are using less than 0.1% of RAM, because they will be counted as "0", even though a great number of them could add up to significant amounts.

adb shell "screencap -p | base64" | sed 's/\r$//' | base64 -d | display
2016-02-07 18:46:35
User: T4b
Functions: sed
4

Dependencies on phone: adb access, screencap command, base64 command.

Dependencies on computer: adb, sed, base64, display (from imagemagick, but can substitute other image viewer which reads from stdin).

This should work around adb stupidies (i.e. that it replaces \n with \r\n) with base64.

sudo system-image-cli --switch ubuntu-touch/rc-proposed/ubuntu --build 0
2016-02-07 18:24:28
User: bugmenot
Functions: sudo
Tags: ubuntu-phone
0

Get the latest and hopefully greatest to test out on the Ubuntu Phone - sometimes broken but always interesting. https://developer.ubuntu.com/en/start/ubuntu-for-devices/image-channels/

sudo mount -o remount,rw /
2016-02-07 17:57:19
User: bugmenot
Functions: mount sudo
0

Warning this will allow you to write to the system image on the phone, not recommended. But sometimes useful.

gsettings reset com.canonical.Unity.Dash favorite-scopes
2016-02-07 17:50:19
User: bugmenot
Functions: reset
0

To fix this bug: https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1389698

fortune | cowsay -f sodomized-sheep
wget https://open.uappexplorer.com/api/download/openstore.mzanetti/openstore.mzanetti_*_armhf.click && pkcon install-local --allow-untrusted openstore.mzanetti_*_armhf.click
2016-02-04 14:24:46
User: bugmenot
Functions: wget
1

This will download and install the latest version of the open store on the ubuntu phone, this store includes unconfined applications such as the TweakGeek and the Ubuntu Touch Tweak Tool. You can see the install instructions from here: https://open.uappexplorer.com/docs#install

ssh-keygen -b 4048 -t rsa -C "comment"
2016-02-03 09:37:39
User: erez83
Functions: ssh ssh-keygen
Tags: ssh centos
0

Generating ssh key

then need to copy public key in to /root/.ssh/authorized_keys

$if [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]]; then <command when up>; else <command when down>; fi;
2016-02-02 13:06:51
User: paulera
0

This command telnet and and looks for a line starting with "SSH" - works for OpenSSH since the SSH banner is something like "SSH-2.0-OpenSSH_6.0p1 Debian-4+deb7u3". Then it triggers an action accordingly.

It can be packed as a script file to echo 0/1 indicating the SSH service availability:

if [[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]]; then echo 1; else echo 0; fi;

Alternative uses:

Trigger an action when server is UP (using &&):

[[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]] && <command when up>

Trigger an action when server is DOWN (using ||):

[[ "$(sleep 1 | telnet -c <host> <port> 2>&1 | grep '^SSH')" == SSH* ]] || <command when down>
tcpdump -nqt -s 0 -A -i eth0 port 5060
ip=$(curl ip.pla1.net)
sudo strace -e trace=network -p `pidof nginx | sed -e 's/ /,/g'`
2016-01-28 18:48:16
Functions: sed strace sudo
1

Nginx (and other webservers like Apache) can be awkward to trace. They run as root, then switch to another user once they're ready to serve web pages. They also have a "master" process and multiple worker processes.

The given command finds the process IDs of all Nginx processes, joins them together with a comma, then traces all of them at once with "sudo strace." System trace output can be overwhelming, so we only capture "networking" output.

TIP: to kill this complex strace, do "sudo killall strace".

Compare with a similar command: http://www.commandlinefu.com/commands/view/11918/easily-strace-all-your-apache-processes

pacman -Ss python | paste - - | grep --color=always -e '/python' | less -R
2016-01-25 14:29:31
User: hute37
Functions: grep less paste python
Tags: less paste pacman
1

Alternative1 (grep support):

pacman -Ss python | paste - - | grep --color=always -e '/python' | less -R

Alternative2 (eye-candy, no grep):

pacman --color=always -Ss "python" | paste - - | less -R

in ~/.bashrc:

pkg-grep() { pacman -Ss "$1" | paste - - | grep --color=always -e "${2:-$1}" | less -R ; }

pkg-search() { pacman --color=always -Ss "python" | paste - - | less -R; }