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 dd from sorted by
Terminal - Commands using dd - 138 results
SIZE=1; dd if=/dev/zero bs=1M count=$((SIZE*1024)) | pv -pters $((SIZE*1024*1024*1024)) | openssl enc -aes-256-ctr -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt > randomfile
2014-08-30 01:46:51
User: kennethjor
Functions: dd
0

SIZE is the number of gigabytes and the file name is at the end. Random data is generated by encrypting /dev/zero, similar to other techniques posted on here.

set +e +u; dd if=/dev/urandom of="/media/usb1/$$";sync;sync
2013-12-22 14:35:53
User: warkruid
Functions: dd set
0

I sometimes have use an usb stick to distribute files to several standalone "internet" pc's. I don't trust these machines period. The sticks I have do not have a write protection. So as a added security measure I fill the unused space on the (small) usb stick with a file with randomly generated bits. Any malware that tries to write to this stick will find no space on it.

Tested on slackware 14

Note: you may need root access to write to the device. This depends on your mount options.

dd if=file | tee >(sha1sum) >(md5sum) >(sha256sum) >/dev/null
2013-11-07 17:43:54
User: dubbaluga
Functions: dd tee
Tags: tee parallel I/O
0

This is to overcome the issue of slow I/O by reading once and forwarding the output to several processes (e. g. 3 in the given command). One could also invoke grep or other programs to work on read data.

dd bs=1M if=/dev/scd0 of=./filename.iso OR readom -v dev='D:' f='./filename.iso' speed=2 retries=8
2013-10-23 15:53:27
User: scotharkins
Functions: dd
-1

This example is taken from Cygwin running on Win7Ent-64. Device names will vary by platform.

Both commands resulted in identical files per the output of md5sum, and ran in the same time down to the second (2m45s), less than 100ms apart. I timed the commands with 'time', which added before 'dd' or 'readom' gives execution times after the command completes. See 'man time' for more info...it can be found on any Unix or Linux newer than 1973. Yeah, that means everywhere.

readom is supposed to guarantee good reads, and does support flags for bypassing bad blocks where dd will either fail or hang.

readom's verbosity gave more interesting output than dd.

On Cygwin, my attempt with 'readom' from the first answer actually ended up reading my hard drive. Both attempts got to 5GB before I killed them, seeing as that is past any CD or standard DVD.

dd:

'bs=1M' says "read 1MB into RAM from source, then write that 1MB to output. I also tested 10MB, which shaved the time down to 2m42s.

'if=/dev/scd0' selects Cygwin's representation of the first CD-ROM drive.

'of=./filename.iso' simply means "create filename.iso in the current directory."

readom:

'-v' says "be a little noisy (verbose)." The man page implies more verbosity with more 'v's, e.g. -vvv.

dev='D:' in Cygwin explicitly specifies the D-drive. I tried other entries, like '/dev/scd0' and '2,0', but both read from my hard drive instead of the CD-ROM. I imagine my LUN-foo (2,0) was off for my system, but on Cygwin 'D:' sort of "cut to the chase" and did the job.

f='./filename.iso' specifies the output file.

speed=2 simply sets the speed at which the CD is read. I also tried 4, which ran the exact same 2m45s.

retries=8 simply means try reading a block up to 8 times before giving up. This is useful for damaged media (scratches, glue lines, etc.), allowing you to automatically "get everything that can be copied" so you at least have most of the data.

dd if=/dev/null of=/dev/sda
2013-08-30 21:06:36
User: metaverse
Functions: dd
1

removes all files/filesystems of a harddisk. It removes EVERYTHING of your hard disk. Be carefull when to select a device. It does not prompt for and second check.

pv -tpreb /dev/sda | dd of=/dev/sdb bs=1M
2013-08-19 23:04:15
User: sc0ttyd
Functions: dd
Tags: dd progress pv,
-1

Your platform may not have pv by default. If you are using Homebew on OSX, simply 'brew install pv'.

dd if=/dev/cdrom of=cd.iso
2013-06-12 14:13:41
User: neilmccauley
Functions: dd
Tags: dvd cdrom
0

For DVD: dd if=/dev/cdrom of=cd.iso

dd if=/dev/sda of=mbr.bk bs=512 count=1
2013-05-17 02:59:33
User: lili
Functions: dd
0

MBR is first 512B in partions.

dd if=/dev/zero of=/fs/to/fill/dummy00 bs=8192 count=$(df --block-size=8192 / | awk 'NR!=1 {print $4-100}')
dd if=/dev/cdrom bs=1 skip=32808 count=32 conv=unblock cbs=32 2>/dev/null
2013-04-14 20:50:58
User: mlk
Functions: dd
Tags: dd cd iso9660
0

The comp.unix.shell posting by St?phane Chazelas also lists the following offsets:

type 32768 (1 byte)

id 32769 (5 bytes)

version 32774 (1 byte)

system_id 32776 (32 bytes)

volume_id 32808 (32 bytes)

volume_space_size 32848 (8 bytes)

escape_sequences 32856 (32 bytes)

volume_set_size 32888 (4 bytes)

volume_sequence_number 32892 (4 bytes)

logical_block_size 32896 (4 bytes)

path_table_size 32900 (8 bytes)

type_l_path_table 32908 (4 bytes)

opt_type_l_path_table 32912 (4 bytes)

type_m_path_table 32916 (4 bytes)

opt_type_m_path_table 32920 (4 bytes)

root_directory_record 32924 (34 bytes)

volume_set_id 32958 (128 bytes)

publisher_id 33086 (128 bytes)

preparer_id 33214 (128 bytes)

application_id 33342 (128 bytes)

copyright_file_id 33470 (37 bytes)

abstract_file_id 33507 (37 bytes)

bibliographic_file_id 33544 (37 bytes)

creation_date 33581 (17 bytes)

modification_date 33598 (17 bytes)

expiration_date 33615 (17 bytes)

effective_date 33632 (17 bytes)

file_structure_version 33649 (1 byte)

application_data 33651 (512 bytes)

dd if=/dev/urandom | tr -d -c [:print:] | tr -d " " | dd count=1 bs=20 2> /dev/null; echo
2013-03-01 22:42:29
User: Progent
Functions: dd tr
-1

It will produce passwords with length of 20 printable characters within a reasonable time.

For shorter or longer passwords just change the 20 in bs=20 to something more convenient.

To create only alpha numeric passwords change [:print:] to [:alnum:]

sudo dd if=/dev/sdc bs=4096 | pv -s `sudo mount /dev/sdc /media/sdc && du -sb /media/sdc/ |awk '{print $1}' && sudo umount /media/sdc`| sudo dd bs=4096 of=~/USB_BLACK_BACKUP.IMG
export BLOCKSIZE='sudo blockdev --getsize64 /dev/sdc' && sudo dd if=/dev/sdc bs=1MB | pv -s $BLOCKSIZE | gzip -9 > USB_SD_BACKUP.img.gz
2013-02-05 18:10:25
User: hur1can3
Functions: dd export gzip sudo
2

This is a useful command to backup an sd card with relative total size for piping to pv with a progressbar

dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm T)>>F
2013-01-31 01:38:30
User: rodolfoap
5

This is just a proof of concept: A FILE WHICH CAN AUTOMOUNT ITSELF through a SIMPLY ENCODED script. It takes advantage of the OFFSET option of mount, and uses it as a password (see that 9191? just change it to something similar, around 9k). It works fine, mounts, gets modified, updated, and can be moved by just copying it.

USAGE: SEE SAMPLE OUTPUT

The file is composed of three parts:

a) The legible script (about 242 bytes)

b) A random text fill to reach the OFFSET size (equals PASSWORD minus 242)

c) The actual filesystem

Logically, (a)+(b) = PASSWORD, that means OFFSET, and mount uses that option.

PLEASE NOTE: THIS IS NOT AN ENCRYPTED FILESYSTEM. To improve it, it can be mounted with a better encryption script and used with encfs or cryptfs. The idea was just to test the concept... with one line :)

It applies the original idea of http://www.commandlinefu.com/commands/view/7382/command-for-john-cons for encrypting the file.

The embedded bash script can be grown, of course, and the offset recalculation goes fine. I have my own version with bash --init-file to startup a bashrc with a well-defined environment, aliases, variables.

sudo dd if=/your.img of=/dev/rdiskx bs=1m
dd if=mybackup.ab bs=24 skip=1 | openssl zlib -d > mybackup.tar
dd.exe --progress if=\\.\Volume{0b1a0cbe-11da-11c0-ab53-003045c00008} of=pendrive.img
2012-10-13 08:25:48
User: bugmenot
Functions: dd
0

Blocksize (bs) is not mandatory. It's only needed when the count option is specified.

perl -e '$s="$s\xFF" while length($s)<512; print $s while 1' | dd of=/dev/sdX
cat /dev/urandom | pv -L 3m | dd bs=1M count=100 iflag=fullblock > /dev/null
2012-07-29 00:42:16
User: bugmenot
Functions: cat dd
6

This example will close the pipe after transferring 100MB at a speed of 3MB per second.

dd if=/dev/cdrom of=~/cdrom_image.iso
2012-07-10 06:03:25
User: o0110o
Functions: dd
Tags: dd cd iso dvd convert
5

An easy method to generate ISOs from CD/DVD media.

time dd if=/dev/zero of=dummy_file bs=512k count=200
2012-04-20 13:47:27
User: rdc
Functions: dd time
1

Write 200 blocks of 512k to a dummy file with dd, timing the result. The is useful as a quick test to compare the performance of different file systems.

dd if=/dev/zero bs=64K count=1 | tr "\0" "\377" > all_ones
2012-04-17 18:01:59
User: anduril462
Functions: dd tr
1

dd can be used with /dev/zero to easily create a file of all zero-bytes. Pipe that through tr and use octal conversions to change the byte values from zero to 0xff (octal 0377). You can replace 0377 with the byte of your choice. You can also use \\0 and \\377 instead of the quoted version.

pv -tpreb /dev/urandom | dd of=file.img
2012-04-11 22:32:52
User: marrowsuck
Functions: dd
Tags: dd pv
8

This version was mentioned in the comments. Credits go to flatcap.

dd if=/dev/cdrom of=~/cdimage.iso
x=1024; y=32768; cat <(echo -e "P5\n$x $y\n255\n") <(dd if=/dev/sda1 bs=$x count=$y) > sda1.pgm
2012-03-06 03:09:16
Functions: cat dd echo
Tags: dd images pnm pgm
1

Keep width to a power of 2 to see patterns emerge. 512 is good. So is 4096 for huge maps.

PNM headers are super basic.

http://netpbm.sourceforge.net/doc/pbm.html