10

I've searched my best through Google, but for the life of me I can't figure out what to use instead of * (asterisk) after a recent update (even Wikipedia seems to think

du -sh * and du -sh * should work)

I've used

du -sh * | sort -h

ever since just before sort got the -h option (on Fedora I think, took a while before I could use the sort -h on CentOS), but suddenly * seems to output a long list of

du: invalid option -- ' '

where the ' ' goes through all the invalid options not mentioned in the man page.

I would be very thankful if someone could tell me what would be the equivalent of

du -sh * | sort -h

on the updated versions.

jmkane
  • 111

2 Answers2

17

You have a file with a funny name, probably starting with a -. Remember that globs (like *) are expanded by your shell, not the command being run. As an example, say you have:

$ ls -1
foo
-q

Simple enough directory, with two files in it. (The -1 option to coreutils ls makes its output single-column.)

When you run du -sh *, the shell notices that the second argument contains a special character (*) and isn't quoted, so it does glob expansion. It expands it to everything that matches, in this case foo and -q. The effect is exactly as if you'd run:

$ du -sh foo -q
du: invalid option -- 'q'
Try 'du --help' for more information.

The error above is clear: GNU utilities allow options mixed with file names for convenience; du is taking the file name -q as an option. And there isn't a -q option. (This is actually the best you can expect; worse would be if there were a -q option, and it did something unwanted.)

Stépahane's suggestion is to change your glob to ./*, which would result in du -sh ./foo ./-q—which of course is taken as a file name, because it no longer begins with -. The other option he suggests is to use --, which tells GNU utilities that there are no more options—only file/directory names.

Really you should always use either … ./* or … -- * instead of *, but we're all lazy…. Just be careful, especially if you don't trust all the file names.

derobert
  • 109,670
  • ok, my bug report is already send now, but I added a link to this question, so if @jmkane could look if this is the case, it would help the devs to know if they have to search for a bug or not. I did´t know this, never had any files on my system starting with -, but nice to know this. – switch87 Apr 07 '15 at 23:08
  • 1
    Note that -- is not just GNU but POSIX too so should be honored by most du implementations. – jlliagre Apr 07 '15 at 23:34
  • You were absolutely right. I can still use du -sh * in other folders, so it must be something new in the folder I've checked regularly, but I can't see anything obvious, and I'll make sure to use ./* or -- * in the future. – jmkane May 21 '15 at 15:46
4

du is taking the dash in the file and taking it as an argument.

If you pass '--' to the command, that signifies no further options will be sent, so run the command like this: du -fh -- * or du -sh -- *

worked for me...

this thread helped out :-http://www.webhostingtalk.com/showthread.php?t=1354139

  • Putting a double-dash at the end of the flag list fixed this, IMO this should be the accepted answer. – Jason D Sep 29 '23 at 14:09