Error while copying (cp) a directory in Linux “omitting directory”

Configurare noua (How To)

Situatie

When we are trying to copy a directory to other location, we get below error:

[opc@coslnxhost001 ~]$ mkdir test_dir1
[opc@coslnxhost001 ~]$ mkdir test_dir2
[opc@coslnxhost001 ~]$ touch test_dir1/test_file1.txt
[opc@coslnxhost001 ~]$ touch test_dir1/test_file2.txt

[opc@coslnxhost001 ~]$ cp test_dir1 test_dir2
cp: omitting directory ‘test_dir1’
[opc@coslnxhost001 ~]$

Solutie

Pasi de urmat

The error above is a common mistake while copying a directory to other locations without using the recursive copy option in the ‘cp’ command.

In order to avoid this error use the “-r” or the “-a” option in the copy command. From the man page of ‘cp’ command:

# From Man page:
-R, -r, --recursive
copy directories recursively

-a, --archive
same as -dR --preserve=all

-v, --verbose
explain what is being done

For example:

cp -rv test_dir1 test_dir2

or

cp -Rv test_dir1 test_dir2

or

cp -av test_dir1 test_dir2

(*) added -v option to be more visible what is being done

By default, if not using explicitly one of the above options “-r”/ “-R” or “-a”, the command will try to only copy the files and not the directories from the source.

Output sample :

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

[opc@coslnxhost001 ~]$ mkdir test_dir1
[opc@coslnxhost001 ~]$ mkdir test_dir2
[opc@coslnxhost001 ~]$ cp test_dir1 test_dir2
cp: omitting directory ‘test_dir1’
[opc@coslnxhost001 ~]$

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

[opc@coslnxhost001 ~]$ cp -rv test_dir1 test_dir2
‘test_dir1’ -> ‘test_dir2/test_dir1’
‘test_dir1/test_file1.txt’ -> ‘test_dir2/test_dir1/test_file1.txt’
‘test_dir1/test_file2.txt’ -> ‘test_dir2/test_dir1/test_file2.txt’
[opc@coslnxhost001 ~]$

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

[opc@coslnxhost001 ~]$ cp -Rv test_dir1 test_dir2
‘test_dir1’ -> ‘test_dir2/test_dir1’
‘test_dir1/test_file1.txt’ -> ‘test_dir2/test_dir1/test_file1.txt’
‘test_dir1/test_file2.txt’ -> ‘test_dir2/test_dir1/test_file2.txt’
[opc@coslnxhost001 ~]$

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

[opc@coslnxhost001 ~]$ cp -av test_dir1 test_dir2
‘test_dir1’ -> ‘test_dir2/test_dir1’
‘test_dir1/test_file1.txt’ -> ‘test_dir2/test_dir1/test_file1.txt’
‘test_dir1/test_file2.txt’ -> ‘test_dir2/test_dir1/test_file2.txt’
[opc@coslnxhost001 ~]$

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

Tip solutie

Permanent

Voteaza

(30 din 57 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?