How do I copy file names starting with a dot in Linux?

UNIX / Linux file systems do not have a hidden file attribute. A file or folder is typically hidden by using a period as the first character in the file name i.e. .hiddenfile .

In order to list all files and directories you can use the ls command.

Linux ls command without arguments
Linux ls command without arguments

To list all files and folders including hidden (starting with a dot) files and directories use ls -a or ls -Ra to recursively list all files and folders under the directory.

Linux ls command with arguments
Linux ls command with arguments

We know bash does not include files with a dot. Additionally, *  does not include files with a dot too. Consider the command ls TestOne/* .

Linux ls command with asterisk
Linux ls command with asterisk

If you want to copy only hidden files from TestOne to TestTwo but not sub-folders run cp TestOne/.* TestTwo . You will get a warning that it did not copy .  or .. .

Linux cp, copy hidden files only
Linux cp, copy hidden files only

If you want to copy hidden files and normal files together, ignoring sub-directories run the command cp TestOne/.* TestOne/* TestThree .

Linux cp, copy hidden and normal files
Linux cp, copy hidden and normal files

You can also use shopt -s dotglob and then use cp  commands like cp TestOne/* TestFour or cp -R TestOne/ TestFive if you want to copy sub-directories too.

Linux cp using shopt -s dotglob
Linux cp using shopt -s dotglob

If you get stuck with wildcards test them with the echo  command before running cp  or mv  commands. For example echo TestOne/*  or echo TestOne/.* .

Linux echo wildcard test
Linux echo wildcard test

 

 

You May Also Like