0

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.

  • @NateT I would remove applcaition but i will copie before removing all the contents to the root. Yes. – Maik Lowrey Nov 19 '21 at 08:49
  • 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 *. – terdon Nov 19 '21 at 19:07

2 Answers2

0

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.

  • 1
    think you meant to do cp -T app/ . (probably wise to append -i) –  Nov 19 '21 at 08:31
  • @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 Nov 19 '21 at 08:39
  • @bac0n i means interactive. yes it would be good. thanks. but i dont found a explanation for the -T option. – Maik Lowrey Nov 19 '21 at 08:47
  • 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). –  Nov 19 '21 at 09:20
  • @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. – Nate T Nov 19 '21 at 11:24
  • 1
    Note that this will also copy subdirectories, while your question is asking about files only. Not sure if this is intended or not. – terdon Nov 19 '21 at 19:12
  • @terdon files and folders. i will updated the title from my question. – Maik Lowrey Nov 20 '21 at 08:10
-1

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

cp -r application/* ./
Nate T
  • 1,524
  • Thank you for answearing. But it doesnt work. cp: missing destination file operand after 'sub/*' – Maik Lowrey Nov 19 '21 at 08:57
  • 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? – Maik Lowrey Nov 19 '21 at 09:04
  • 1
    Glad you got it. @MaikLowrey – Nate T Nov 19 '21 at 09:06
  • Thank you for your time and effort! – Maik Lowrey Nov 19 '21 at 09:21
  • 1
    @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. – terdon Nov 19 '21 at 19:11
  • 1
    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. – Maik Lowrey Nov 20 '21 at 08:08