Score:0

Copy all from a folder into the project root directory

in flag

I have a web project with the following structure:

application/
dumps/
nginx/
logs/
.git/
docker-compose.yml
Dockerfile
README.md
.gitignore

For several reasons, I would like to dissolve the application folder and copy all files to the root directory. I would like to do this as usual via the CLI. However, I am not getting anywhere at the moment.

I have tried the following. I am in the root of the project.

cp -r application/ ./
// output cp: 'application/' and './application' are the same file

cp -r app/*.* ./
// copy only the root files from application to the project root and not the folders.

Which is kind of logical. How do I formulate the command so that it only copies the files to the root? It's probably just a small thing but it's too early for me.

Maik Lowrey avatar
in flag
@NateT I would remove applcaition but i will copie before removing all the contents to the root. Yes.
terdon avatar
cn flag
Remember that most files don't have extensions on Linux systems so the `*.*` is almost never what you want since that will only match files or directories that have a `.` in their name. To match everything, you just want `*`.
Score:0
in flag

Ok. I got it. It was really to early ;-) The right command would be:

cp -a app/. ./

And better to use -a option instead of -r option. It is an improved recursive option and means, that preserve all file attributes, and also preserve symlinks. Found it: How can I copy the contents of a folder to another folder in a different directory using terminal?

The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

bac0n avatar
cn flag
think you meant to do `cp -T app/ .` (probably wise to append `-i`)
Maik Lowrey avatar
in flag
@bac0n Thanks for comment. Why you advice to append the `i` option? and what means -T? i have copied it as in my answer and it has worked well "so far". should i be worried now?
Maik Lowrey avatar
in flag
@bac0n i means interactive. yes it would be good. thanks. but i dont found a explanation for the -T option.
bac0n avatar
cn flag
The target doesn't have to exist before copying is probably the main difference, next if *source* is a file and target a directory you get an error instead of the file being copied into the directory (should be cp -aT or -rT for recursive).
Nate T avatar
it flag
@Maik It's Bash, there are 10 separate ways to do anything. I was just working on a script yest. that uses both `cp -a` and a redirect from find within about 10-15 lines of each other. I would recommend looking further into all versions and learning the quirks and caveats of each. For example a set of commands may be exactly alike w/ params, but only one reads from stdin w/ none. This may not matter 90% of the time, but on that 10th time, it pays to know the diff. Figured I would point that out as it is what I was originally aiming at but I think I missed the mark before.
terdon avatar
cn flag
Note that this will also copy subdirectories, while your question is asking about files only. Not sure if this is intended or not.
Maik Lowrey avatar
in flag
@terdon files and folders. i will updated the title from my question.
Score:-1
it flag

You just need a wildcard at the end of the command like so:

cp -r application/* ./
Maik Lowrey avatar
in flag
Thank you for answearing. But it doesnt work. `cp: missing destination file operand after 'sub/*'`
Maik Lowrey avatar
in flag
Yes that would work too. But i used `cp -a app/. ./` before and it works perfectly. I was unsettled by bac0n's comment. Do you happen to know what the `-T` option means?
Nate T avatar
it flag
Glad you got it. @MaikLowrey
Maik Lowrey avatar
in flag
Thank you for your time and effort!
terdon avatar
cn flag
@MaikLowrey the original answer had quotes around the `"application/*"` which cause the glob (wildcard) not to be expanded. I fixed that, but the second command is completely wrong, I'm afraid. I _think_ what Nate meant was something like `find ./application/ -type f -exec cp {} ./ +`. The command in this answer will instead look for _directories_ in the directory `/application/` and then will append the list of directory names to a file called `/` which will fail since `/` is a directory.
Maik Lowrey avatar
in flag
Thank @terdon! You are right. I took `cp -a app/. ./` It is best to use the command with the -i option (thank to bac0n) to avoid overwriting files with the same name. In my case, "only" the .gitigniore was overwritten. Commands with recursion should be used carefully.
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.