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?

6
  • 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 Jan 31, 2011 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, 2011 at 17:54
  • so the problem is regular user cannot write to /dev/r$temp1 or to dev/null
    – jet
    Jan 31, 2011 at 18:05
  • 2
    If you're not using any of dd's advanced features, use cat, head or tail instead. Jan 31, 2011 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, 2020 at 18:03

8 Answers 8

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.
7
  • @roaima - just curious, why editing a quote from the man page ? Apr 23, 2015 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. Apr 23, 2015 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. Apr 23, 2015 at 12:46
  • 3
    Doesn't seem to be available in dd (coreutils) 8.13: dd: invalid status flag: `none' Try `dd --help' for more information. May 23, 2015 at 18:44
  • 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, 2020 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.

2
  • that was I'm afraid of.
    – dierre
    Jan 31, 2011 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, 2016 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

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


3
  • 1
    Actually, stderr has the file descriptor 2. (I assume "STDERR=3" is just a typo.)
    – n.st
    Jun 21, 2014 at 13:29
  • 1
    It is undesirable to suppress all output. If an error occurs, we would like to see the error message. Sep 17, 2016 at 14:56
  • cmd 2>logfile.txt seems more adapted
    – TheSola10
    Apr 21, 2017 at 11:02
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...

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
4
  • 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, 2011 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, 2011 at 17:50
  • I'm using Ubuntu and it's giving me operation not permitted...uhm...
    – dierre
    Jan 31, 2011 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, 2011 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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .