Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux cp command to copy a folder to current directory

Tags:

cp

I am wondering if there is an identical command for copying a folder to current directory like it did using the old MS-DOS. Let's say my current directory location is:

/var/www/

I have folders and files at:

/home/hope/subfolder/docs/
/home/hope/subfolder/images/
/home/hope/subfolder/.config
/home/hope/subfolder/readme.txt

I know that the following command:

cp -rT /home/hope/subfolder .

will copy all the files (even dot hidden files) and folders within the "subfolder" folder to the current directory, so the result will be:

/var/www/docs/
/var/www/images/
/var/www/.config
/var/www/readme.txt

Looks like the command to that to copy the source folder to the current location is:

cp -rT /home/hope/subfolder ./subfolder

although this is fine, I find it that sometimes I will make mistakes for complicated folder names for the destination, so is there a way to use a command like:

cp -rT /home/hope/subfolder .

or even like this

cp -rT /home/hope/subfolder /var/www/.

to have the following result:

/var/www/subfolder/docs/
/var/www/subfolder/images/
/var/www/subfolder/.config
/var/www/subfolder/readme.txt

Thank you.

like image 832
user702300 Avatar asked Apr 14 '15 12:04

user702300


People also ask

How do you copy a file to an existing directory in Linux?

So, if you want to copy all files including hidden files from a directory into an existing directory, you can: cd [source dir] , cp . [path to destination dir, with no trailing slash] .

Can cp command copy directories?

With cp command, you can copy a directory and an entire subdirectory with its content and everything beneath it. cp and rsync are one of the most popular commands for copying files and directory.

How do I copy a folder to another directory in terminal?

In the Terminal app on your Mac, use the cp command to make a copy of a file. The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.


Video Answer


2 Answers

Just omit the -T parameter, as that's what prevents the command from working properly:

cp -r /home/hope/subfolder .

The -T parameter treats the target argument as a file, so no copying will be performed at all if that is actually a directory.

A friendly reminder: virtually all Unix commands have a --help command line argument that is worth trying out in case of a trouble :)

like image 192
Vadim Landa Avatar answered Oct 16 '22 20:10

Vadim Landa


For me the main barrier was the /home part. I needed to copy files from a folder in my home that started with the letter 'a' to my current folder, which was not home. So I used:

cp home/tmp/a* ./

the first line worked for me. While I was trying commands like:

cp ~/home/tmp/a* ./

but this didn't work.

like image 31
Aadn Avatar answered Oct 16 '22 18:10

Aadn