52

I have a bash script that creates some file using dd. The problem is dd throws a great amount of output that is going to mess with the output of my script. Searching around I've found a solution:

dd if=boot1h of="/dev/r$temp1" >& /dev/null

Is there an alternative, or is redirecting to /dev/null the only way?

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
dierre
  • 645
  • 3
    I'm lost on your last sentence, "the script should not request for privileges". That doesn't seem to have anything to do with redirecting to /dev/null -- you're sudoing because dd needs write access to /dev/r$temp1 (I assume). You're going to need to do that no matter how you suppress dd's output; redirecting output to /dev/null doesn't require root – Michael Mrozek Jan 31 '11 at 17:40
  • I'm an idiot. It was giving operation not permitted because of the use of /dev/r$temp1/. I'm really sorry. I'm editing a script that's not mine and didn't noticed it. Really sorry. – dierre Jan 31 '11 at 17:54
  • so the problem is regular user cannot write to /dev/r$temp1 or to dev/null – jet Jan 31 '11 at 18:05
  • Don't worry about it; I edited it a bit to remove the confusing part – Michael Mrozek Jan 31 '11 at 18:59
  • 2
    If you're not using any of dd's advanced features, use cat, head or tail instead. – Gilles 'SO- stop being evil' Jan 31 '11 at 19:46
  • 1
    Please consider marking the post by @orgoj as the answer instead, since it has been a decent few years and dd 8.21 is likely on almost everyones machines at this point. – hak8or Oct 30 '20 at 18:03

8 Answers8

89

Add status=none:

dd if=boot1h of="/dev/r$temp1" status=none

From the dd (coreutils) 8.21 docs:

'status=LEVEL'
     Transfer information is normally output to stderr upon receipt of
     the 'INFO' signal or when 'dd' exits.  Specifying LEVEL will adjust
     the amount of information printed, with the last LEVEL specified
     taking precedence.
 'none'
      Do not print any informational or warning messages to stderr.
      Error messages are output as normal.

 'noxfer'
      Do not print the final transfer rate and volume statistics
      that normally make up the last status line.

 'progress'
      Print the transfer rate and volume statistics on stderr, when
      processing each input block.  Statistics are output on a
      single line at most once every second, but updates can be
      delayed when waiting on I/O.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
orgoj
  • 991
  • @roaima - just curious, why editing a quote from the man page ? – don_crissti Apr 23 '15 at 11:50
  • @don_crissti I felt the (lack of) context in extracting the quote from the man page mean that it required a slightly different form of words. As it stands in the man page it's fine; here it looked strange. – Chris Davies Apr 23 '15 at 12:02
  • @roaima - all right - curiosity satisfied :) - but note that you link to the info page; the initial answer here had the exact quote from the man page. I would have copy/pasted directly from the info page if the man page is ambiguous but really, up to you... Oh, btw, thanks much for your other comment on my answer related to sorting by week day. – don_crissti Apr 23 '15 at 12:46
  • @don_crissti I couldn't find a man page online that had the quote; thank you for that one. (die.net has the older dd without status=) – Chris Davies Apr 23 '15 at 12:50
  • 3
    Doesn't seem to be available in dd (coreutils) 8.13:

    dd: invalid status flag: `none' Try `dd --help' for more information.

    – Per Lundberg May 23 '15 at 18:44
  • Neither the status=none option, nor redirecting stderr to /dev/null, nor redirecting stderr to stdout with 2>&1 and then redirecting stdout to /dev/null, makes a difference for the dd output on my machine (Debian Stretch). – mcandre Nov 07 '17 at 04:50
  • 3
    is status=none supported by busybox? how about toybox? how about OpenBSD/FreeBSD? how about Solaris? i know that support for status=progress varies wildly – hanshenrik Nov 24 '20 at 19:10
18

From the dd(1) man page:

   status=noxfer
          suppress transfer statistics

thus:

dd if=boot1h of="/dev/r$temp1" status=noxfer

This still outputs the

0+1 records in
0+1 records out

garbage when dd exits, so redirecting to a data sink really is your only option.

amphetamachine
  • 5,517
  • 2
  • 35
  • 43
  • that was I'm afraid of. – dierre Jan 31 '11 at 16:59
  • I believe status=noxfer, could be related to SIGUSR1 signal, that normally show the transfer statistic. Hoewever, I am not willing to test if it's true what I am saying. – maxadamo May 30 '16 at 18:42
6

For future reference:

To suppress dd output completely redirect stderr to /dev/null like so:

dd if=/dev/urandom of=sample.txt bs=5MB count=1 2> /dev/null

This works nicely if you want to, for example, time the process using the time command in bash and assign the result to a variable, without getting any of the output that dd produces.

reference: http://www.unix.com/shell-programming-and-scripting/131624-how-suppress-dd-output.html

CSCH
  • 169
3

With any Unix application or command, you can suppress all output with

cmd >/dev/null 2>&1

The first bit redirects the standard output (unit number 1) to /dev/null. But you need the second part to ALSO redirect the error output (unit number 2) to the same place as number 1.

In UNIX, STDIN=0, STDOUT=1 and STDERR=2


2

Something like this should also work for you with recent versions of BASH and ZSH:

dd if=/path/to/file of=/path/to/another_file bs=1M count=1 &> /dev/null

P.S. This is just an example I ran...

wag
  • 35,944
  • 12
  • 67
  • 51
slashdot
  • 666
0

If I understand correctly what you are trying to do, are you putting that sudo command into the script and expecting the script to prompt for your password when it runs there? In that case you are just doing things the complicated way.

A cleaner solution is to write the script in the usual way (i.e without sudo) and run it as the superuser. The reason behind this is, if the script needs superuser access, then just give it the access (why wait until a certain command?). In the script, to check if it is being run as root do something like this:

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi
phunehehe
  • 20,240
  • nope. That's exactly what I don't want to do. I don't want the script to be run as root only because I don't want dd to display the output. Your check would correct if I want the script to be run as superuser. – dierre Jan 31 '11 at 16:59
  • @dierre So why did you need sudo in the first place? Normal users should be able to redirect things to /dev/null just fine. – phunehehe Jan 31 '11 at 17:50
  • I'm using Ubuntu and it's giving me operation not permitted...uhm... – dierre Jan 31 '11 at 17:52
  • I'm sorry. I didn't notice I was creating something under /dev/. The script is not mine, I was editing it. I'm really sorry. – dierre Jan 31 '11 at 17:55
0

you can redirect the output to an regular file i.e.:

 dd if=boot1h of="/dev/r$temp1" >& /tmp/dd.log
jet
  • 894
0

Per the man page, using dd status=none won't get rid of error messages.

If there is a single error message that is expected/desirable, you can use grep to both check that execution was as expected and eat the output. In this example, I obliterate a partition by overwriting with zeros so that mke2fs won't question the need to reformat it.

Running dd without a count specifier will always result in a "No space left on device" error message and nonzero exit value. By searching for the expected error message with grep, the expected behavior returns no error while any unexpected behavior returns an error.

# desired behavior: erase to the end of partition and return zero as exit code.
$ dd if=/dev/zero of=/dev/mmcblk2p7 bs=1M 2>&1 | grep -q 'No space left on device'
$ echo $?
0

Nonzero exit code is returned if our expected error message doesn't appear.

$ dd if=/dev/zero of=/dev/mmcblk2p7 bs=1M 2>&1 | grep -q 'Frobulators are not block-aligned' $ echo $? 1

The redirect of stderr to stdout is necessary because most dd output goes to stderr while grep operates on stdout. Use grep's -q flag to control visibility of the output.