msgbartop
MAC OS X, Linux, Windows and other IT Tips and Tricks
msgbarbottom

11 May 15 Copying all files including hidden files in Linux command

PROBLEM:
If you use the command cp or mv on hidden files you will notice that the hidden files won’t get ‘seen’ and therefore not copied or moved. The problem doesn’t belong to cp or mv but to bash. Bash doesn’t include the hidden files in the globbing expansion.
for example:
mkdir ~/temp1 ~/temp2
touch ~/temp1/.file1 ~/temp1/file2
cp -a ~/temp1/* ~/temp2/
ls -la ~/temp2/
total 16
drwxrwxr-x 2 michel michel 4096 Mai 11 17:58 .
drwxr-xr-x 117 michel michel 12288 Mai 11 17:58 ..
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 file2

The file ~/temp/.file1 was not included in the copying.

SOLUTION: (one of them…)
Setting the bash option which will include all the files including the hidden ones in the globbing expansion.
shopt -s dotglob # for considering dot files (turn on dot files)
and
shopt -u dotglob # for don't considering dot files (turn off dot files)

Example:
shopt -s dotglob
cp -a ~/temp1/* ~/temp2/
shopt -u dotglob
ls -la ~/temp2/
#
total 16
drwxrwxr-x 2 michel michel 4096 Mai 11 18:01 .
drwxr-xr-x 117 michel michel 12288 Mai 11 17:58 ..
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 .file1
-rw-rw-r-- 1 michel michel 0 Mai 11 17:58 file2

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: