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.


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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.
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,427 results
ps aux | grep -v name_you_want_to_hide
2017-11-21 04:22:12
Functions: grep ps
0

The classical 'ps aux | grep' can do this with one more parameter, the '-v', with means 'NOT' to grep.

exec -a "$(ps -fea | awk '{print $8}'| sort -R | head -n1)" your_command -sw1 -sw2
2017-11-19 04:36:24
User: pulketo
Functions: exec
0

Hides the process "your_command" from showing with ps, displaying some other random process name already running for a better camouflage.

pdftk in.pdf cat 1-endeast output out.pdf
ffmpeg -re -i "index.m3u8" -acodec libmp3lame -ar 44100 -b:a 128k -pix_fmt yuv420p -profile:v baseline -s 426x240 -bufsize 6000k -vb 400k -maxrate 1500k -deinterlace -vcodec libx264 -preset veryfast -g 30 -r 30 -f flv "rtmp://rtmp-api.facebook.com"
2017-10-29 03:12:27
User: siamware
0

ffmpeg covert m3u8 to facebook live stream

ffmpeg -re -y -i mm.mp4 -b:a 128k -vcodec libx264 -pix_fmt yuv420p -vf scale=640:480 -r 30 -g 60 -f flv "rtmp://rtmp-api.facebook.com:80/rtmp/xxxxxxxxxx"
2017-10-29 02:10:31
User: siamware
0

ffmpeg mp4 to facebook live steam

[command] 2> >(fb=$(dd bs=1 count=1 2>/dev/null | od -t o1 -A n); [ "$fb" ] && err=$(printf "\\${fb# }"; cat) && echo "ERROR - $err")
2017-10-16 22:22:42
User: tyzbit
Functions: dd echo od printf
0

This is a bit of a bash hack to catch STDERR and append a log level to it.

So for example, if your script has pseudo loglevels like so:

echo "INFO - finding files"

[ -f ${files} ] || echo "WARN - no files found"

Any subcommands that write to STDERR will screw that up

Adding 2> >(fb=$(dd bs=1 count=1 2>/dev/null | od -t o1 -A n); [ "$fb" ] && err=$(printf "\\${fb# }"; cat) && echo "ERROR - $err") to the command does the following:

2>

Redirect STDERR

>(

Spawn a subshell (STDERR is then redirected to the file descriptor for this subshell)

fb=$(....)

get the first byte of input

[ "$fb" ]

test if there's a first byte

&& err=$(printf....)

save the output to the $err variable

&& echo "ERROR - $err"

append your pseudo loglevel and the error message

Heavily borrowed from https://unix.stackexchange.com/questions/33049/check-if-pipe-is-empty-and-run-a-command-on-the-data-if-it-isnt

while [ true ]; do cat /proc/loadavg | perl -ne 'm/(^[^ ]+)/; $L = $1; print "*" x $L; print " $L\n";' ; sleep 6; done
2017-10-13 06:47:06
User: t3o
Functions: cat perl sleep true
0

It takes the first value of /prov/loadavg to print that many stars followed by the value.

dockpage() { lynx -width=180 --dump https://docs.docker.com/v1.11/engine/reference/commandline/$1/ | sed -n '/^Usage/,/On this page/{/On this page/b;p}'; }
2017-09-18 23:53:34
User: nnsense
Functions: sed
0

Docker's local man pages are (often) half of what you have online, so I wanted that as local man.

Install lynx and run my oneliner, then use as follows:

dockpage

Adjust lynx's page width at will

alias dockps='docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}"'
2017-09-18 23:46:47
User: nnsense
Functions: alias ps
0

Usefull, for example, when many ports are exposed and the docker ps output looks cluttered.

chage -M -1 root; echo "" > /etc/security/opasswd
2017-07-30 21:26:43
User: tsener
Functions: chage echo
0

Disable password expiration and clear password history for VMware vcenter appliance

socat "UNIX-LISTEN:/tmp/mysqld.temp.sock,reuseaddr,fork" EXEC:"ssh [email protected] socat STDIO UNIX-CONNECT\:/var/run/mysqld/mysqld.sock"
2017-07-27 11:32:19
User: paulera
0

Won't work with password login. You must add your RSA key to the server's authorizedkeys file, or change the ssh command adding the -i option for a custom RSA key:

socat "UNIX-LISTEN:/tmp/mysqld.temp.sock,reuseaddr,fork" EXEC:"ssh [email protected] -i /home/user/rsa-keys/id_rsa socat STDIO UNIX-CONNECT\:/var/run/mysqld/mysqld.sock"

---

/tmp/mysqld.temp.sock will be created locally by socat, don't create it yourself. The folder it lives must be writable. Connect your MySQL client to this socket, with database and username set properly.

---

In case you need to forward a remote socket to a LOCAL PORT instead, check http://www.commandlinefu.com/commands/view/9436/socat-tcp-listen5500-execssh-userremotehost-socat-stdio-unix-connectvarrunmysqldmysqld.sock

openssl rand -base64 12
numfmt --to=iec $[$(du -s /home/*/docs | cut -f 1 | paste -sd+)]
2017-07-15 00:53:20
User: odoepner
Functions: cut du paste
0

For example to add up the disk usage at several disjoint locations.

The $[..] is for arithmetic evaluation in bash.

Alternatively pipe to the bc command.

curlh() { x="$(curl -Is -w '%{http_code}' "[email protected]")"; if [[ "$(tail -n 1 <<< "$x")" == [45]* ]]; then curl -is "[email protected]" | awk '{ if (!NF) { exit }; print }'; else head -n -1 <<< "$x"; fi; }
2017-07-10 12:29:00
User: nyuszika7h
Functions: awk exit head
0

Some sites running on basic web servers don't support the HEAD request, so using "curl -I" on them doesn't work. This will automatically try "curl -I" at first and if that fails with a 4xx or 5xx status code, it falls back to "curl -i" and prints only the headers from that.

find . -name '*.log' | xargs ls -hlt > /tmp/logs.txt && vi /tmp/logs.txt
for domain in $(cat dominios.txt); do nslookup -type=mx $domain ; done >ejemplo.txt
grep -oP '(?<=www\.)\s?[^\/]*' file | uniq
2017-07-04 20:18:40
User: nedzoul
Functions: file grep
0

Obtener nombre de dominio de un archivo

csv2json() { for file in [email protected]; do python -c "import csv,json,fileinput; print(json.dumps(list(csv.reader(fileinput.input()))))" "$file" 1> "${file%%csv}json"; done; }
2017-06-28 04:28:32
User: snipertyler
Functions: file python
Tags: CSV python json
0

Based / Inspired by malathion's below command

http://www.commandlinefu.com/commands/view/20528/convert-csv-to-json

Is written for python3 and is very easy to use

csv2json *csv will convert all files ending in csv to json

eg csv2json file.csv will output a file to file.json

Validity of json tested in python3 and below site

https://jsonformatter.curiousconcept.com/

echo "$(obase=16; echo "$(date +%s)" | bc | xxd -r -p | base32)"
echo "http://commandlinefu.com" | curl -F-=\<- qrenco.de
phase() { while :; do for n in E4 F#4 B4 C#5 D5 F#4 E4 C#5 B4 F#4 D5 C#5; do /usr/bin/play -qn synth $1 pluck $n; done; echo -n "[$1]"; done; }; phase 0.13 & phase 0.131 &
2017-06-14 20:29:26
User: rodolfoap
Functions: echo
0

The Piano Phase piece, by Steve Reich is a minimalist composition which is played on two pianos played at slightly different tempos, a task that's very difficult to accomplish by human players. The auditive effects produced by the cell displacement produce beautiful patterns. See https://en.wikipedia.org/wiki/Piano_Phase . My rendered version: https://ydor.org/SteveReich/piano_phase.mp3

Requires sox to be installed on the system.

There are multiple videos on youtube showing different approaches and experiences to this interpretation. There is also a synthesized version.

Even if Bash can behave as a powerful pianist, a simple threaded version leaves full room to several time glitches and even negative displacements, the same issues that human pianists experience when playing the piece. The older the computer, the better the chaos added to the result due to the CPU load. Apparently that's the reason Steve Reich composes pieces such as this.

Without further ado, please give a warm welcome to the Bash minimalist player on synthesized two-threaded pianos. Please turn off your cellphones.

scrypt(){ [ -n "$1" ]&&{ echo '. <(echo "$(tail -n+2 $0|base64 -d|mcrypt -dq)"); exit;'>$1.scrypt;cat $1|mcrypt|base64 >>$1.scrypt;chmod +x $1.scrypt;};}
2017-06-14 16:27:20
User: rodolfoap
Functions: cat chmod echo
2

This function will encrypt a bash script and will only execute it after providing the passphrase. Requires mcrypt to be installed on the system.

cat hello

#!/bin/bash

case "$1" in

""|-h) echo "This is the fantastic Hello World. Try this:" $(basename $0) "[entity]" ;;

moon) echo Good night. ;;

sun) echo Good morning. ;;

world) echo "Hello, world!" ;;

*) echo Hi, [email protected] ;;

esac

scrypt hello

Enter the passphrase (maximum of 512 characters)

Please use a combination of upper and lower case letters and numbers.

Enter passphrase:

Enter passphrase:

Stdin was encrypted.

cat hello.scrypt

. <(echo "$(/usr/bin/tail -n+2 $0|base64 -d|mcrypt -dq)");exit;

AG0DQHJpam5kYWVsLTEyOAAgAGNiYwBtY3J5cHQtc2hhMQAV34412aaE8sRzQPQzi09YaNQPedBz

aGExAARvB6A/HYValW4txoCFmrlp57lmvhKBbM4p+OUiZcCxr6Y+Mm7ogg3Y14pHi0CrfT70Tubq

9g8/kNJrQr7W/ogHpVuOOdD0YfuRatrV7W2+OlNQ63KX780g4qTHrTqNnyLp8sF5RQ7GwxyZ0Oti

kROtVIU4g4+QAtn/k/e7h7yt4404VF1zzCYRSw20wmJz1o/Z0XO7E/DFBr5Bau7bWjnF7CRVtims

HGrDwv1miTtAcyB9PknymDxhSyjDUdNhqXGBIioUgqjX1CKgedtO0hQp050MiQd3I6HacpSrVUIW

kuuS+BtMrxHDy+48Mh1hidV5JQFP7LP5k+yAVLpeHd2m2eIT1rjVE/Bp2cQVkpODzXcWQDUAswUd

vulvj/kWDQ==

./hello

This is the fantastic Hello World. Try this: hello [entity]

./hello.scrypt

Enter passphrase:

This is the fantastic Hello World. Try this: hello.scrypt [entity]

./hello world

Hello, world!

./hello.scrypt world

Enter passphrase:

Hello, world!

bosh deployments --json | jq '.Tables[0].Rows[][0]' --raw-output
python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
2017-05-17 19:35:53
User: malathion
Functions: python
Tags: CSV python json
3

Replace 'csv_file.csv' with your filename.

ps aux | awk '{if ($8 ~ "D") print $0}'