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 ..?

marked as duplicate by Gilles, muru, mosvy, Jeff Schaller Jun 3 at 11:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
  • I've already given an answer but do you want to copy all of the files in the current directory or just the hidden ones? From your question, it's not clear as to whether you are just trying to copy only hidden files or all the files in the directory while excluding the directory itself and the parent directory. – Nasir Riley Jun 3 at 4:46
0

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 )
-1

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

cp /root/test/.??* /root/test1
  • 4
    What if file name is .a? – dedowsdi Jun 2 at 7:15
  • Exactly! How would I do that in such a case where filename is .a? – Haris Jun 2 at 11:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.