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,420 results
[ ! -t 0 ] && [ -t 1 ] && exec 0<&1
2017-04-12 00:10:27
User: pdxdoughnut
Functions: exec
0

Tests to see if stdin (0) is NOT a terminal.

Tests to see if stdout (1) IS a terminal.

Copies stdout (opened for write) to stdin (open for read).

soxi -D * | awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
function cdb() { select dir in $(find -type d -name "$1" -not -path '*/\.*' -prune); do cd "${dir}" && break; done }
sudo security delete-certificate -c 1000-sans.badssl.com
2017-04-06 01:40:04
User: lgarron
Functions: sudo
0

Use `security list-keychains` to find possible keychains, and `security dump-keychain` to look for certificates.

`sudo security delete-certificate` without arguments shows options for removing: by common name or by SHA-1 hash.

This is especially useful if a certificate locks up Keychain Access.app, which happens for certificates with many SANs in macOS 10.12.

Found via: http://stackoverflow.com/a/16019443

cat externs.json | jq ".efExports | .[] | (keys|.[0]) as \$kind | {kind:\$kind,value:(.[\$kind] |.Ident?)}"
grep -i s2enc /etc/vcac/server.xml | sed -e 's/.* password=\"\([^\"]*\)\".*/\1/' | xargs -n 1 vcac-config prop-util -d --p 2>/dev/null; echo
ps -ef | grep [p]mon | awk '{print $2}' | xargs -I {} ps eww {} | awk '{print $1 " " $5 " " $6 " " $0}' | sed 's/\(S*\) \(S*\) .*ORACLE_HOME/\1 \2/g' | cut -f1,2,3 -d" "
2017-03-24 18:45:40
User: Enirevlow
Functions: awk cut grep ps sed xargs
0

Show the executable that spawned the process and show the PID and ORACLE_HOME relative to the environment within which the process is running.

sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }
2017-03-21 23:31:25
User: pdxdoughnut
Functions: echo printf
3

Converts any number of seconds into days, hours, minutes and seconds.

sec2dhms() {

declare -i SS="$1"

D=$(( SS / 86400 ))

H=$(( SS % 86400 / 3600 ))

M=$(( SS % 3600 / 60 ))

S=$(( SS % 60 ))

[ "$D" -gt 0 ] && echo -n "${D}:"

[ "$H" -gt 0 ] && printf "%02g:" "$H"

printf "%02g:%02g\n" "$M" "$S"

}

shebang () { printf '%s\n' 0a '#!'"$1" . w | ed -s "$2" ; }
2017-03-20 19:14:41
User: zlemini
Functions: ed printf
1

Function to add a shebang to an existing script, handy if you forgot to add it in the first place.

find . -type f ! -path "./.git/*" -exec sh -c "head -n 1 {} | egrep -a 'bin/bash|bin/sh' >/dev/null" \; -print -exec shellcheck {} \;
2017-03-16 08:43:56
User: brx75x
Functions: find sh
5

This is a commodity one-liner that uses ShellCheck to assure some quality on bash and sh scripts under a specific directory. It ignores the files in .git directory.

Just substitute "./.git/*" with "./.svn/*" for older and booring centralized version control.

Just substitute ShellCheck with "rm" if your scripts are crap and you want to get rid of them :)

videospeed() { vname="$1"; speedc="$2"; vs=$(python3 -c "print(1/$speedc)"); aspeed=$(python3 -c "print(1*$speedc)"); ffmpeg -i "$vname" -filter:a "atempo=$aspeed" -filter:v "setpts=$vs*PTS" "${3:-converted_$1}"; }
2017-03-07 22:44:06
User: snipertyler
4

Usage:

videospeed video_filename speedchange newfilename

videospeed foo.mp4 0.5 foo_slow.mp4

Range of 0.5 (50%) - 2.0 (200%) is valid.

lc() { od="$1"; nd="$2"; of=$3; nf=$4; cp -rl "$od" "$nd"; parallel -0 "ffmpeg -i {1} -loglevel error -q:a 6 {1.}.{2} && { rm {1}; echo {1.}.{2}; }" :::: <(find "$nd" -type f -iname \*$of -print0) ::: "$nf"; }
2017-03-02 17:37:34
User: snipertyler
Functions: cp find
3

Uses parallel processing

Reiteration of my earlier command

https://www.commandlinefu.com/commands/view/15246/convert-entire-music-library

Usage

lc Old_Directory New_DIrectory Old_Format New_Format

lc ~/Music ~/Music_ogg mp3 ogg

sudo pv -tpreb /path/to/source | sudo dd bs=4096 of=/path/to/destination
2017-03-01 19:11:57
Functions: dd sudo
3

Uses the pv utility to show progress of data transfer and an ETA until completion.

You can install pv via Homebrew on macOS

tar -zcvf archive-name.tar.gz directory-name
wait_for_this.sh; echo "wait_for_this.sh finished running" | mail -s "Job Status Update" [email protected]
rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php*
gcp -r -l source/ destination/
2017-02-09 23:48:38
User: fivestones
2

This will move a folder and merge it with another folder which may contain duplicates. Technically it's just creating hardlinks of everything in the folder, and after it's done, delete the source (with rm -r source/ ) to complete the move. This is much faster than, for example, using rsync to merge folders which would actually copy the entire contents and so for a lot of files would take much longer.

This uses macutils gcp port of cp so it can be used on osx/MacOS. If using in linux or some unix where cp includes the ability to create links with -l you can just use cp instead of gcp.

find -wholename "*/query/*.json"
curl -s http://public-dns.info/nameserver/br.csv| cut -d, -f1 | xargs -i timeout 1 ping -c1 -w 1 {} | grep time | sed -u "s/.* from \([^:]*\).*time=\([^ ]*\).*/\2\t\1/g" | sort -n | head -n1
2017-02-02 12:00:38
User: glaudiston
1

http://public-dns.info gives a list of online dns servers. you need to change the country in url (br in this url) with your country code. this command need some time to ping all IP in list.

stf=$(date +%s.%N);for ((;;));do ctf=$( date +%s.%N );echo -en "\r$(date -u -d "0 $ctf sec - $stf sec" "+%H:%M:%S.%N")";done
stf=$(date +%s.%N);st=${stf/.*/};sn=%{stf/*./};for ((;;));do ctf=$( date +%s.%N );ct=${ctf/.*/};cn=${ctf/*./}; dtf=$(echo "scale=3; $ctf-$stf" | bc); dt=${dtf/.*/}; dt=${dt:=0};echo -en "\r$(date -u -d @$dt "+%H:%M:%S.${dtf/*./}")";done
stf=$(date +%s.%N);st=${stf/.*/};sn=%{stf/*./};for ((;;));do ctf=$( date +%s.%N );ct=${ctf/.*/};cn=${ctf/*./}; echo -en "\r$(echo "scale=3; $ctf-$stf" | bc)";done
2017-01-26 13:20:30
User: glaudiston
Functions: date echo
Tags: chronometer
0

A way for tracking times in bash

favorite --add myhost 'ssh [email protected]'
2017-01-23 18:15:03
User: djangofan
Tags: history
-3

Command 'favorite' to store your favorite commands from history.

find . -name "node_modules" -exec rm -rf '{}' \;
ex -c '1,$-1000d' -c 'wq' file