user7988893
user7988893

Reputation:

Why can't redirect tee command's stdout into /dev/null

I make fingerprint for all files in /var/www/html this way.

find /var/www/html  -type  f |xargs md5sum | tee /opt/figerprint.db.ori  

It works fine.

Now i want to redirect all stdout of tee command into /dev/null.

find /var/www/html  -type  f |xargs md5sum | tee /opt/figerprint.db.ori  1>/dev/null

md5sum: /var/www/html/news/uploads/Red: No such file or directory
md5sum: Hat: No such file or directory
md5sum: Enterprise: No such file or directory
md5sum: Linux: No such file or directory
md5sum: 6.txt: No such file or directory

I found thant a file name can't be parsed properly.

ls /var/www/html/news/uploads/Red*
/var/www/html/news/uploads/Red Hat Enterprise Linux 6.txt

How to fix my command to properly redirect stdout into /dev/null?

Upvotes: 0

Views: 3646

Answers (3)

user7988893
user7988893

Reputation:

Why find /var/www/html -type f |xargs md5sum | tee /opt/figerprint.db.ori can't show error info?

find /var/www/html  -type  f |xargs md5sum
md5sum: /var/www/html/news/uploads/Red: No such file or directory
md5sum: Hat: No such file or directory
md5sum: Enterprise: No such file or directory
md5sum: Linux: No such file or directory
md5sum: 6.txt: No such file or directory

tee will show both stdout and stderr on screen but only write stdout info by default.

find /var/www/html  -type  f |xargs md5sum | tee /opt/figerprint.db.ori

I go through the output of the above command in the screen ,

md5sum: /var/www/html/news/uploads/Red: No such file or directory
md5sum: Hat: No such file or directory
md5sum: Enterprise: No such file or directory
md5sum: Linux: No such file or directory
md5sum: 6.txt: No such file or directory

The above lines(call it related error info) were emcircled by many other lines such as

b61b25303be0f573a6b9446d5cbe3a5b  /var/www/html/index.php

The related error info can't be written into file by default (tee's character).

find /var/www/html -type f |xargs md5sum | tee /opt/figerprint.db.ori never works fine.

Upvotes: 0

tripleee
tripleee

Reputation: 189749

Your stdout redirection is working fine, and is unrelated to the error message you are getting (which is on standard error, not standard output, anyway. If you want to discard stderr that's 2>/dev/null).

The reason for the error is that you are passing unquoted file names to md5sum. A common workaround with GNU find is to use zero-byte terminators instead of newlines:

find /var/www/html -type f -print0 | 
xargs -0 md5sum |
tee /opt/figerprint.db.ori >/dev/null

Of course, there is no need to tee anything if you discard the standard output.

find /var/www/html -type  f -print0 | 
xargs -0 md5sum >/opt/figerprint.db.ori

A common addition is xargs -r to prevent md5sum from running at all if find doesn't find any files.

The error message suggests that you probably don't have GNU find. Another way to accomplish the same thing is

find /var/www/html -type f -exec md5sum {} + >/opt/figerprint.db.ori

If your find is really ancient and doesn't support -exec ... + you will need to switch to

find /var/www/html -type f -exec md5sum {} \; >/opt/figerprint.db.ori

which is however going to be rather inefficient, because it creates one process per found file.

Upvotes: 2

ssemilla
ssemilla

Reputation: 3970

Try doing this instead:

find /var/www/html -type  f | xargs -i md5sum | tee /opt/figerprint.db.ori  1>/dev/null

From xargs's manual:

-I replace-str

Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.

Upvotes: 0

Related Questions