Hey everyone! I was trying to list files starting with the letter 'L' while avoiding backup files. I considered two commands: using the letter 'l' (which is an alias for `ls -B`) and running `ls -B L*` to achieve the same goal. However, when I executed both commands, I found that they listed both 'Lubuntu' and 'Lubuntu~'. I'm confused because 'l' should filter out backup files, just like `ls -B` does. Why is that the wildcard `*` seems to take priority over the `-B` option? Thanks in advance for your insights!
5 Answers
You can also try piping the output of `ls -B` to `grep` to filter the results further. For example: `ls -B | grep '^L.*'` will give you clean results without backup files starting with 'L'.
Check out the `man` page for globbing terms. The `*` wildcard expands the list of files before it's sent to `ls`, so it overrides filtering options like `-B`. If you really want to ensure backups don’t show, list files explicitly or use a command that filters after expansion.
If you want to exclude backup files while still using the wildcard, try using the command `ls --ignore=*~ L*`. This way, you can list your 'L' files without the backups showing up!
It seems like there's some confusion with how shell wildcards work. When you use `L*`, bash expands that before passing it to `ls`, so the command you actually run looks like `ls -B Lubuntu Lubuntu~`. Since you provided explicit filenames, the `-B` option gets ignored for those entries. The `-B` flag only filters out files that are not explicitly listed, so that's why the backup file shows up!
Just a heads up: the `--ignore-backups` option in `ls` only acts on files in the directory that are implied, not on files you're specifically listing. So when you use `L*`, those files are explicitly mentioned, and anything relating to '-B' doesn’t filter those out anymore.
Good point! This is also why you might not notice a difference when using wildcards versus when you run `ls -B` without any arguments. It’s all about how the shell interprets those patterns!
Exactly! When you include file patterns, they are treated as explicit arguments, which means `-B` doesn’t apply to them. You would see the effect of `-B` if you just ran `ls -B` without specifying any files.