SlideShare a Scribd company logo
2
Most read
3
Most read
CP COMMAND
SYED SADATH ULLAH
M.Tech;
Linux – Administrator
Topics Listed:
1) Cp command syntax with example
2) How to copy a file from current directory to subdirectory
3) How to copy a file from current directory to an absolute destination path
4) How to copy a file from absolute source path to an absolute path
5) How to copy multiple files
6) How to copy file with verbose output
7) How to copy files interactively
8) How to copy files associated with a certain file extension
9) How to copy Directory Recursively
10) Linux man cp
1) Cp command syntax with example
The basic command structure of cp command is as follows:
Command: $ cp [options] source destination
Let’s look at some examples of using cp command to copy files and folders in Linux.
Command: $ ls
a.txt
Command: $ cp a.txt b.txt
Command: $ ls
a.txt b.txt
2) How to copy a file from current directory to subdirectory
To copy a file from the present working directory to a subdirectory use the syntax below:
Command: $ cp file directory
Example
For example, to copy a file file1.txt to a sub directory called data execute the cp command as
follows. Later use the ls command to verify the existence of the file in the directory.
Command: $ cp file1.txt data
Sample output
3) How to copy a file from current directory to an absolute
destination path
To copy a file from the present working directory to a different destination directory or absolute
path, use the syntax below.
Command: $ cp file /path/to/directory
Example
Command: $ cp file1.txt /home/james/
Output:
4) How to copy a file from absolute source path to an
absolute path
To copy a file(s) from a different file path to another absolute path use the syntax.
Command: $ cp /path/to/source/file /path/to/destination/directory
Example
Command: $ cp /home/james/file1.txt /opt/data
Sample output
5) How to copy multiple files
Linux cp command also allows you to copy more than one file at a go using the syntax below.
Command: $ cp file1 file2 .... /path/to/directory
Example
Command: $ cp file1.txt file2.txt file3.txt /home/james/
Sample
output
6) How to copy file with verbose output
To display verbose output, use the -v flag as shown.
Command: cp -v file1.txt file2.txt /home/james
Sample output
7) How to copy files interactively
To copy files interactively i.e to provoke a prompt from the Linux shell, use the -i flag as shown.
Command: $ cp -i file1.txt /home/james
Sample output
8) How to copy files associated with a certain file extension
If you wish to copy a number of files with the same file extension, say .txt or .pdf follow the
example below.
Command: $ cp *.txt /home/james
9) How to copy Directory Recursively
If you want to copy a directory alongside all its contents, use the -R attribute followed by the
directory name as shown.
Command: $ cp -R data /home/james
Sample output
To copy the contents of a directory but not the directory itself, run the command as follows.
Command: $ cp -R data/* /home/james
Sample output
10) Linux man cp
We can run man cp command to look at the documentation of cp command. This is useful to
find out the right options to use with the cp command.
Command: man cp
Sample output
11) Usinng cp –f (Force)
-f(force): If the system is unable to open destination file for writing operation because the user
doesn’t have writing permission for this file then by using -f option with cp command, destination
file is deleted first and then copying of content is done from source to destination file.
Command: $ ls -l b.txt
-r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt
User, group and others doesn't have writing permission.
Without -f option, command not executed
Command: $ cp a.txt b.txt
cp: cannot create regular file 'b.txt': Permission denied
With -f option, command executed successfully
Command: $ cp -f a.txt b.txt
12) Using cp –b (backup)
-b(backup): With this option cp command creates the backup of the destination file in the same
folder with the different name and in different format.
Command: $ ls
a.txt b.txt
Command: $ cp -b a.txt b.txt
Command: $ ls
a.txt b.txt b.txt~
13) Using cp * wildcard:
The star wildcard represents anything i.e. all files and directories. Suppose we have many text
document in a directory and wants to copy it another directory, it takes lots of time if we copy
files 1 by 1 or command becomes too long if specify all these file names as the argument, but
by using * wildcard it becomes simple.
Note: Initially Folder1 is empty
Command: $ ls
a.txt b.txt c.txt d.txt e.txt Folder1
Command: $ cp *.txt Folder1
Command: $ ls Folder1
a.txt b.txt c.txt d.txt e.txt
14) Using cp -p
. -p(preserve): With -p option cp preserves the following characteristics of each source file in the
corresponding destination file: the time of the last data modification and the time of the last
access, the ownership (only if it has permissions to do this), and the file permission-bits.
Note: For the preservation of characteristics you must be the root user of the system, otherwise
characteristics changes.
Command: $ ls -l a.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt
Command: $ cp -p a.txt c.txt
Command: $ ls -l c.txt
-rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt
Note: Here, we can see above both a.txt and c.txt (created by copying) have same
characteristics.
Thank U

More Related Content

PPT
PPT on Basic HTML Tags
PDF
Compilation v. interpretation
PPT
Chapter13 designing forms and reports
PPTX
Data Flow Diagrams
PPT
Data Flow Diagram
PDF
Html table tags
PPT
Web Servers (ppt)
PPTX
Web Building Blocks
PPT on Basic HTML Tags
Compilation v. interpretation
Chapter13 designing forms and reports
Data Flow Diagrams
Data Flow Diagram
Html table tags
Web Servers (ppt)
Web Building Blocks

What's hot (20)

PPTX
Introduction of c programming unit-ii ppt
PDF
Vi Cheat Sheet v 1 00
PDF
Web Development Course: PHP lecture 1
PPTX
PowerPoint on Powerpoint
PPTX
Xml namespace
DOC
COMPUTER GRAPHICS LAB MANUAL
PPTX
Loops in java script
PPTX
Data base security & integrity
PPTX
PDF
1 introduction to problem solving and programming
PPTX
Introduction to php
PPT
Form validation client side
PDF
SE_Lec 07_UML CLASS DIAGRAM
PPTX
Algorithm Development
PDF
Intro to HTML, CSS & JS - Internship Presentation Week-3
PPTX
Software programming and development
PPTX
Excel Basics
DOCX
Practical file on web technology(html)
PPTX
Bootstrap PPT Part - 2
PPT
Introduction to CSS
Introduction of c programming unit-ii ppt
Vi Cheat Sheet v 1 00
Web Development Course: PHP lecture 1
PowerPoint on Powerpoint
Xml namespace
COMPUTER GRAPHICS LAB MANUAL
Loops in java script
Data base security & integrity
1 introduction to problem solving and programming
Introduction to php
Form validation client side
SE_Lec 07_UML CLASS DIAGRAM
Algorithm Development
Intro to HTML, CSS & JS - Internship Presentation Week-3
Software programming and development
Excel Basics
Practical file on web technology(html)
Bootstrap PPT Part - 2
Introduction to CSS
Ad

Similar to Cp command in Linux (20)

PDF
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
DOCX
Chapter 4 Linux Basic Commands
PDF
Linux_Commands.pdf
DOC
Unix Basics For Testers
PPTX
Managing files chapter 7
PPTX
Managing files chapter 7
PDF
Basic unix commands
PDF
LinuxCommands (1).pdf
PPTX
Linux Command.pptx
PDF
Course 102: Lecture 3: Basic Concepts And Commands
PDF
Linux basic commands with examples
PPTX
Basic Linux Commands and implementation with Examples
PPTX
Basic Linux Commands with syntax and functions
PPTX
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
PPT
Examples -partII
PPTX
File Commands - R.D.Sivakumar
PDF
When you use the DOS COPY command you can assume many defaults. In f.pdf
PDF
Linux cheat sheet
PPTX
various shell commands in unix operating system.pptx
PPTX
Linux System commands Essentialsand Basics.pptx
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
Chapter 4 Linux Basic Commands
Linux_Commands.pdf
Unix Basics For Testers
Managing files chapter 7
Managing files chapter 7
Basic unix commands
LinuxCommands (1).pdf
Linux Command.pptx
Course 102: Lecture 3: Basic Concepts And Commands
Linux basic commands with examples
Basic Linux Commands and implementation with Examples
Basic Linux Commands with syntax and functions
AARAV NAYAN OPERATING SYSTEM LABORATORY PCA
Examples -partII
File Commands - R.D.Sivakumar
When you use the DOS COPY command you can assume many defaults. In f.pdf
Linux cheat sheet
various shell commands in unix operating system.pptx
Linux System commands Essentialsand Basics.pptx
Ad

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPT
Introduction Database Management System for Course Database
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
L1 - Introduction to python Backend.pptx
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Softaken Excel to vCard Converter Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction Database Management System for Course Database
CHAPTER 2 - PM Management and IT Context
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms II-SECS-1021-03
Understanding Forklifts - TECH EHS Solution
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Reimagine Home Health with the Power of Agentic AI​
Design an Analysis of Algorithms I-SECS-1021-03
Digital Systems & Binary Numbers (comprehensive )
L1 - Introduction to python Backend.pptx
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
Softaken Excel to vCard Converter Software.pdf

Cp command in Linux

  • 1. CP COMMAND SYED SADATH ULLAH M.Tech; Linux – Administrator Topics Listed: 1) Cp command syntax with example 2) How to copy a file from current directory to subdirectory 3) How to copy a file from current directory to an absolute destination path 4) How to copy a file from absolute source path to an absolute path 5) How to copy multiple files 6) How to copy file with verbose output 7) How to copy files interactively 8) How to copy files associated with a certain file extension 9) How to copy Directory Recursively 10) Linux man cp
  • 2. 1) Cp command syntax with example The basic command structure of cp command is as follows: Command: $ cp [options] source destination Let’s look at some examples of using cp command to copy files and folders in Linux. Command: $ ls a.txt Command: $ cp a.txt b.txt Command: $ ls a.txt b.txt 2) How to copy a file from current directory to subdirectory To copy a file from the present working directory to a subdirectory use the syntax below: Command: $ cp file directory Example For example, to copy a file file1.txt to a sub directory called data execute the cp command as follows. Later use the ls command to verify the existence of the file in the directory. Command: $ cp file1.txt data Sample output
  • 3. 3) How to copy a file from current directory to an absolute destination path To copy a file from the present working directory to a different destination directory or absolute path, use the syntax below. Command: $ cp file /path/to/directory Example Command: $ cp file1.txt /home/james/ Output: 4) How to copy a file from absolute source path to an absolute path To copy a file(s) from a different file path to another absolute path use the syntax. Command: $ cp /path/to/source/file /path/to/destination/directory Example Command: $ cp /home/james/file1.txt /opt/data Sample output
  • 4. 5) How to copy multiple files Linux cp command also allows you to copy more than one file at a go using the syntax below. Command: $ cp file1 file2 .... /path/to/directory Example Command: $ cp file1.txt file2.txt file3.txt /home/james/ Sample output 6) How to copy file with verbose output To display verbose output, use the -v flag as shown. Command: cp -v file1.txt file2.txt /home/james Sample output 7) How to copy files interactively To copy files interactively i.e to provoke a prompt from the Linux shell, use the -i flag as shown.
  • 5. Command: $ cp -i file1.txt /home/james Sample output 8) How to copy files associated with a certain file extension If you wish to copy a number of files with the same file extension, say .txt or .pdf follow the example below. Command: $ cp *.txt /home/james 9) How to copy Directory Recursively If you want to copy a directory alongside all its contents, use the -R attribute followed by the directory name as shown. Command: $ cp -R data /home/james Sample output To copy the contents of a directory but not the directory itself, run the command as follows. Command: $ cp -R data/* /home/james Sample output
  • 6. 10) Linux man cp We can run man cp command to look at the documentation of cp command. This is useful to find out the right options to use with the cp command. Command: man cp Sample output
  • 7. 11) Usinng cp –f (Force) -f(force): If the system is unable to open destination file for writing operation because the user doesn’t have writing permission for this file then by using -f option with cp command, destination file is deleted first and then copying of content is done from source to destination file. Command: $ ls -l b.txt -r-xr-xr-x+ 1 User User 3 Nov 24 08:45 b.txt User, group and others doesn't have writing permission. Without -f option, command not executed Command: $ cp a.txt b.txt cp: cannot create regular file 'b.txt': Permission denied With -f option, command executed successfully Command: $ cp -f a.txt b.txt 12) Using cp –b (backup) -b(backup): With this option cp command creates the backup of the destination file in the same folder with the different name and in different format. Command: $ ls a.txt b.txt Command: $ cp -b a.txt b.txt Command: $ ls a.txt b.txt b.txt~
  • 8. 13) Using cp * wildcard: The star wildcard represents anything i.e. all files and directories. Suppose we have many text document in a directory and wants to copy it another directory, it takes lots of time if we copy files 1 by 1 or command becomes too long if specify all these file names as the argument, but by using * wildcard it becomes simple. Note: Initially Folder1 is empty Command: $ ls a.txt b.txt c.txt d.txt e.txt Folder1 Command: $ cp *.txt Folder1 Command: $ ls Folder1 a.txt b.txt c.txt d.txt e.txt 14) Using cp -p . -p(preserve): With -p option cp preserves the following characteristics of each source file in the corresponding destination file: the time of the last data modification and the time of the last access, the ownership (only if it has permissions to do this), and the file permission-bits. Note: For the preservation of characteristics you must be the root user of the system, otherwise characteristics changes. Command: $ ls -l a.txt -rwxr-xr-x+ 1 User User 3 Nov 24 08:13 a.txt Command: $ cp -p a.txt c.txt Command: $ ls -l c.txt -rwxr-xr-x+ 1 User User 3 Nov 24 08:13 c.txt Note: Here, we can see above both a.txt and c.txt (created by copying) have same characteristics. Thank U