Why does `*` seem to take priority over the `-B` option in the ls command?

0
3
Asked By Curious_Coder42 On

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

Answered By NerdyDev_23 On

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'.

Answered By CommandLine_Pro On

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.

Answered By ShellSavvy_21 On

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!

Answered By TechWhiz_99 On

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!

ScriptNinja_87 -

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.

Answered By BashGuru_42 On

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.

CodeMaster_88 -

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.