Copy dot (hidden) files and normal files from one directory to another in Linux

Written by James McDonald

September 15, 2011

Something that has annoyed me for the longest time.

I have just downloaded a zip ball of the latest development code of gallery 3.

Typically you unzip the zipball and it creates randomly named folder e.g. “gallery-gallery3-dfbfe09” then you have to copy the contents of the randomly named folder over the contents of your current gallery3/ folder.

If you run cp -r gallery-gallery3-dfbfe09/* path/to/gallery3/ it omits the dot files located in the source directory specifically the .build_number and .htaccess files which means you may have upgraded gallery 3 but it won’t know about it because Gallery 3 use the .build_number file to check if it’s up to date.

If you run cp -r gallery-gallery3-dfbfe09/.* path/to/gallery3/ you copy not only the hidden files but files and folders in the directory above your gallery-gallery3-dfbfe09/ folder into path/to/gallery3/ annoying. If you have ever tried to copy the contents of /etc/skel/ to a users home directory and found you copied the entire /etc dir aswell you know what I mean.

Here is a way that works with the bash shell:

# specify two source arguments 1 to copy the hidden and another that picks up normal files
# with the target dir last
cp -rv gallery-gallery3-dfbfe09/.[^.]* gallery-gallery3-dfbfe09/* /path/to/gallery3/

# this doesn't require you to turn on extended shell globbing


Submit a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…