37

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?

11 Answers 11

46

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
2
  • That's only a problem if you use -r. Without -r, it will just skip the directories.
    – Mikel
    Apr 21, 2014 at 1:44
  • And the example I give using -R should work just fine too. If you think it's wrong, please explain why.
    – Mikel
    Apr 21, 2014 at 1:48
8

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
2
  • Using dotglob OP'll also copy all non-hidden files, which is not what he wants.
    – peoro
    Apr 1, 2011 at 0:07
  • @peoro, you should read the question again. All files in A is hidden. He just want to copy that.
    – kurumi
    Apr 1, 2011 at 0:14
8

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.

2
  • 1
    This 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
    Sep 12, 2014 at 15:30
  • This miss also single char files like 'g'
    – luzik
    Apr 2, 2020 at 8:39
2

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

cp A/.* B/
1
  • 2
    This 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
    Apr 1, 2011 at 0:09
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.

1
  • 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.
    – kurumi
    Apr 1, 2011 at 0:40
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!

1

You can use this script

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

0

copy:

cp A/.* B

list:

ls -l A/.*

list files without descending directories:

ls -ld A/.*
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
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/

0

This may not be the cleanest solution but it works:

find . -type f -name ".*" | xargs -I{} cp {} destinationDir/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .