0

I created a function in my .bashrc file to print the top N files/directories in the working directory:

# .bashrc
function topN () { du -hs * | sort -rh | head -"$1"; }

However, sourcing my .bashrc and then running topN 5 returns:

head: cannot open '5' for reading: No such file or directory

I have attempted various quoting methods and combinations, with generally the same error. How can I re-write this function to treat the argument 5 as a value rather than a file/directory?

Simply running du -hs * | sort -rh | head -5 returns the expected listing of the top 5 files/directories and their sizes.

IslandPatrol
  • 261
  • 3
  • 11

1 Answers1

1

You were running a (stale) alias rather than the function you shared with us.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
  • Note that by adding an answer and having it upvoted or accepted, you blocked the OP from deleting their own question. For a question that's off-topic (in this case, eligible for close on acount of being "resolved in a way unlikely to help other users"), that's poor form. – Charles Duffy Sep 10 '22 at 02:59
  • Shall I un-accept the answer @CharlesDuffy? – IslandPatrol Sep 10 '22 at 03:01
  • @CharlesDuffy hmm... root cause was: I thought that I was doing x but actually doing y. This is debugging 101. Makes no difference to me. It usually annoys me when people delete their question after I answered it (only they derived value of my effort instead of the larger community). – Allan Wind Sep 10 '22 at 03:10
  • @CharlesDuffy you mentioned "you blocked the OP from deleting their own question". However, there is a [Delete question] link presented at the top of my screen. Unless this is a case of poor UI design, it appears that I could, in fact, delete the question. – IslandPatrol Sep 10 '22 at 03:13
  • @IslandPatrol, if you don't have enough reputation points it won't let you do that if the question is open and has an upvoted answer. (That's a deliberate design decision, because we've long had a problem with students asking questions to support their homework, then deleting them to hide evidence of academic dishonesty after receiving an answer). If it's being permitted for you right now, I suspect that's likely on account of the question being closed (or perhaps the threshold below which that rule applies is much lower than I remember). – Charles Duffy Sep 10 '22 at 23:46