When working with the key * with BASH, becareful. And simply try to avoid it when you can. * usually does not select hidden files. Unless you tell it other wise with a shell option called dotglob. Shopt Dotglob. Usually * autocompletes to every nonehidden file and folder in current folder. Changing dotglob to be set as on, will make * autocomplete to every file & folder (hidden and not).

SIDE NOTE: bash comes with a set of options that can be set with “set” or “shopt”. I recommend google man page search “bash set” and “bash shopt” and have fun.

So when thinking do I run

Run du .  or the du so that you dont have to worry about dotglobness.

Same with cp. If your running cp /data/* /destination . Instead do cp /data /destination .

In bash the * keyword can work on hidden files and none-hidden files. But by default it will only work on none hidden files and folders.

If you ever do

You will notice that * only operates on none-hidden files and folders.  This is default bash option and its default setting.

You can make the * work on hidden files and folders as well. So * will select all files and folders where you are at.

This is useful when your running commands like du, to measure disk space. You cant just skip the hidden files & folders., because they take up space as well.

CAVEAT and NOT A BUG by EXAMPLE:

If you do something like this example:

When you have the default dotglob off. so * only sees nonehidden files and folders.

You will see that “ls” looks thru the current folder and shows the information for each none-hidden file and folder. running ls on a folder, everyone knows that the action will list its files inside. Well, becauseof the -a we have in ls. The subfolders of the none-hidden files will show their hidden files (even though * was used, and dotglob was off – see they are completely unrelated, * was only used for the ls command, but then we tell ls to show us whatever it wants with -a, inside the folders we tell it too look at)

So look at the hiddenfile it found, in side the cases folder

Why? because dotglob only affects the * and the command it ran on

After that find has the freedom to do what it wants, which is list every file and folder (no matter if its hidden or not) in the files specified.  Which will include showing that hidden file in the cases folder.

Now if I turn dotglob on, so that * selects hidden files and folders as well.

Now look at what find does:

I will skip showing the output of the above command, you just know that it will show all of the hidden files and folders

Essentially though, to end all of this mess. Just run it like this

That way you dont have to worry about * and if dotglob is on (and looks at hidden files as you will probably want it to), or dotglob is off.

ANOTHER EXAMPLE “Dotglob vs not Dotglob” WITH DU EXAMPLE

This is for the example from this article: Finding largest file

 

Leave a Reply

Your email address will not be published. Required fields are marked *