Why Does Using `*` in the `ls` Command Overrule the `-B` Option?

0
27
Asked By CuriousCoder42 On

Hey everyone! I'm trying to figure out why using `*` seems to have priority over the `-B` option in the `ls` command. I was looking for files that start with the letter 'L' but want to avoid any backup files. I have two ways to do this: one is using the command `l L*`, which is an alias for `ls -B`, and the other is using `ls -B L*`. When I run both commands, I still see both 'Lubuntu' and 'Lubuntu~' listed. I thought the `-B` option was supposed to filter out backup files, but it's like `*` takes precedence. Can someone clarify this for me? Thanks!

5 Answers

Answered By TechWhiz99 On

It sounds like you're running into a common misunderstanding with how the bash shell processes wildcards. When you use `L*`, bash expands that to match any file that starts with 'L', including backups like 'Lubuntu~'. By the time `ls` runs, it sees those names directly and doesn't apply the `-B` option to them because they're explicitly listed. If you want to use `-B` effectively, you should run `ls -B` without specifying a filename or wildcard.

SyntaxSavant -

Good point! It’s all about when the filtering happens. Wildcards are expanded before the command runs, so `-B` won't filter anything that's already been explicitly listed.

Answered By BashGuru88 On

Exactly! The `-B` option only applies to 'implied' files, which means files that are in the directory but not specified as an argument. Since 'Lubuntu' and 'Lubuntu~' are directly passed in as arguments when you use `L*`, they bypass the `-B` filter. If you run `ls -B` without any files, then it will correctly filter out the backups if they aren't explicitly mentioned.

FileMaster -

Definitely! Just remember that `*` is processed by bash during the command expansion, so it takes precedence and gives explicit filenames to `ls`.

Answered By WildCardWarrior On

One more thing to consider is that using `--ignore-backups` or `--ignore=` would affect this too, but again, those are for files that aren't explicitly mentioned in the command. So in your case, you must manage how you're calling `ls` and what arguments you pass to it.

Answered By LinuxLegend On

Just to add on, if you want to specifically list files that start with 'L' but exclude backups, you can use `ls -B | grep "^L"` to filter the output after getting all files. That way, you'll have the backup files excluded.

Answered By VerboseVox On

If you want to learn more, check the documentation for bash and `ls`. It can get quite intricate with how arguments and wildcards interact!

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.