0

I want to calculate disk space via du, but I need sudo to access some dirs:

$perl -ane '$i+=($F[0] =~ s/^(\d+).*/$1/r); print "$i\n"' <(sudo du /home)

which gives:

[sudo] password for user1: 
sudo: unable to read user1: Input/output error

How to enable sudo in process substitution?

0

Run it first with an empty command, or with sudo -v to set up the authentication token, then a following run should work without asking for password. Depends on how sudo is set up on the system though, but the default is that the authentication is valid for 15 minutes.

$ sudo -v
[sudo] password for iv: 
$ cat <(sudo du -d 1)
28636  ./foo
...

Or use a pipe instead of the process substitution:

$ sudo du -d 1 | cat
[sudo] password for iv: 
28636  ./foo
...

Note that your Perl script doesn't seem to make much sense. du lists all directories, including the ones with subdirectories, and counts the sizes of all subtrees itself. So if you just add the numbers it shows, you get all directories counted once for each level of their depth. E.g. here, there's just one 100 kB file in a/b/c:

$ du a
104     a/b/c
108     a/b
112     a

Also, there's du -s to only show the total size of the given directories, and at least GNU du has -d or --max-depth=N to limit how many levels of subdirectories it shows, etc.

| improve this answer | |
  • So file c is 4bytes long, directory b is 8bytes long and directory a is 100 bytes long and together 112? Or I do no understand, why those number are – milanHrabos Jul 8 at 22:15
  • @milanHrabos, that's GNU du, so the numbers are kilobytes. It doesn't show individual files, just directories. a/b/c contains a file of 100 kB, that plus c itself is 104 kB, plus b is 108 kB, plus a is 112 kB, which is the total of the subtree. But if you add it all up, you get 324, an obviously wrong number. – ilkkachu Jul 8 at 23:19
  • you firstly say it does no show individual files and then a/b/c contains a file... this contradict. Still do not understand the numbers. a/b/c is file, it is 100kB. a/b/ is directory, it is 4kB, a is directory, it is 8kB. Is that correct? – milanHrabos Jul 9 at 0:03
  • @milanHrabos, no, a/b/c here is not a file, it's a directory that contains the file. du doesn't list individual files in the output, but of course it counts their sizes in the sums. – ilkkachu Jul 9 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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