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

# NOT AS GOOD BECAUSE DEPEING ON CURRENT VALUE OF "DOTGLOB" THIS WILL ACT DIFFENT
du *
# the above will only du the none-hidden files and folders if dotglob is off (as it is by default)
# but most likely you want du to look thru all files and folders (even hidden ones), so you will need to turn on dotglob

# THIS WILL ALWAYS ACT THE SAME AND LOOK AT ALL FILES AND FOLDERS IN CURRENT FOLDER:
du .
# or similarly, will act as above
du

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.

shopt -s dotglob
# the above will make the keyword * select hidden and none hidden files and folders

shopt -u dotglob
# the above will bring the bash property dotglob its default value (of dotglob off). So that* only works on none-hidden files and folders.

# to see current status of dotglob, is it on or off.
shopt -s dotglob

# trick to remember what turns it on and turn it off
# shopt -s : s for SET as in ENABLE
# shopt -u : u for UNSER as in DISABLE

If you ever do

ls -lisah *
# or
cat *
# or do this to see what files and folders its selecting:
echo *

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:

# default: shopt -u dotglob
# here you can see
shopt dotglob
# output: dotglob         off
cd /root
find *

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

# find *
WORDPRESS-BACKUPS
WORDPRESS-BACKUPS/starter.sh
WORDPRESS-BACKUPS/wordpress-d11-19-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d04-01-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d03-24-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d07-31-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d02-22-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-new.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d08-06-14-t01-00-01.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d01-18-14-t14-38-40.7z
WORDPRESS-BACKUPS/_archive-of-backups/wordpress-d01-26-14-t22-54-22.7z
WORDPRESS-BACKUPS/wordpress-d11-18-14-t01-00-01.7z
WORDPRESS-BACKUPS/BACKUP-WP.sh
WORDPRESS-BACKUPS/wordpress-d11-13-14-t01-00-01.7z
WORDPRESS-BACKUPS/wordpress-d11-16-14-t01-00-02.7z
WORDPRESS-BACKUPS/wordpress-d11-15-14-t01-00-01.7z
WORDPRESS-BACKUPS/wordpress-d11-19-14-t10-24-10.7z
WORDPRESS-BACKUPS/wordpress-d11-20-14-t01-00-01.7z
WORDPRESS-BACKUPS/wordpress-d11-14-14-t01-00-01.7z
WORDPRESS-BACKUPS/wordpress-d11-17-14-t01-00-01.7z
cases
cases/randomcase
cases/randomcase/.hiddenfile
cases/randomcase/randomcase-customer-case-11-drives-trying-to-find-6.txt
cases/randomcase/randomcase-allstats.txt
cases/randomcase/randomcase-superinfo.tgz
cases/randomcase/randomcase-customer-case-11-drives-trying-to-find-6-disk-smart.txt
certs
certs/readme-cacert-03-25-2014.txt
certs/infotinks-ssl-ram.csr
certs/infotinks-ssl-ram-cacert.crt
certs/infotinks-ssl-www-cacert.crt
monitor
monitor/monitor.sh
monitor/monitor-wrap.sh
scripts
scripts/rename.pl
scripts/screenfetch-dev

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

# Basically when I ran "find *"
find *
# is the same as
find WORDPRESS-BACKUPS cases certs monitor scripts

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.

# turn on dotglob
shopt -s dotglob
# check its status
shopt dotglob

Now look at what find does:

# Basically when I ran "find *" 
find *http://www.infotinks.com/wp-admin/post-new.php#
# its the same as this, when dotglob is on
find WORDPRESS-BACKUPS .bashrc .ssh cases certs monitor scripts

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

find .
# or simply 
find

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

# Default DOTGLOB
shopt -u dotglob
du -hsx * | sort -rh | head -10
# 2.6G WORDPRESS-BACKUPS
# 25M cases
# 144K scripts
# 76K certs
# 12K monitor

# DOTGLOB ON
shopt -s dotglob
du -hsx * | sort -rh | head -10
# 2.6G WORDPRESS-BACKUPS
# 61M .phoronix-test-suite
# 25M cases
# 144K scripts
# 76K certs
# 48K .subversion
# 36K .bash_history
# 20K .viminfo
# 20K .ssh
# 20K .config

# DONT FORGET TO SET DOTGLOB BACK TO OFF, so that its back to defaults. Its good behaviour
shopt -u dotglob

# Generally I just run them together in one line so that I dont forget like this
shopt -s dotglob; du -hsx * | sort -rh | head -10; shopt -u dotglob

# or in a subshell so that the dotglob property being on in the subshell, doenst even carry over into the main parent shell (subshells are made for this reason)
(shopt -s dotglob; du -hsx * | sort -rh | head -10;)

 

Leave a Reply

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