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

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

Top Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 12,071 results
for a in $(ls /usr/sbin /usr/bin); do ps -fC $a;done|grep -v PPID
2015-04-27 18:15:56
User: knoppix5
Functions: grep ls ps
0

Thanks to pooderbill for the idea :-)

input=a.pdf ; pages=`pdftk $input dump_data | grep -i numberofpages | cut -d" " -f 2`; pdftk A=$input shuffle A1-$[$pages/2] A$pages-$[$pages/2+1] output "${input%.*}.rearranged.${input##*.}"
2015-04-26 20:05:20
User: kobayashison
Functions: cut grep
0

Rearrange pdf document coming from a simplex document feed scanner, feeded first with odd pages, then with even pages from the end. Needs pdftk >1.44 w/ shuffle.

Similar to http://www.commandlinefu.com/commands/view/7965/pdf-simplex-to-duplex-merge where there are 2 separate documents, odd and even

perl -e 'for(;;sleep 1){printf"\r"."%.4b "x6,split"",`date +%H%M%S`}'
timeDNS() { parallel -j0 --tag dig @{} "$*" ::: 208.67.222.222 208.67.220.220 198.153.192.1 198.153.194.1 156.154.70.1 156.154.71.1 8.8.8.8 8.8.4.4 | grep Query | sort -nk5; }
du -sk -- * | sort -n | perl -pe '@SI=qw(K M G T P); s:^(\d+?)((\d\d\d)*)\s:$1." ".$SI[((length $2)/3)]."\t":e'
2015-04-26 08:07:27
Functions: du perl sort
0

Tested on MacOS and GNU/Linux.

It works in dirs containing files starting with '-'.

It runs 'du' only once.

It sorts according to size.

It treats 1K=1000 (and not 1024)

sudo npm cache clean -f | sudo npm install -g n | sudo n stable
git rev-list --reverse --topo-order master... | while read rev; do git checkout preview; git cherry-pick $rev || break; done
2015-04-23 14:28:06
User: shadyvb
Functions: read
Tags: git preview
-2

Creating feature-branches off master, and trying to merge them in an integration branch (preview), sometimes causes conflicts because the feature-branch might hold changes from 'master' that aren't on preview yet. So this ensures only the commits added to the feature-branch are moved to integration (preview).

Note: This assumes you're currently on the feature-branch. Adjust 'master/preview' branch names to suit your environment.

tail +### MYFILE
2015-04-23 11:49:15
User: pooderbill
Functions: tail
1

Useful for finding newly added lines to a file, tail + can be used to show only the lines starting at some offset. A syslog scanner would look at the file for the first time, then record the end_of_file record number using wc -l. Later (hours, days), scan only at the lines that were added since the last scan.

Blocking ip: arp -s ip_of_host 0, Unblocking ip: arp -d ip_blocked
2015-04-22 19:00:13
User: andregyn62
Functions: arp
0

When you block any hosts using this method, the hosts can't do anything in the network.

The block is applied on firewall or gateway of the network.

view + LOGFILE
2015-04-21 11:23:01
User: pooderbill
0

view is the command: vi -r which opens a file in read-only mode. The + character jumps to the bottom of the logfile where the most relevant information starts. Other aliases can be created for commonly viewed logfiles.

git diff --word-diff --color-words | aha > index.html && firefox index.html
ps -fC PROCESSNAME
2015-04-20 13:09:44
User: pooderbill
Functions: ps
Tags: grep function ps
9

ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.

openssl req -nodes -newkey rsa:2048 -keyout server.key -out server.csr -subj "/C=BR/ST=State/L=City/O=Company Inc./OU=IT/CN=domain.com"
fold -sw 20 <(echo "Long Text to be wrapped with \"\n\"") |sed ':a;N;$!ba;s/ *\n/\\n/g'
2015-04-16 21:06:53
User: alecthegeek
Functions: echo fold sed
0

I used this fragment with Imagemagick convert so that I can place long text strings in pictures. The "\n" gets converted to a true newline in the image.

So this fragment uses fold command to wrap the line and then sed to convert newlines (and any trailing spaces on the line) to the text "\n"

find -name pom.xml | while read f; do cd $(dirname "$f"); mvn clean; cd -; done;
2015-04-15 21:24:49
User: glaudiston
Functions: cd dirname find read
-2

this command is used to locate all pom.xml files, access the dir and do a mvn clean, but I do recommend you to disable network interfaces to not download dependencies packages to be faster.

awk -F"|" 'BEGIN {OFS="|"} NR==1 {for (b=1;b<=NF;b++) {hdr[b]=$b} } NR > 1 {for (i=1;i<=NF;i++) {if(length($i) > max[i]) max[i] = length($i)} } END {for (i=1;i <= NF;i++) print hdr[i],max[i]+0}' pipe_delimited_file.psv
find /PATHNAME -type l | while read nullsymlink ; do wrongpath=$(readlink "$nullsymlink") ; right=$(echo "$wrongpath" | sed s'|OLD_STRING|NEW_STRING|') ; ln -fs "$right" "$nullsymlink" ; done
2015-04-14 14:58:41
User: iDudo
Functions: echo find ln read readlink sed
0

After you run this script, you can check status for broken symlink with this command:

find -L . -type l

docker stop $(docker ps -a -q); docker rm $(docker ps -a -q)
2015-04-14 13:34:15
User: das_shark
Functions: ps rm
3

Will stop all running containers, then remove all containers

**This isn't for selectively handling containers, it removes everything**

env DISPLAY=:0 /usr/bin/gedit ~/df.txt && wmctl -a gedit
2015-04-12 13:48:31
User: knoppix5
Functions: env
0

Usage example: display output of a command running in the background at desired time

The example in details: report disk quotas and that backup process will start soon

In my /etc/crontab file I added following four lines for weekly automatic incremental backup:

.

52 13 * * 7 root mount /dev/sda3 /media/da2dc69c-92cc-4249-b2c3-9b00847e7106

.

53 13 * * 7 knoppix5 df -h >~/df.txt

.

54 13 * * 7 knoppix5 env DISPLAY=:0 /usr/bin/gedit ~/df.txt && wmctl -a gedit

.

55 13 * * 7 root /home/knoppix5/rdiff-backup.sh

.

line one: as root mount media for backup on Sunday 13:52

line two: as user knoppix5 write out to text file in home directory the free space of all mounted disks on Sunday 13:53

line three: in front of you open and display a very simple text editor (I prefer gedit) with content of previously reported disk usage at Sunday 13:54

wmctl -a gedit means (from the manual):

-a Switch to the desktop containing the window , raise the window, and give it focus.

line four: as root run incremental backup script rdiff-backup.sh as root on Sunday 13:54

.

my rdiff-backup.sh, with root permissions backups in short time (writes only changes from the last backup) the etire linux system (except excluded - i.e. you don't want backup recursively your backup disk), looks like this (Show sample output):

netstat -anp | grep :80 | grep ESTABLISHED | wc -l
2015-04-10 19:32:31
User: krizzo
Functions: grep netstat wc
Tags: session
-1

This counts all established sessions on port 80. You can change :80 to any port number you want to check.

sudo lsof -i -n | grep sshd | grep sshuser | grep :[PORT-RANGE] | grep -v IPv6 | awk -F\: '{print $2}' | grep -v http | awk -F" " '{print $1}'
2015-04-09 15:41:11
User: das_shark
Functions: awk grep sshd sudo
-1

gets network ports

only ones for the sshd service

only logged in a specific user (changed for public posting)

only in a specific localhost:port range

not IPv6

Only the part of the response after the ":" character

Only the part of the response before the 1st space

Output is just the rssh port

debugfs -R "stat <$(stat --printf=%i filename)>" /dev/sdaX | grep crtime
2015-04-09 01:23:56
User: pggx999
Functions: debugfs grep
3

Return the creation date of a file on ext2, 3, 4 filesystems, because stat command won't show it.

Useful on ubuntu, debian, and else

runonchange () { local cmd=( "$@" ) ; while inotifywait --exclude '.*\.swp' -qqre close_write,move,create,delete $1 ; do "${cmd[@]:1}" ; done ; }
2015-04-08 17:42:03
User: funollet
1

Example:

runonchange /etc/nginx nginx -t

Ignores vim temp files. Depends on 'inotify-tools' for monitoring of file changes. Alternative to tools like 'entr', 'watchr'.

awk '{print $0+0}' <(echo -2; echo +3;)
2015-04-08 09:19:24
Functions: awk echo
-1

The leading plus sign is removed - Minus sign is left intact

a=$(b=$(($LINES/2));f() { for c in $(seq $b); do for i in $(seq $c);do echo x;done|xargs echo;done };paste <(f) <(f|tac|tr 'x' '-') <(f|tac|tr 'x' '-') <(f)|tr '\t' ' ');(cat <<<"$a"|tac;cat <<<"$a")|tr '-' ' '