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.

You May Also Like…


Zebra Printer Language The above website has a page where you can enter ZPL and it...

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...

PHP array_map Multiple Arrays

array_map can take multiple arrays. I like how it starts mapping through them starting at the first element of each...