LinuxMoz

Linux Stuff && Coffee

Cp Linux Command & Usage Examples

The cp Linux command is used to copy files from one location to another, with the recursive options it can also copy directories.

Useful articles for tasks using the cp command

Linux cp command usage

Usage instructions for the cp command, used to copy files on Linux / Unix systems.

1
cp [OPTION]... [-T] SOURCE DEST

cp command examples

Copy all files from a dir to enother dir (/home/jesus/ to /home/stan):

1
cp /home/jesus/* /home/stan/

Copy all files & sub dir’s from a dir to enother dir (/home/jesus/ to /home/stan):

1
cp -r /home/jesus/* /home/stan/

Copy files and keep the file permissions the same:

1
cp -a /home/jesus/* /home/stan/

Copy all files ending in .sh to /tmp:

1
cp *.sh /tmp

cp command options

Options for the cp Linux command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-a, --archive
              same as -dR --preserve=all

       --attributes-only
              don't copy the file data, just the attributes

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       --copy-contents
              copy contents of special files when recursive

       -d     same as --no-dereference --preserve=links

       -f, --force
              if an existing destination file cannot be opened, remove it  and
              try again (redundant if the -n option is used)

       -i, --interactive
              prompt before overwrite (overrides a previous -n option)

       -H     follow command-line symbolic links in SOURCE

       -l, --link
              hard link files instead of copying

       -L, --dereference
              always follow symbolic links in SOURCE

       -n, --no-clobber
              do  not  overwrite  an  existing  file  (overrides a previous -i
              option)

       -P, --no-dereference
              never follow symbolic links in SOURCE

       -p     same as --preserve=mode,ownership,timestamps

       --preserve[=ATTR_LIST]
       --preserve[=ATTR_LIST]
              preserve the specified attributes (default: mode,ownership,time‐
              stamps),  if  possible  additional  attributes:  context, links,
              xattr, all

       -c     same as --preserve=context

       --no-preserve=ATTR_LIST
              don't preserve the specified attributes

       --parents
              use full source file name under DIRECTORY

       -R, -r, --recursive
              copy directories recursively

       --reflink[=WHEN]
              control clone/CoW copies. See below

       --remove-destination
              remove each existing destination file before attempting to  open
              it (contrast with --force)

       --sparse=WHEN
              control creation of sparse files. See below

       --strip-trailing-slashes
              remove any trailing slashes from each SOURCE argument

       -s, --symbolic-link
              make symbolic links instead of copying

       -S, --suffix=SUFFIX
              override the usual backup suffix

       -t, --target-directory=DIRECTORY
              copy all SOURCE arguments into DIRECTORY

       -T, --no-target-directory
              treat DEST as a normal file

       -u, --update
              copy  only  when  the  SOURCE file is newer than the destination
              file or when the destination file is missing

       -v, --verbose
              explain what is being done

       -x, --one-file-system
              stay on this file system

       -Z, --context=CONTEXT
              set security context of copy to CONTEXT

       --help display this help and exit

       --version
              output version information and exit

Comments