0

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?

2
  • -madepth 1 is only going to affect find, it won't have anything to do with cp -r
    – muru
    Commented Nov 22, 2023 at 7:37
  • Try "man cp" and read what -r does.
    – U. Windl
    Commented Nov 24, 2023 at 11:02

1 Answer 1

1

-maxdepth 1 is a directive for find. Because of it your find will run cp … for ./1 and it won't run cp … for ./1/1-1; and so on. Note it will run cp … for . in the first place.

When your find runs cp -p -r -v . /home/username/Downloads/results, the cp does not care about -maxdepth 1. It only sees its own arguments. And this exact command will copy ./1/1-1 because of -r.

See this: Understanding the -exec option of find. There is no magic, find -exec builds and runs standalone commands.

5
  • Ok I see...so then how can I copy nth level of directories with cp and find together?
    – anmac1789
    Commented Nov 22, 2023 at 7:43
  • @anmac1789 Please take our short tour to see how the site is designed to work. Your explicit questions were "How is this copying all the subfolders?", "How do I make sense of why this is happening?". I believe I have answered these. Asking a follow up question in comments does not fit the site. Editing your post is possible, but expanding it after it was answered is not a good practice. Consider asking a separate question. Commented Nov 22, 2023 at 7:52
  • my follow up question is related to the original question what do you mean ?
    – anmac1789
    Commented Nov 22, 2023 at 7:57
  • @anmac1789 The follow-up question is related, but answer(s) will be substantially different. I was able to answer the current questions, but maybe I cannot answer the follow-up. Other users may know answers to the follow-up and there is no need for them to duplicate my answer. In this case having two questions (i.e. posts, threads) is totally within the gist of the site; they will be related, we can link them, no problem. Having one post that covers all your questions may give you partial answers: my answer answers the original questions, new answers will answer the follow-up. Not good. Commented Nov 22, 2023 at 8:12
  • 1
    Ok, a new follow up question is linked to this post: unix.stackexchange.com/questions/762088/… I've edited my original post to let you know
    – anmac1789
    Commented Nov 22, 2023 at 8:30

You must log in to answer this question.

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