Home > other >  How do I copy three files to a specific directory structure dir1/dir2/dir3
How do I copy three files to a specific directory structure dir1/dir2/dir3

Time:11-20

I am finding it difficult to copy three files to a specific directory structure such as dir1/dir2/dir3 respectively.

I tried using cp and Accessing the directory directory through command line (used :- cp .. /) this didn't work too. The command runs successful but the file don't copy in those specific directory

CodePudding user response:

When you just add the directories to a single cp command like cp file a b c then cp thinks you want to copy file, a, and b into c.

So you need to run cp multiple times, once for each destination directory, for example in a loop:

robert@saaz:~$ mkdir a b c
robert@saaz:~$ touch foo
robert@saaz:~$ for targetDir in a b c; do cp foo $targetDir; done
robert@saaz:~$ tree
.
├── a
│   └── foo
├── b
│   └── foo
├── c
│   └── foo
└── foo

3 directories, 4 files

Maybe I misread your question. If you want to copy a file into a directory in a directory, just specify that:

robert@saaz:~$ mkdir -p 1/2/3
robert@saaz:~$ cp foo 1/2/3/
robert@saaz:~$ tree 1
1
└── 2
    └── 3
        └── foo

2 directories, 1 file

CodePudding user response:

If you need to copy directories, you'll need to use -r for recursive.

so for example:

cp -r dir1/ target_dir/

This will copy dir1/ with it's contents to target_dir/

  • Related