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.

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

Tags

Hide

Functions

Commands by flatcap from sorted by
Terminal - Commands by flatcap - 33 results
FILE=file_name; CHUNK=$((64*1024*1024)); SIZE=$(stat -c "%s" $FILE); for ((i=0; i < $SIZE; i+=$CHUNK)); do losetup --find --show --offset=$i --sizelimit=$CHUNK $FILE; done
2014-10-03 13:18:19
User: flatcap
Functions: losetup stat
2

It's common to want to split up large files and the usual method is to use split(1).

If you have a 10GiB file, you'll need 10GiB of free space.

Then the OS has to read 10GiB and write 10GiB (usually on the same filesystem).

This takes AGES.

.

The command uses a set of loop block devices to create fake chunks, but without making any changes to the file.

This means the file splitting is nearly instantaneous.

The example creates a 1GiB file, then splits it into 16 x 64MiB chunks (/dev/loop0 .. loop15).

.

Note: This isn't a drop-in replacement for using split. The results are block devices.

tar and zip won't do what you expect when given block devices.

.

These commands will work:

hexdump /dev/loop4

.

gzip -9 < /dev/loop6 > part6.gz

.

cat /dev/loop10 > /media/usb/part10.bin
sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
2014-04-18 19:12:05
User: flatcap
Functions: sed
5

Show the current load of the CPU as a percentage.

Read the load from /proc/loadavg and convert it using sed:

Strip everything after the first whitespace:

sed -e 's/ .*//'

Delete the decimal point:

sed -e 's/\.//'

Remove leading zeroes:

sed -e 's/^0*//'
find . -name \*.svg -print0 | xargs -0 -n1 -P4 -I{} bash -c 'X={}; convert "$X" "${X%.svg}.png"'
2014-04-11 14:30:30
User: flatcap
Functions: bash find xargs
2

Convert some SVG files into PNG using ImageMagick's convert command.

Run the conversions in parallel to save time.

This is safer than robinro's forkbomb approach :-)

xargs runs four processes at a time -P4

echo thisIsATest | sed -r 's/([A-Z])/_\L\1/g'
2014-04-11 13:36:08
User: flatcap
Functions: echo sed
Tags: sed
1

Convert a camelCase string into snake_case.

To complement senorpedro's command.

for i in *.svg; do convert "$i" "${i%.svg}.png"; done
2014-03-24 14:02:02
User: flatcap
1

Convert some SVG files into PNG using ImageMagick's convert command.

sed -n "/^.\{73,\}/p" < /path/to/file
2014-03-20 12:31:57
User: flatcap
Functions: sed
0

Filter out lines of input that contain 72, or fewer, characters.

"sed -n" : don't print lines by default

"/^.\{73,\}/" : find lines that start with 73 (or more) characters

"p" : print them

while read i; do [ ${#i} -gt 72 ] && echo "$i"; done < /path/to/file
2014-03-20 12:27:06
User: flatcap
Functions: echo read
1

Filter out lines of input that contain 72, or fewer, characters.

This uses bash only. ${#i} is the number of characters in variable i.

watch -d "ls -l /proc/$!/fd"
2014-01-31 23:51:17
User: flatcap
Functions: watch
1

You're running a program that reads LOTS of files and takes a long time.

But it doesn't tell you about its progress.

First, run a command in the background, e.g.

find /usr/share/doc -type f -exec cat {} + > output_file.txt

Then run the watch command.

"watch -d" highlights the changes as they happen

In bash: $! is the process id (pid) of the last command run in the background.

You can change this to $(pidof my_command) to watch something in particular.

echo $(($(find . -name "pattern" -type f -printf "+%s")))
2014-01-16 03:14:36
User: flatcap
Functions: echo find
3

Use find's internal stat to get the file size then let the shell add up the numbers.

ssh USER@HOST cat REMOTE_FILE.mp4 | tee LOCAL_FILE.mp4 | mplayer -
2013-11-28 11:25:26
User: flatcap
Functions: cat ssh tee
6

Securely stream a file from a remote server (and save it locally).

Useful if you're impatient and want to watch a movie immediately and download it at the same time without using extra bandwidth.

This is an extension of snipertyler's idea.

Note: This command uses an encrypted connection, unlike the original.

curl -s http://en.m.wikipedia.org/wiki/List_of_Internet_top-level_domains | sed -n '/<tr valign="top">/{s/<[^>]*>//g;p}'
2012-12-24 23:54:05
User: flatcap
Functions: sed
1

Quietly get a webpage from wikipedia: curl -s

By default, don't output anything: sed -n

Search for interesting lines: /<tr valign="top">/

With the matching lines: {}

Search and replace any html tags: s/<[^>]*>//g

Finally print the result: p

for i in /var/spool/cron/*; do echo ${i##*/}; sed 's/^/\t/' $i; echo; done
cal 04 2012 | awk '{ $7 && X=$7 } END { print X }'
2012-05-06 23:43:21
User: flatcap
Functions: awk cal
2

If your locale has Monday as the first day of the week, like mine in the UK, change the two $7 into $6

find . ! -regex '.*/\(unit-test\|android\)/.*' \( -name '*.c' -o -name '*.cpp' \)
2012-04-26 17:53:45
User: flatcap
Functions: find
Tags: find
0

Find files matching multiple descriptions (*.c and *.cpp) excluding multiple directories (unit-test and android).

find . -type f -print0 | xargs -0 du -h | sort -hr | head -20
2012-03-30 10:21:12
User: flatcap
Functions: du find head sort xargs
7

Search for files and list the 20 largest.

find . -type f

gives us a list of file, recursively, starting from here (.)

-print0 | xargs -0 du -h

separate the names of files with NULL characters, so we're not confused by spaces

then xargs run the du command to find their size (in human-readable form -- 64M not 64123456)

| sort -hr

use sort to arrange the list in size order. sort -h knows that 1M is bigger than 9K

| head -20

finally only select the top twenty out of the list

for w in $(tr 'A-Z ,."()?!;:' 'a-z\n' < sample.txt); do echo ${#w} $w; done | sort -u | sort -n
2012-03-15 14:14:11
User: flatcap
Functions: echo sort tr
Tags: bash sort tr
0

Take a file and ,."()?!;: give a list of all the words in order of increasing length.

First of all use tr to map all alphabetic characters to lower case and also strip out any puntuation.

A-Z become a-z

,."()?!;: all become \n (newline)

I've ignored - (hyphen) and ' (apostrophe) because they occur in words.

Next use bash to print the length ${#w} and the word

Finally sort the list numerically (sort -n) and remove any duplicates (sort -u).

Note: sort -nu performs strangely on this list. It outputs one word per length.

for i in *; do sed -i "s/^/$i: /" $i; done
mysql -u root -p -BNe "select host,count(host) from processlist group by host;" information_schema
2011-12-22 10:35:23
User: flatcap
Tags: mysql
6

Count the number of active connections to a MySQL database.

The MySQL command "show processlist" gives a list of all the active clients.

However, by using the processlist table, in the information_schema database, we can sort and count the results within MySQL.

printf "$PWD/%s\n" *
2011-12-16 13:43:01
User: flatcap
Functions: printf
Tags: printf
3

List the full path of some files.

You can add ".*" on the end if you want to display hidden files.

sed -n '3,6p' /path/to/file
2011-12-14 15:09:38
User: flatcap
Functions: sed
Tags: sed
6

Print all lines between two line numbers

This command uses sed(1) to print all lines between two known line numbers in a file. Useful for seeing output in a log file, where the line numbers are known. The above command will print all lines between, and including, lines 3 and 6.

UUID="63b726a0-4c59-45e4-af65-bced5d268456"; echo ${UUID//-/}
2011-11-22 22:49:30
User: flatcap
Functions: echo
0

Remove the dashes from a UUID using bash search and replace.

whichpkg() { rpm -qf "$@"; }
2011-07-05 23:39:32
User: flatcap
Functions: rpm
Tags: rpm
0

Find the package a file belongs to on an rpm-based distro.

summpkg() { rpm -qfi "$@"; }
2011-07-05 23:39:24
User: flatcap
Functions: rpm
Tags: rpm
0

Find information about the rpm package which owns a certain file.

find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01"
2011-06-26 09:52:26
User: flatcap
Functions: find
Tags: find dates
30

Find files in a specific date range - in this case, the first half of last year.

-newermt = modification time of the file is more recent than this date

GNU find allows any date specfication that GNU date would accept, e.g.

find . -type f -newermt "3 years ago" ! -newermt "2 years ago"

or

find . -type f -newermt "last monday"
cd() { if [ -n "$1" ]; then [ -f "$1" ] && set -- "${1%/*}"; else [ -n "$CDDIR" ] && set -- "$CDDIR"; fi; command cd "$@"; }
2011-06-24 08:48:13
User: flatcap
Functions: cd command set
Tags: cd test set
0

Move efficiently between directories.

.

This command adds a couple of extra features to cd, without affecting normal use.

CDPATH use is also unaffected. It introduces and environment variable CDDIR which is used as an alternate home directory.

.

Note: I don't want to alter $HOME because then all my dot files will move.

.

Examples:

.

cd dir

Change directory to "dir" (using CDPATH if necessary)

.

cd dir/file.txt

Change directory to "dir" (containing folder of "file.txt")

This allows you to cut'n'paste, or use

.

CDDIR is unset

cd

Change directory to $HOME

.

CDDIR=/home/flatcap/work

cd

Change directory to /home/flatcap/work

.

For convenience, put the command, and the following, in your .bashrc or .bash_profile

export CDDIR="/home/flatcap/work"

alias cdd="CDDIR=$(pwd)"