I have a directory structure like this:
test folders
├── 1
│ ├── 1-1
│ ├── 1-2
│ └── 1-3
├── 2
│ ├── 2-1
│ ├── 2-2
│ └── 2-3
├── 3
│ ├── 3-1
│ ├── 3-2
│ └── 3-3
└── 4
├── 4-1
├── 4-2
└── 4-3
16 directories, 0 files
and I am struggling with copying ONLY the 1st level folders -- 1,2,3,4 excluding all subfolders using find & cp commands
├── 1
├── 2
├── 3
└── 4
working directory is: "/home/username/Downloads/test folders"
I used
username@username-desktop:~/Downloads/test folders$ find . -maxdepth 1 -type d
and the output is
.
./2
./3
./4
./1
If I combine the 1st part of the command with cp: find . -maxdepth 1 -type d -exec cp -p -v "{}" "/home/username/Downloads/results" \;
then the result is:
username@username-desktop:~/Downloads/test folders$ find . -maxdepth 1 -type d -exec cp -p -v "{}" "/home/username/Downloads/results" \;
cp: -r not specified; omitting directory '.'
cp: -r not specified; omitting directory './2'
cp: -r not specified; omitting directory './3'
cp: -r not specified; omitting directory './4'
cp: -r not specified; omitting directory './1'
so then I added -r switch to the above command:
username@username-desktop:~/Downloads/test folders$ find . -maxdepth 1 -type d -exec cp -p -r -v "{}" "/home/username/Downloads/results" \;
'./1' -> '/home/username/Downloads/results/./1'
'./1/1-1' -> '/home/username/Downloads/results/./1/1-1'
'./1/1-2' -> '/home/username/Downloads/results/./1/1-2'
'./1/1-3' -> '/home/username/Downloads/results/./1/1-3'
'./2' -> '/home/username/Downloads/results/./2'
'./2/2-1' -> '/home/username/Downloads/results/./2/2-1'
'./2/2-2' -> '/home/username/Downloads/results/./2/2-2'
'./2/2-3' -> '/home/username/Downloads/results/./2/2-3'
'./3' -> '/home/username/Downloads/results/./3'
'./3/3-1' -> '/home/username/Downloads/results/./3/3-1'
'./3/3-2' -> '/home/username/Downloads/results/./3/3-2'
'./3/3-3' -> '/home/username/Downloads/results/./3/3-3'
'./4' -> '/home/username/Downloads/results/./4'
'./4/4-1' -> '/home/username/Downloads/results/./4/4-1'
'./4/4-2' -> '/home/username/Downloads/results/./4/4-2'
'./4/4-3' -> '/home/username/Downloads/results/./4/4-3'
so now the results folder has:
results
├── 1
│ ├── 1-1
│ ├── 1-2
│ └── 1-3
├── 2
│ ├── 2-1
│ ├── 2-2
│ └── 2-3
├── 3
│ ├── 3-1
│ ├── 3-2
│ └── 3-3
└── 4
├── 4-1
├── 4-2
└── 4-3
How is this copying all the subfolders ? even though I included -r but I also included - maxdepth 1 from the beginning in the find command. How do I make sense of why this is happening?
[edit] A follow up question is linked to here: How to exclude different levels of subdirectories with cp?
-madepth 1
is only going to affectfind
, it won't have anything to do withcp -r
-r
does.