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,330 results
xmlpager() { xmlindent "$@" | awk '{gsub(">",">'`tput setf 4`'"); gsub("<","'`tput sgr0`'<"); print;} END {print "'`tput sgr0`'"}' | less -r; }
2015-07-12 09:22:10
User: hackerb9
Functions: awk less
0

Don't want to open up an editor just to view a bunch of XML files in an easy to read format? Now you can do it from the comfort of your own command line! :-) This creates a new function, xmlpager, which shows an XML file in its entirety, but with the actual content (non-tag text) highlighted. It does this by setting the foreground to color #4 (red) after every tag and resets it before the next tag. (Hint: try `tput bold` as an alternative). I use 'xmlindent' to neatly reflow and indent the text, but, of course, that's optional. If you don't have xmlindent, just replace it with 'cat'. Additionally, this example shows piping into the optional 'less' pager; note the -r option which allows raw escape codes to be passed to the terminal.

git diff $(git log --pretty=format:%h -2 --reverse | tr "\n" " ")
2015-07-07 17:50:50
Functions: diff tr
Tags: git
1

A great command to assign to an alias, allowing you to git diff the last two commits in git.

for file in $( git ls-files ); do echo $file; touch -t $(git --no-pager log --date=local -1 --format="%ct" $file | php -r 'echo @date( "YmdHi.s", trim( file_get_contents( "php://stdin" ) ) );') $file; done
echo "display:data?" | nc "$scope_ip_address" 5555 | dd bs=1 skip=11 2>/dev/null | convert bmp:- out_file.png
2015-06-27 06:43:43
User: ichbins
Functions: dd echo
Tags: convert rigol
1

Scope should have the Rigol Ultravision Technology otherwise it won't accept the command. ImageMagic is required. Scope sends a 1.1M BMP file and converted to PNG it's only 18-20K

ssh -o "ProxyCommand ssh [email protected]_host -W %h:%p" [email protected]_host
2015-06-26 06:23:14
User: renich
Functions: ssh
Tags: ssh
4

This command uses the reachable_host as a proxy; redirecting your request to the unreachable_host.

The main advantage is that you need only one ssh key. You copy the public part to both servers and you can access the unreachable_host without a problem.

Also, you can put this on ~/.ssh/config to access the unreachable_host directly:

# config

unreachable_host

ProxyCommand ssh [email protected]_host -W %h:%p

and, then, just ssh [email protected]_host.

jhead -autorot -ta+1 *
exiftool -AllDates+=1 -{Track,Media}{Create,Modify}Date+=1 *.mov
diff a.txt b.txt | grep -E '^(<|>)' | sed 's:^< \(.*\):<del style="color\:red; text-decoration\: none">- \1</del><br>:' | sed 's:^> \(.*\):<ins style="color\:green; text-decoration\: none">+ \1</ins><br>:'
ps -auxf | sort -nr -k 4 | head -10
pm2 install pm2-webshell
2015-06-11 12:43:49
User: strzel_a
Functions: install
-11

pm2 is a process manager that includes a module system to install powerful and small softwares

tree -a -L 1 --inodes /
tail -f <file>
2015-06-01 16:55:00
User: adrruiz
Functions: tail
-12

In cases when the user is following a log file that rotates then it is advisable to use the -F option as it keeps following the log even when it is recreated, renamed, or removed as part of log rotation.

To interrupt tail while it is monitoring, break-in with Ctrl+C.

less +F <file>
find <path> |xargs grep <pattern>
cat <file>|column -t
awk '{for(i=2;i<=NF;i++) printf("%s%s",$i,(i!=NF)?OFS:ORS)}'
awk '{ $1="";print}'
xxd -p source | fold -w2 | paste -sd' ' | sed "s/A/B/g" | xxd -p -r > destination
2015-05-26 18:29:48
User: hincor
Functions: fold paste sed
Tags: sed xxd fold paste
2

Replace all instances of "A" with "B" in file "source" saved as file "destination".

!! IF A/B is multi-byte, then separate bytes with spaces like so: "s/20\ 0A/00/g".

aplay -c 2 -f S16_LE -r 44100 /dev/urandom
ls -lt --time=atime *.txt
2015-05-21 21:03:44
User: miccaman
Functions: ls
Tags: ls
1

list all txt files order by time, newest first

for f in `ls`; do sed -i '/MATCHING STRING/ { s/ORIGINAL/REPLACEMENT/; }' ${f} ; done
2015-05-21 19:37:42
User: krizzo
Functions: sed
-1

Find and replace specific characters in a single line in multiple files with sed.

for x in {a..d}; do echo -e "n\np\n\n\n\nt\n8e\nw\n" | fdisk /dev/sd"$x"; done
2015-05-21 12:59:48
User: jaimerosario
Functions: echo fdisk
Tags: fdisk for loop
1

So, I'm using a CentOS VM in VirtualBox, and created four new disks in the SCSI controller.

The VM created the folders:

/dev/sda

/dev/sdb

/dev/sdc

/dev/sdd

Using a 'for loop' all disks are partitioned for LVM.

sudo docker rm $(docker ps -a -q); sudo docker rmi $(docker images -q)
2015-05-20 12:34:40
User: lpalgarvio
Functions: ps rm sudo
3

# Delete all containers

docker rm $(docker ps -a -q)

# Delete all images

docker rmi $(docker images -q)

avconv -i FILE.mp4" -f dvd -c:v:0 mpeg2video -s 720x576 -r 25 -g 15 -b:v:0 8000000 -maxrate:v:0 8000000 -minrate:v:0 8000000 -bufsize:v:0 1835008 -packetsize 2048 -muxrate 10080000 -b:a 192000 -ar 48000 -c:a:0 ac3 -map 0:v -map 0:a FILE.mpeg
2015-05-20 09:55:48
User: alikhalil
0

The command will use avconv to convert any video file in to MPEG2 format which will be added to a DVD project.

for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$p/environ 2>/dev/null); [[ -n $d ]] && break; done; echo $d
2015-05-18 20:01:20
User: geyslan
Functions: awk cat echo
Tags: display xorg
1

It's useful when you cannot access your env (systemd) or the process DISPLAY variable is not set. Perhaps also when you have a multi-head/user configuration.