Blog

03/06/2019

Bash: .* Considered Harmful To Match Dot Files. Why?



How do I match dot files under Unix / Linux using bash shell? Why .* considered as harmful when matching dot files under bash shell?

Short answer.* matches the special “..” link in the directory and your command ends up getting applied to the parent directory. This may end up in unexpected results. Hence, .* considered harmful.

Long answer – Say, you want to run a chmomd/chown or find command on all of the dot files in a directory called /home/nixcraft/projects. You type the following command in $HOME/projects/ to match all dot files:

chmod -R 0444 .*

OR

chown -R user:group .*

This will change the permission for both in the current and parent directory as .. will be matched by .*. This may result into unexpected security related issues and few other problems. Many Linux / Unix commands offers an option to work recursively on files. In this example, the -R option will match the current and all sub-dirs of parent directory. This can result into disaster if you have several thousand files. Restoring and cleaning from this kind of problems can be time consuming. Use the following syntax to match dot files:

chmod -R 0444 .[^.]*

OR

chown -R user:group .[^.]*

OR

ls -R .[^.]*

OR

find . -iname ".[^.]*" -print

OR

find . -iname ".[^.]*" -ls

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....