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

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,179 results
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.

find /dev/disk/by-id -type l -printf "%l\t%f\n" | cut -b7- | sort
ls -l /dev/disk/by-id |gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }' |sort
2015-05-18 15:42:33
User: lig0n
Functions: gawk ls
Tags: zfs disk info
2

Scrap everything and use `gawk` to do all the magic, since it's like the future or something.

gawk 'match($11, /[a-z]{3}$/) && match($9, /^ata-/) { gsub("../", ""); print $11,"\t",$9 }'

Yank out only ata- lines that have a drive letter (ignore lines with partitions). Then strip ../../ and print the output.

Yay awk. Be sure to see the alternatives as my initial command is listed there. This one is a revision of the original.

find . -name '._*' -type f -delete
2015-05-16 18:12:50
User: MarcLaf
Functions: find
Tags: mac os x
2

Searches from present dir forward and removes all Mac generated . (dot) files.

tar c folderName | md5
grep MemTotal: /proc/meminfo # display how much memory installed
2015-05-15 09:19:02
User: mpb
Functions: grep
0

This will display the system memory size in kb. If you want to see the value in mb, you can type:

grep MemTotal: /proc/meminfo | awk '{printf("MemTotal: %d MB\n",$2/1024)}'

showip() { nmcli connection show $1|grep ipv4.addresses|awk '{print $2}' ; }
2015-05-13 16:24:28
User: nnsense
Functions: awk grep
1

Sometimes it's useful to output just the ip address. Or some other information, changing the "ipv4.addresses" in command. The power of awk! Show all possible "greps" with

nmcli connection show [yourInterfaceNameHere]
grep page.php /var/log/httpd/access_log|awk '{print $1}'|sort|uniq|perl -e 'while (<STDIN>){chomp; $cmd=`ipset add banned -! -q $_`; }'
if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then sudo apt-get update fi
2015-05-12 14:45:11
User: gargolito
Functions: date expr stat sudo
0

I have this in my .bash_aliases and call it before running apt-get install or apt-get upgrade

Example:

alias apt-install='apt-update; apt-get install'

alias apt-upgrade='apt-update; apt-get upgrade'

function apt-update () {

if [[ $(expr $(date +%s) - $(stat -c %X /var/lib/apt/periodic/update-success-stamp)) -gt 86400 ]]; then

sudo apt-get update

else

echo apt is up to date

fi

}

col_look(){ column -nts$'\t' "$1" | less -SN#2 }
2015-05-11 18:17:29
User: razerwolf
Functions: column less
-2

Opens up a tab separated file in less with columns printed prettily

bind '"<ctrl+v><functionKey>":"command\n"'
2015-05-11 17:59:09
User: nnsense
0

This is a common use of bind. Hitting any key after will output the key's character sequence. This makes possible using it into a bind command. So pressing ctrl+v and then F2 will output "^[[12~", once binded every time you'll press the function key F2 it will execute your command. Added the \n to make it execute it as well.

mosth() { history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10; }
2015-05-11 17:41:55
User: nnsense
Functions: awk column grep head nl sort
0

I copied this (let's be honest) somewhere on internet and I just made it as a function ready to be used as alias. It shows the 10 most used commands from history. This seems to be just another "most used commands from history", but hey.. this is a function!!! :D

fileinfo() { RPMQF=$(rpm -qf $1); RPMQL=$(rpm -ql $RPMQF);echo "man page:";whatis $(basename $1); echo "Services:"; echo -e "$RPMQL\n"|grep -P "\.service";echo "Config files:";rpm -qc $RPMQF;echo "Provided by:" $RPMQF; }
2015-05-11 16:46:01
User: nnsense
Functions: basename echo grep rpm whatis
2

Many times I give the same commands in loop to find informations about a file. I use this as an alias to summarize that informations in a single command. Now with variables! :D

qf2s() { rpm -ql $(rpm -qf $1)|grep -P "\.service"; }
2015-05-11 16:32:16
User: nnsense
Functions: grep rpm
1

I use this as an alias to get all .service files related a single installed file/conf (if it has services, of course).

For rpm based systems ;)