1

I have a folder structure like this

a/
-b/
-c.txt
-d.txt
-backups/

I want to move the contents of folder a into backups so the folder structure is this.

a/
-b/
-c.txt
-d.txt
-backups/
    -b/
    -c.txt
    -d.txt

Here are the commands I have used so far.

for d in a/*/ ; do
    mkdir -p ${d}backups/
    cp -ra ${d}* backups
done

I make the folder backups then I try to copy the content into the backups folder. However, I get the error: CP Hardlink cannot copy folder onto itself. How can i do this?

Thank You

chaitanya.varanasi
  • 960
  • 1
  • 9
  • 26
  • See: [Copy folder recursively, excluding some folders](http://stackoverflow.com/q/2193584/3776858) – Cyrus May 03 '16 at 19:24

2 Answers2

2
a
├── b
├── backups
├── c.txt
└── d.txt

2 directories, 2 files

Enable extglob by shopt -s extglob and execute cp -r !(backups/) backups. The following will be the result:

a
├── b
├── backups
│   ├── b
│   ├── c.txt
│   └── d.txt
├── c.txt
└── d.txt

3 directories, 4 files
Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
1

it is trying to copy "backups" into "backups" , so you need to make sure you exclude "backups" from the a/*/ pattern.

you should probably use "find" to find files with a given pattern and exclude the "backup" directory. With find you can do "-not -name backup"

Markus Mikkolainen
  • 3,397
  • 18
  • 21