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 using df from sorted by
Terminal - Commands using df - 36 results
(mountpoint -q "/media/mpdr1" && df /media/mpdr1/* > /dev/null 2>&1) || ((sudo umount "/media/mpdr1" > /dev/null 2>&1 || true) && (sudo mkdir "/media/mpdr1" > /dev/null 2>&1 || true) && sudo mount "/dev/sdd1" "/media/mpdr1")
2014-04-12 11:23:21
User: tweet78
Functions: df mkdir mount sudo umount
9

In my example, the mount point is /media/mpdr1 and the FS is /dev/sdd1

/mountpoint-path = /media/mpdr1

filesystem=/dev/sdd1

Why this command ?

Well, in fact, with some external devices I used to face some issues : during data transfer from the device to the internal drive, some errors occurred and the device was unmounted and remounted again in a different folder.

In such situations, the command mountpoint gave a positive result even if the FS wasn't properly mounted, that's why I added the df part.

And if the device is not properly mounted, the command tries to unmount, to create the folder (if it exists already it will also work) and finally mount the FS on the given mount point.

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-") | sudo tee /sys/bus/usb/drivers/usb/unbind
2014-04-06 12:06:29
User: tweet78
Functions: awk cut df echo grep head sudo tail tee tr
18

You have an external USB drive or key.

Apply this command (using the file path of anything on your device) and it will simulate the unplug of this device.

If you just want the port, just type :

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-")

df -h --total | awk 'NR==1; END{print}'
diff <(cat /etc/fstab | grep vol | grep -v "^#" | awk '{print $1}') <(df -h | grep vol)
2014-01-23 15:18:08
User: Koobiac
Functions: awk cat df diff grep
Tags: diff fstab df
0

With this command, you can check the difference between the volumes mounted and the volume in /etc/fstab.

sudo tune2fs -l $(df -h / |(read; awk '{print $1; exit}')) | grep -i created
2013-08-08 15:18:09
User: thechile
Functions: awk df grep sudo tune2fs
12

..not guaranteed to always be accurate but fun to see how old you Linux installation is based on the root partitions file system creation date.

dd if=/dev/zero of=/fs/to/fill/dummy00 bs=8192 count=$(df --block-size=8192 / | awk 'NR!=1 {print $4-100}')
df | awk '{if ($2!=dspace) print "different"; dspace=$2;}'
df -H | grep -vE '^Filesystem|tmpfs|cdrom|none' | awk '{ print $5 " " $1 }'
tune2fs -l $(df -P / | awk 'NR==2 {print $1}') | sed -n 's/^.*created: *//p'
2012-05-31 12:12:35
User: forcefsck
Functions: awk df sed tune2fs
0

Find out the earliest installation time of a linux system by getting the / filesystem creation time. This example is only valid the os is installed on an ext2/3/4 filesystem.

# umount /media/filesystem; e2fsck -f /dev/device ; resize2fs -p /dev/device 200G #actual newsize#;lvreduce --size 200G /dev/device; mount /media/filesystem; df -h /media/filesystem
2011-09-14 08:52:02
User: bbelt16ag
1

Just the commands for the lvreduce I keep forgetting.

df -PH|column -t
df -P | column -t
2011-04-09 13:12:46
User: fossilet
Functions: column df
17

-P uses the POSIX output format, which makes information on each file system always printed on exactly one line. "column -t" makes a table from the input.

nohup df -k | sort -rn 12
df -h | grep -v ^none | ( read header ; echo "$header" ; sort -rn -k 5)
2011-03-16 14:25:45
User: purpleturtle
Functions: df echo grep read sort
Tags: sort headers df
0

Show disk space info, grepping out the uninteresting ones beginning with ^none while we're at it.

The main point of this submission is the way it maintains the header row with the command grouping, by removing it from the pipeline before it gets fed into the sort command. (I'm surprised sort doesn't have an option to skip a header row, actually..)

It took me a while to work out how to do this, I thought of it as I was drifting off to sleep last night!

df /media/mountpoint |egrep -o '^[/a-z0-9]*'
2011-01-24 21:14:55
User: DaveQB
Functions: df egrep
Tags: grep,df
-1

Shorter way to find the device for a given mountpoint

df | grep -w '/media/armadillo' | cut -d " " -f 1
df | grep -w '/media/mountpoint' | cut -d " " -f 1
2011-01-21 05:38:05
User: ntropia
Functions: cut df grep
-2

Identical output but a different way without having to shoot with the Awk cannon :)

df -P | awk '$6=="/media/KINGSTON" {print $1}'
2011-01-21 00:32:00
User: linuts
Functions: awk df
0

No need for grep | awk. -P on df will force the mount point to be on the same line as the device

df | grep -w /media/KINGSTON | awk {'print $1'}
2011-01-20 09:07:19
Functions: awk df grep
-3

most usefull when creating batch scripts using several usb drives and some commands like mkntfs needs a device name

the -w option for grep is here to filter lines when you have multiple drives with the same volume label. Without this option, the grep command will return

/media/KINGSTON

/media/KINGSTON_

/media/KINGSTON__

df -i <partition>
2010-12-12 03:34:57
User: bzaman
Functions: df
1

tune2fs also provides the same information . But the information does not give the current usage , it gives the information when the file system was last mounted.

http://www.zaman4linux.in/2010/10/using-up-all-the-free-inodes.html

while (true); do clear; uname -n; echo ""; df -h /; echo ""; tail -5 /var/log/auth.log; echo ""; vmstat 1 5; sleep 15; done
2010-08-23 04:37:58
User: roknir
Functions: df echo sleep tail uname vmstat
1

You can use this one-liner for a quick and dirty (more customizable) alternative to the watch command. The keys to making this work: everything exists in an infinite loop; the loop starts with a clear; the loop ends with a sleep. Enter whatever you'd like to keep an eye on in the middle.

tune2fs -l $(df -P / | tail -n1 | cut -d' ' -f1 ) | grep 'Filesystem created:'
df -l | grep -e "9.%" -e "100%"
2010-04-26 17:57:54
User: dooblem
Functions: df grep
2

Reports all local partitions having more than 90% usage.

Just add it in a crontab and you'll get a mail when a disk is full.

(sending mail to the root user must work for that)

df -kh /dev/vg0*/lv*
DIR=. ; FSTYPE=$(df -TP ${DIR} | grep -v Type | awk '{ print $2 }') ; echo "${FSTYPE}"
2009-12-22 14:01:50
User: unixhome
Functions: awk df echo grep
1

Exclude 400 client hosts with NFS auto-mounted home directories.

Easily modified for inclusion in your scripts.