How do I copy file named starting with a dot?

28

2

I am trying to copy all files under directory A to directory B. All files under directory A are starting with dot, for example:

A/.a
A/.b
A/.c

which I found if I use: cp A/* B, always get error:

cp: cannot stat 'A/*': no such file or directory

It seems there is no option for cp as ls to handle entries started with dot, anyone has idea how to fix it?

user59285

Posted 2011-03-31T23:54:12.903

Reputation: 415

Answers

36

The reason is because in bash, * does not include files starting with dot (.).

You can run

cp A/.* B

It will warn you that it did not copy . or .., or any subdirectories, but this is fine.

Or, if you want to copy dot files and normal files together, run

cp A/.* A/* B

You could also run

shopt -s dotglob
cp A/* B

which will work in bash, but not sh.

And if you don't mind subdirectories being copied too, then this is the easiest:

cp -R A/ B

Tip: If ever wildcards aren't doing what you expect, try running it with echo, e.g.

$ echo A/*
A/file1 A/file2

$ echo A/.*
A/. A/.. A/.hidden1 A/.hidden2

$ echo A/.* A/*
A/. A/.. A/.hidden1 A/.hidden2 A/file1 A/file2

$ shopt -s dotglob
$ echo A/*
A/file1 A/file2 A/.hidden1 A/.hidden2

Mikel

Posted 2011-03-31T23:54:12.903

Reputation: 7 890

That's only a problem if you use -r. Without -r, it will just skip the directories. – Mikel – 2014-04-21T01:44:43.617

And the example I give using -R should work just fine too. If you think it's wrong, please explain why. – Mikel – 2014-04-21T01:48:07.777

7

If bash, you can set dotglob before you copy

shopt -s dotglob
cp A/* /destination

Or a programming language

$ ruby -rfileutils -e  'Dir[".*"].each {|x| FileUtils.copy(x,"/destination") if File.file?x}'

If you don't want to set dotglob, just

cp A/.* /destination 2>/dev/null

kurumi

Posted 2011-03-31T23:54:12.903

Reputation:

Using dotglob OP'll also copy all non-hidden files, which is not what he wants. – peoro – 2011-04-01T00:07:59.713

@peoro, you should read the question again. All files in A is hidden. He just want to copy that. – None – 2011-04-01T00:14:37.090

7

What you're looking for is more along the lines of:

cp A/.??* B/

This will match all dotfiles, but not "." or "..". Most of the above solutions are fine as long as you're not working recursively. But as soon as you want to do something like:

cp -R A/.??* B/

Without omitting ".." you'll copy everything from the parent directory on down, including non-dotfiles.

John Westlund

Posted 2011-03-31T23:54:12.903

Reputation: 75

1This will miss dot-files that are a single character, like .a or .x. The shortest globbing pattern I have found that matches every dot file except . and .. is .[^.]*. – pavon – 2014-09-12T15:30:24.297

2

I justed tried the following and it works just find...

cp A/.* B/

Andrew White

Posted 2011-03-31T23:54:12.903

Reputation: 271

2This will also pass A/. and A/.. as parameters to cp, which could be problematic in case OP also needs to copy directories (using cp -r) – peoro – 2011-04-01T00:09:12.630

2

That's not cp's fault, it's bash: bash expands * in all the non-hidden (ie: non starting with .) files.

Bash will expand .* (thus A/.*, in your case) with all the files starting with ., but unluckily it also includes . and .. (current and parent directories) which you will probably want to skip. (Note that other shells, like zsh, wouldn't include them, and IIRC also bash, after setting some options).

An easy solution could be to remove . and .. from files matched by .*, in a (very) hacky way like this one:

cp $( for F in A/.*; do echo $F | grep -v "^\.*$"; done ) B

or this one (probably cleaner: it uses find to find the files to copy):

cp $( find A -maxdepth 1 -mindepth 1 -name ".*" ) B

but you'll likely find cleaner solutions.

peoro

Posted 2011-03-31T23:54:12.903

Reputation: 943

why should you use a for loop and then create extra grep process just to do a job like cp A/.* ? cp will ignore the "." and ".." directories. – None – 2011-04-01T00:40:47.287

1

If they all start with a dot, just use A/.*:

[holt@Michaela test]$ cp A/* B
cp: cannot stat `A/*': No such file or directory

[holt@Michaela test]$ cp A/.* B
cp: omitting directory `A/.'
cp: omitting directory `A/..'
[holt@Michaela test]$ ls -al B
total 8
drwxrwxr-x. 2 holt holt 4096 2011-03-31 16:57 .
drwxrwxr-x. 4 holt holt 4096 2011-03-31 16:57 ..
-rw-rw-r--. 1 holt holt    0 2011-03-31 16:57 .a
-rw-rw-r--. 1 holt holt    0 2011-03-31 16:57 .b
-rw-rw-r--. 1 holt holt    0 2011-03-31 16:57 .c

Hope this helps!

Xavier Holt

Posted 2011-03-31T23:54:12.903

Reputation: 111

1

You can use this script

IFS=$'\n';for act in $(find A -d 1); do cp -R "$act" B; done

teki

Posted 2011-03-31T23:54:12.903

Reputation: 111

0

This is an old thread... but I've always been able to copy all files. Including files/folders that begin with '.'

Example will copy all files and folders:

cp -aup /root/.* /backup/root/

Ontologik

Posted 2011-03-31T23:54:12.903

Reputation: 1

0

copy:

cp A/.* B

list:

ls -l A/.*

list files without descending directories:

ls -ld A/.*

Blessed Geek

Posted 2011-03-31T23:54:12.903

Reputation: 553

0

You must exclude . and .. from the files list passed to cp!

This is safe:

ls -1d A/.[a-zA-Z]* | xargs -i cp -rp {} B

AmitP

Posted 2011-03-31T23:54:12.903

Reputation: 191