1

I am using CentOS7 and when I use command

cp /root/test/.* /root/test1

it also copies .. which copies all the files and folder in the parent directory of the source.

How to avoid copying . and ..?

Haris
  • 19

2 Answers2

1

It's not explicitly stated in the question whether you want to copy only hidden names, or whether you'd want to copy all files. Likewise whether you want to copy the contents of any subdirectories. I'm going to assume that you want to copy only hidden name is the current directory (because that you seem to want to do with your command).

Your command would not copy all the files in the parent directory unless you also used the -R option with cp (or -r with GNU cp) to do a recursive copy. Without the -R option, cp would complain about . and .. being directories, but and would not copy them.

In the bash shell, set the GLOBIGNORE shell variable to the :-delimited list of names that you don't want the shell to expand filename globbing patterns to,

GLOBIGNORE='.:..'

then copy your files,

cp /root/test/.* /root/test1

To set GLOBIGNORE temporarily, just for the copy operation, use a sub-shell:

( GLOBIGNORE='.:..'; cp /root/test/.* /root/test1 )
Kusalananda
  • 333,661
0

The standard idiom for globbing all names beginning with a dot except . and .. is .??*. So your command would be

cp /root/test/.??* /root/test1
Johan Myréen
  • 13,168