Score:-1

How to copy ONLY parent folder with find and CP?

im flag

Why does the following command copy the DCIM folder and it's subfolders and files..I just want the DCIM folder to be copied.

find ./"DCIM" -name "DCIM" -maxdepth 0 -type d -exec cp -a -p -i "{}" "/home/username/Documents" \;

if I do

find ./"DCIM" -name "DCIM" -maxdepth 0 -type d -print

then only the DCIM folder is shown

How can I use find and cp together to copy just the parent folder without the subfolders and files contained within? There are alot of files inside of the DCIM folder so having exclude for each file or pattern is cumbersome and not really practical. Is there a solution to this?

sudodus avatar
jp flag
`cp -a` is equivalent to `cp -dR --preserve=all`; in other words, it recurses through the directory tree ( the `-R` option). See `man cp` for a complete list of options.
ghostanime2001 avatar
im flag
so leaving out the -a and -R gives me a message saying cp: -r not specified; omitting directory './DCIM'
sudodus avatar
jp flag
Either let `find` 'list' the files or let `cp` 'list' them. For example, if you know that you want to work only on `DCIM`: `cp DCIM/* ...` or if there are several directories there: `cp */* ...` should work. Use `-d --preserve=all` if you want that kind of action.
sudodus avatar
jp flag
... or use `find -maxdepth=1`; test first that the correct set of files are listed before running anything.
sudodus avatar
jp flag
If I understand correctly what you want to do in this case, you need only `cp`. You need not involve `find`. (An alternative is `rsync` which can do the same things as `cp` and also a lot more. `rsync` is a very good tool for copying and backup within a computer but also between computers (via a network).)
Score:1
jp flag

You need only cp. You need not involve find. Try the following command.

cp -dpi DCIM/* "/home/username/Documents"

from the directory 'above DCIM', where you run your previous command lines (with find).

It should work (if the path to the target directory exists and you have enough permissions to perform the operation; you need sudo to write to another user's home directory).

If you want to write to a separate directory in the target drive, you can create it:

mkdir -p "/home/username/DCIM"

and then use

cp -dpi DCIM/* "/home/username/DCIM"
ghostanime2001 avatar
im flag
so using this command cp -dpi DCIM/* "/home/username/Documents" i get a bunch of lines saying cp: -r not specified; omitting directory [subfolder path] My working directory is in the parent folder which contains the DCIM folder. I do not want to create new directories just copying is enough so no need for mkdir I'm surprised there is no option to exclude all files and subfolders for a simple command as this...there isn't even a include only command...
sudodus avatar
jp flag
You wrote in the question, that you do not want to copy any subdirectories. This command works like that, but writes a warning, that there are subdirectories that are omitted from the copy process. Did you change your mind? Do you want the subdirectories (and their content) or not?
ghostanime2001 avatar
im flag
maybe there was a misunderstanding..what I wanted was to copy the DCIM/ folder excluding all subdirectories and files within...so the target directory should be /home/username/DCIM without files and subfolders
sudodus avatar
jp flag
There are many options in `rsync` that can help you specify in detail how to include/exclude things. `cp` is a simpler command, but it can be used in this case.
sudodus avatar
jp flag
Without files? Or only the files directly in the top layer of DCIM, no subdirs, no files in subdirs?
ghostanime2001 avatar
im flag
What I want is to exclude everything below DCIM/ so, DCIM/ --> copy DCIM/subfolder 1/ -->do not copy DCIM/subfolder 1/file 1.txt -->do not copy DCIM/subfolder 1/file 2.txt -->do not copy DCIM/subfolder 2/ -->do not copy DCIM/subfolder 2/file 1.txt -->do not copy DCIM/subfolder 2/file 2.txt -->do not copy DCIM/subfolder 3/ -->do not copy DCIM/subfolder 3/file 1.txt -->do not copy DCIM/subfolder 3/file 2.txt -->do not copy DCIM/file 1.txt -->do not copy DCIM/file 2.txt -->do not copy DCIM/file 3.txt -->do not copy in the target directory
sudodus avatar
jp flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/146558/discussion-between-sudodus-and-ghostanime2001).
ghostanime2001 avatar
im flag
or to put it in a more simpler way, include only the DCIM directory, without files without subdirectories
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.