Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using following command to list out 20 large files in a directory. But along with the file listing, it also shows error messages, such as permission denied. I tried adding 2> /dev/null but it does not seem to like it.

This is the command I'm using

find . -type f -printf "%s %p\n" | sort -nr | head -n 20

May I know how to avoid all the error message and only display the list of files?

share|improve this question
add comment

1 Answer

In bash, 2> /dev/null would actually work, as long as you add it as part of the find command itself, i.e., before the pipe.

For example, this will work:

find -type f -printf "%s %p\n" 2> /dev/null | sort -nr | head -n 20

With tcsh, there's no straightforward way to do the same, but man tcsh mentions a workaround:

The shell cannot presently redirect diagnostic output without also redirecting standard output, but '(command > output-file) >& error-file' is often an acceptable workaround. Either output-file or error-file may be '/dev/tty' to send output to the terminal.

For a plain find command, this gives:

(find > /dev/tty) >& /dev/null

When using pipes, in contrast to bash, you have to place > /dev/tty after the last pipe to avoid the Ambiguous output redirect. error message.

This will work in tcsh:

(find -type f -printf "%s %p\n" | sort -nr | head -n 20 > /dev/tty) >& /dev/null
share|improve this answer
    
With your suggestion, I am getting following error - Ambiguous output redirect. –  user196833 Feb 9 '13 at 0:48
    
I tried this find . -type f -printf "%s %p\n" 2> /dev/null | sort -nr | head -n 20 but it is giving out error - Ambiguous output redirect –  user196833 Feb 9 '13 at 0:57
1  
I think this is because I am using tcsh and not bash. –  user196833 Feb 9 '13 at 1:02
    
I've installed tcsh and I found a way. –  Dennis Feb 9 '13 at 11:52
    
So to output to a file: (find -type f -printf "%s %p\n" | sort -nr | head -n 20 > file.txt) >& /dev/null –  Umber Ferrule Feb 4 at 16:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.