Web Masters Site All For Web Masters

1Feb/090

How To Copy Files In Linux

To copy files, you use the cp command. The following will copy file to file2. Note that if file2 doesn't exist, it'll be created, but if it exists, it'll be overwritten:
$ cp file file2

There aren't any undo commands in the Linux CLI, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i option ("interactive") with cp. The following does the same as the above, but if file2 exists, you'll be prompted before overwriting:

$ cp -i file file2
cp: overwrite `file2'? n
$

So it's a good idea to use the -i option whenever you're dealing with important files you don't want to lose!

If you want to copy file into directory dir1:
$ cp file dir1

The following would do the same as the above, copy file into dir1, but under a different name:
$ cp file dir1/file2

You can also copy multiple files into one directory with a single command:
$ cp file1 file2 file3 dir1

If you want to copy all files to dir2 you can use this command:

$ cp *.* dir2

This command will copy only .txt files to dir2:

$ cp *.txt dir2

Note that if the last argument isn't a directory name, you'll get an error message complaining about it.

cp

Copy one or more files to another location Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Syntax
      cp [options]... Source Dest
      cp [options]... Source... Directory

Key

  -a, --archive                same as -dpR

  -b, --backup                 make backup before removal

  -d, --no-dereference         preserve links

  -f, --force                  remove existing destinations, never prompt

  -i, --interactive            prompt before overwrite

  -l, --link                   link files instead of copying

  -p, --preserve               preserve file attributes if possible

  -P, --parents                append source path to DIRECTORY

  -r                           copy recursively, non-directories as files

      --sparse=WHEN            control creation of sparse files

  -R, --recursive              copy directories recursively

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

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

  -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

  -V, --version-control=WORD   override the usual version control

  -x, --one-file-system        stay on this file system
      --help                   display this help and exit
      --version                output version information and exit.
Filed under: Linux SA Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

 

No trackbacks yet.