Saturday, 27 June 2015

How to copy files and directories in Linux operating system

Cp command in Linux/Unix

cp is a Linux shell command to copy files and directories.

cp command syntax

Copy from source to dest
$ cp [optionssource dest

cp command options

cp command main options:
optiondescription
cp -aarchive files
cp -fforce copy by removing the destination file if needed
cp -iinteractive - ask before overwrite
cp -llink files instead of copy
cp -Lfollow symbolic links
cp -nno file overwrite
cp -Rrecursive copy (including hidden files)
cp -uupdate - copy when source is newer than dest
cp -vverbose - print informative messages

cp command examples

Copy single file main.c to destination directory bak:
$ cp main.c bak

Copy 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/ :
$ cp main.c def.h /home/usr/rapid/

Copy all C files in current directory to subdirectory bak :
$ cp *.c bak

Copy directory src to absolute path directory /home/usr/rapid/ :
$ cp src /home/usr/rapid/

Copy all files and directories in dev recursively to subdirectory bak:
$ cp -R dev bak

Force file copy:
$ cp -f test.c bak

Interactive prompt before file overwrite:
$ cp -i test.c bak
cp: overwrite 'bak/test.c'? y

Update all files in current directory - copy only newer files to destination directory bak:
$ cp -u * bak

cp code generator

Select cp options and press the Generate Code button:

Options
 Force copy (-f)
 Interactive - ask before overwrite (-i)
 Link files (-l)
 Follow symbolic links (-L)
 No overwrite (-n)
 Recursive directory tree copy (-R)
 Update newer files (-u)
 Verbose messages (-v)
Files / folders
Source files / folders: 
Destination folder / file:

For more information follow the below link
http://www.rapidtables.com/code/linux/cp.htm#examples

No comments:

Post a Comment