Technoracle
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    TechnoracleTechnoracle
    • Home
    • AWS
      • EC2
      • Lightsail
      • RDS
      • S3
    • Ubuntu
      • Ubuntu 20.04
      • Ubuntu 22.04
    • AI Tools
    • Web Hosting
    • WordPress
    • Tech News
    Technoracle
    Home - How to Copy Files and Directories in Linux
    Uncategorized

    How to Copy Files and Directories in Linux

    Rajesh ShivamBy Rajesh ShivamMarch 28, 2022Updated:March 28, 2022No Comments4 Mins Read
    Copy Files and Directories in Linux
    Copy Files and Directories in Linux

    In Linux based operating system such as Ubuntu, Devian, Fedora , etc. All shares common command to copy files and directories using command line. If you are a Linux user, I am sure everyday you use to copy files and directories. To perform file copy and move, you must have read permission of source file and write permission for destination directory. In this article, I am going to explain how to copy files and directories in Linux.

    So let’s get started.

    Page Contents

    • What is cp command
      • How to Copy Files and Directories in Linux
        • Copy a file within the same directory
        • Copy file to the directory
        • Copy multiple files in Linux
        • Copy directories using cp command
        • Use rsync command to copy files and directories
    • Conclusion

    What is cp command

    ‘cp’ is the most commonly used command to copy a file from one directory to another in Unix based operating systems such as Ubuntu, Fedora, etc. ‘cp’ creates an exact copy of the file on a disk with a different name.

    Syntax of commonly used cp command syntax

    $ cp [option] [source] [destination]

    Explanation of the options use cp command syntax

    OptionDetails
    -iInteractive copying, it will warns you before copying.
    -bThis option will create a backup of the file in the destination directory with different name and format.
    -fforce the copy by deleting an existing file first if the user has no write permission
    -r or -RCopies all files and sub directories in a directory recursively.
    -v It shows the progress of copying.
    -pIt keeps the file attributes same, such as creation date and file permissions.
    -uOnly copy if source file is newer than destination file

    How to Copy Files and Directories in Linux

    Let’s discuss the command with all the options one by one. To explain, I am taking two files

    Copy a file within the same directory

    For, example to copying a file name sample.txt to the file name sample_new.txt in the same directory, you would run following command.

    $ cp sample.txt sample_new.txt

    Above command will copy the sample.txt file to sample_new.txt without any error if the sample_new.txt is not exist on the directory.

    -i option usage in cp command

    If the destination file name as mentioned above already exist, it will override the file sample_new.txt without any error, unless you use flag option -i

    $ cp -i sample.txt sample_new.txt

    Output will be: cp: overwrite ‘sample_new.txt’?

    -p option usage in cp command

    When a user perform copy operation by cp command, the new file will be owned by the user who perform copy operation. To keep the attribute same as before use-p option.

    $ cp -p sample.txt sample_new.txt

    -v option usage in cp command

    When you use -v option, it will print the progress.

    $ cp -v sample.txt sample_new.txt

    Output will be: ‘sample.txt’ -> ‘sample_new.txt’

    Copy file to the directory

    To copy a file to a directory, always specify the absolute or the relative path to the directory. Let’s take an example file name file.txt and directory name New_Files. Now we will copy file.txt to New_Files.

    $ cp file.txt New_Files/

    Above command will copy the file with same name in the directory. If you want to copy the file with different name, command will be

    $ cp file.txt New_Files/rajesh.txt

    Copy multiple files in Linux

    If you want to copy multiple files to a directory, you have to specify the files name one by one followed by directory name at last argument.

    $ cp a.txt b.txt c.txt multiple_files

    You can also copy by pattern matching, suppose you have some file with .txt extension and some files have other extension. Now, your mission is to copy only .txt extension files to the directory, in that case you have use following command.

    $ cp *.txt multiple_files

    Copy directories using cp command

    Copy all the files and directories to another directories using following command.

    $ cp -R multiple_file new_directory

    Above command will copy all the files and directories from multiple_file to the new_directory.

    Use rsync command to copy files and directories

    Rsync is a Linux-based tool that can be used to sync files between remote and local servers. It is great for complex file syncs and for transferring a large number of files.

    The basic command of rsync is

    rsync [option] [origin] [destination]

    Read More: How to Upload File to AWS EC2 Instance

    Conclusion

    In this article, I explained how to copy files and directories in Linux. If you have any doubts feel free to contact me, I will definitely try to help you. Also, you can join our elite Facebook group to get direct help from me. If you like this tutorial, please share this article on your social media handle.

    Copy Files and Directories in Linux linux file copy ubuntu tips
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit
    Previous ArticleHow to Setup Ubuntu Firewall Easily
    Next Article How to Check Ubuntu Version from Command Line Easily
    Rajesh Shivam
    • LinkedIn

    Rajesh Shivam is a seasoned professional with expertise in AWS and Linux System Administration. With over 10 years of freelancing experience, he brings a wealth of knowledge to the table. Rajesh has a strong passion for technology and stays connected with the latest happenings in the field. His dedication and expertise make him a trusted resource for all things tech-related.

    Related Posts

    Install PHP on Ubuntu 22.04
    Ubuntu

    How to Install PHP on Ubuntu 22.04 [Simple Guide]

    Read More
    Uncategorized

    How to Get your First Order On Fiverr in 2023 – 5 Proven tricks

    Read More
    Install Microsoft Teams on Ubuntu 22.04
    Ubuntu

    How to Install Microsoft Teams on Ubuntu 22.04 (Easy Steps)

    Read More
    Add A Comment

    Leave A Reply Cancel Reply

    Install Google Chrome on Ubuntu 22.04

    How to Install Google Chrome on Ubuntu 22.04(Easy Guide)

    August 29, 2023
    Enable PHP BCMath Extension in cPanel

    How to Enable PHP BCMath Extension in cPanel (3 Easy Quick Steps)

    August 25, 2023
    install PHP GD on Ubuntu

    How to Install PHP GD on Ubuntu 22.04 [Easy Guide]

    May 2, 2023
    Install PHP on Ubuntu 22.04

    How to Install PHP on Ubuntu 22.04 [Simple Guide]

    April 26, 2023

    Subscribe to Blog

    Enter your email address to subscribe to
    this blog and receive notifications of new posts by email.

    Facebook Twitter Instagram Pinterest
    • Sitemap
    • Privacy Policy
    • Disclaimer
    • Term of Services
    © 2023 Technoracle

    Type above and press Enter to search. Press Esc to cancel.

    Click to Copy
    Go to mobile version