SlideShare a Scribd company logo
1 of 8
Download to read offline
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

What's hot

Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script Amit Ghosh
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 
LINUX:Control statements in shell programming
LINUX:Control statements in shell programmingLINUX:Control statements in shell programming
LINUX:Control statements in shell programmingbhatvijetha
 
Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Noé Fernández-Pozo
 
Swap Administration in linux platform
Swap Administration in linux platformSwap Administration in linux platform
Swap Administration in linux platformashutosh123gupta
 
Transaction management DBMS
Transaction  management DBMSTransaction  management DBMS
Transaction management DBMSMegha Patel
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programmingSrinivas Narasegouda
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell scriptBhavesh Padharia
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraintsmadhav bansal
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...Edureka!
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
Shell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaShell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaEdureka!
 

What's hot (20)

Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
 
Shell & Shell Script
Shell & Shell Script Shell & Shell Script
Shell & Shell Script
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 
Vi editor in linux
Vi editor in linuxVi editor in linux
Vi editor in linux
 
LINUX:Control statements in shell programming
LINUX:Control statements in shell programmingLINUX:Control statements in shell programming
LINUX:Control statements in shell programming
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Triggers and active database
Triggers and active databaseTriggers and active database
Triggers and active database
 
Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014Unix Command-Line Cheat Sheet BTI2014
Unix Command-Line Cheat Sheet BTI2014
 
Object oriented programming in python
Object oriented programming in pythonObject oriented programming in python
Object oriented programming in python
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Swap Administration in linux platform
Swap Administration in linux platformSwap Administration in linux platform
Swap Administration in linux platform
 
Transaction management DBMS
Transaction  management DBMSTransaction  management DBMS
Transaction management DBMS
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Operating system lab manual
Operating system lab manualOperating system lab manual
Operating system lab manual
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...What is Interface in Java | How to implement Multiple Inheritance Using Inter...
What is Interface in Java | How to implement Multiple Inheritance Using Inter...
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Shell Scripting Tutorial | Edureka
Shell Scripting Tutorial | EdurekaShell Scripting Tutorial | Edureka
Shell Scripting Tutorial | Edureka
 

Similar to Cp command in Linux

Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...Engr. Md. Jamal Uddin Rayhan
 
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsChapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsShankar Mahure
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualKuntal Bhowmick
 
Anandha ganesh linux1.ppt
Anandha ganesh linux1.pptAnandha ganesh linux1.ppt
Anandha ganesh linux1.pptanandha ganesh
 
Lab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxLab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxCiceer Ghimirey
 
Lab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxLab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxCiceer Ghimirey
 
Linux_Commands.pdf
Linux_Commands.pdfLinux_Commands.pdf
Linux_Commands.pdfMarsMox
 
Introduction to the linux command line.pdf
Introduction to the linux command line.pdfIntroduction to the linux command line.pdf
Introduction to the linux command line.pdfCesleySCruz
 
Using the command line on macOS
Using the command line on macOSUsing the command line on macOS
Using the command line on macOSAdamFallon4
 

Similar to Cp command in Linux (20)

Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
Linux Command For Beginners 6 - copy commands for linux | BUET Pattern Job Pr...
 
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic CommandsChapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
 
Unix Basics For Testers
Unix Basics For TestersUnix Basics For Testers
Unix Basics For Testers
 
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformaticsBITS: Introduction to Linux - Text manipulation tools for bioinformatics
BITS: Introduction to Linux - Text manipulation tools for bioinformatics
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manualBasic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
 
Examples -partII
Examples -partIIExamples -partII
Examples -partII
 
Anandha ganesh linux1.ppt
Anandha ganesh linux1.pptAnandha ganesh linux1.ppt
Anandha ganesh linux1.ppt
 
Lab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxLab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptx
 
Lab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptxLab 3 -Linux Files, Directories and Basic Commands.pptx
Lab 3 -Linux Files, Directories and Basic Commands.pptx
 
Linux basics
Linux basicsLinux basics
Linux basics
 
Linux_Commands.pdf
Linux_Commands.pdfLinux_Commands.pdf
Linux_Commands.pdf
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
40 basic linux command
40 basic linux command40 basic linux command
40 basic linux command
 
40 basic linux command
40 basic linux command40 basic linux command
40 basic linux command
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
Introduction to the linux command line.pdf
Introduction to the linux command line.pdfIntroduction to the linux command line.pdf
Introduction to the linux command line.pdf
 
Linux ppt
Linux pptLinux ppt
Linux ppt
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
50 most frequently used unix
50 most frequently used unix50 most frequently used unix
50 most frequently used unix
 
Using the command line on macOS
Using the command line on macOSUsing the command line on macOS
Using the command line on macOS
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?Watsoo Telematics
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?What are the features of Vehicle Tracking System?
What are the features of Vehicle Tracking System?
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 

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