Parsing the output of ls
is always problematic. You should always use a different tool if you mean to process the output automatically.
In your particular case, your command was failing -- not because of some missing or incompatible argument to ls
-- but because of the glob you were sending it. You were asking ls
to list all results including hidden ones with -a
, but then you were promptly asking it to only list things that matched the */
glob pattern which does not match things beginning with.
and anything ls
might have done was restricted to things that matched the glob. You could have used .*/
as a second glob to match hidden directories as well, or you could have left the glob off entirely and just let ls
do the work. However, you don't even need ls
for this if you have a glob to match.
One solution would be to skip the ls
entirely and just use shell globing:*
$ du -s */ .*/ | sort -n
Another way which might be overkill for this example but is very powerful in more complex situations would be to use find
:*
$ find ./ -type d -maxdepth 1 -exec du -s {} + | sort -n
Explanation:
find ./
starts a find operation on the current directory. You could use another path if you like.
-type d
finds only things that are directories
-maxdepth 1
tells it only to find directories in the current directory, not to recurse down to sub-directories.
-exec [command] [arguments] {} +
works much like xargs
, but find
gets to do all the heavy lifting when it comes to quoting and escaping names. The {}
bit gets replaced with the results from the find.
du -s
you know
* Note that I used the -n
operator for sort
to get a numeric sorting which is more useful in than alphabetic in this case.
Expanding on the comment by @SamiLaine for using man
- one reason I didn't like it, was because I expected it to be tedious to set up, and I think this post will show that; but, it seems to work. First, some introduction can be found here:
First let's create the directory and add it to MANPATH:
mkdir ~/myreminderhelp
echo 'MANPATH=$MANPATH:'$(echo ~/myreminderhelp) >> ~/.bashrc
Let's test if it is found: close the terminal, open a new terminal; and then:
manpath -d 2>&1 | grep myrem
Unfortunately, this reports nothing for me; even if echo $MANPATH
says :~/myreminderhelp
.
Trying with global /etc/bash.bashrc
(after deleting the line in local ~/.bashrc
, and restarting terminal again):
sudo bash -c "echo 'MANPATH=$MANPATH:'$(echo ~/myreminderhelp)" # check home dir printout
sudo bash -c "echo 'MANPATH=$MANPATH:'$(echo ~/myreminderhelp) >> /etc/bash.bashrc"
Restart terminal; still manpath
does not report this directory. Let's now try this:
sudo bash -c "echo 'MANDATORY_MANPATH '$(echo ~/myreminderhelp) >> /etc/manpath.config"
Close and reopen terminal again; finally, we get:
$ manpath
/usr/local/man:/usr/local/share/man:/usr/share/man:/media/extern/texlive/2011/bin/i386-linux/man:~/myreminderhelp
In fact, after finding this, I erased the line from /etc/bash.bashrc
, and manpath
still reports the directory. So, I guess editing /etc/manpath.config
is all that is needed.
Ok, so let's create an example custom reminder file here:
echo "Just a bit of a reminder...
Use nmcli con --help" >> ~/myreminderhelp/nmcli-reminder.txt
Then use txt2man
to get a man
formatted file, and gzip it:
cat ~/myreminderhelp/nmcli-reminder.txt | txt2man > ~/myreminderhelp/nmcli-reminder.1
gzip ~/myreminderhelp/nmcli-reminder.1
Restart terminal again, try typing man nm
and press TAB - autocompletion shows nmcli-reminder
is not found...
So let's try put our files in a man
section subfolder; the links above indicate section 7 would be appropriate; so:
mkdir ~/myreminderhelp/man7
mv ~/myreminderhelp/nmcli-reminder.* ~/myreminderhelp/man7/
tree ~/myreminderhelp # to check - ok
Restart terminal again; try typing man nm
and press TAB - autocompletion finally works:
$ man nmcli # and here press TAB:
nmcli nmcli-reminder
... but now we have this problem:
$ man nmcli-reminder
No manual entry for nmcli-reminder
Damn it. Could be that currently, our file is nmcli-reminder.1.gz
indicating section 1 - let's rename it:
mv ~/myreminderhelp/man7/nmcli-reminder.1.gz ~/myreminderhelp/man7/nmcli-reminder.7.gz
man nmcli-reminder
... and finally the man
command works!
So for this use case, probably it's best to keep the source .txt
files directly in ~/myreminderhelp/
, and then generate man pages in the appropriate subfolder - as in:
$ tree ~/myreminderhelp/
~/myreminderhelp/
├── man7
│ └── nmcli-reminder.7.gz
└── nmcli-reminder.txt
... the appropriate generation commands for this being:
cat ~/myreminderhelp/nmcli-reminder.txt | txt2man > ~/myreminderhelp/man7/nmcli-reminder.7
gzip ~/myreminderhelp/man7/nmcli-reminder.7
And here is a ~/myreminderhelp/buildreminders.sh
script:
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
for ix in $DIR/*.txt; do
bx=$(basename $ix)
isn=${bx%%.txt}
echo Processing $isn;
set -x
# txt2man -t "$isn" -s 7 $ix > $DIR/man7/${isn}.7
pandoc -f markdown -t man -s -o $DIR/man7/${isn}.7 $ix
gzip --force $DIR/man7/${isn}.7
{ set +x; } 2>/dev/null
done
tree -a $DIR
EDIT: Turns out, it is very difficult to get txt2man
to leave literal source code unformatted, as it tends to auto-extract information like man
(sub)sections etc. I've modified the script above to use pandoc
instead (via WritingManPages - HerzbubeWiki) -- at least with pandoc
with a Markdown input, you have some control over what is literal preformatted text, and what isn't ...
However, both of these tools will indent contents, as is typical for a man
page (since there, section headings are non-indented, rest of the text content as in paragraphs, is). And I'm not sure I end up liking that all too much...
Best Answer
The typical way of doing this on Unix- or Linux-style systems is to use shell aliases:
Note that this will overwrite any existing
ls
alias, so you might want to check the output offirst and combine the above; for example
To make this permanent, add it to your
.bashrc
file in your home directory (assuming you’re using Bash, which you probably are if you’re discovering all this).Altering the default behaviour in such an extensive way can end up being confusing, so I’d suggest creating a new command using an alias instead; I have
for example. This also allows building upon other aliases: this
l
alias uses whatever is defined asls
, and if that’s an alias, that gets used to (so there’s no need to repeat the--color=tty
option in this case).To remove an alias, use
unalias
: