HomeLinuxCopy Recordsdata and Directories in Linux Command Line

Copy Recordsdata and Directories in Linux Command Line


Copying information is without doubt one of the most simple but essential duties you’ll be doing repeatedly.

Linux has a devoted cp command for copying each information and directories (folders).

On this a part of the Terminal Fundamentals collection, you will be taught to repeat information and folders within the terminal.

Let’s go on with the seventh chapter within the collection.

Copying information in Linux command line

Let me present you a couple of examples of copying information.

Copy a file to a different listing

To repeat one file to a different listing, all you need to do is comply with the given command syntax:

cp Source_file Destination_directory

For instance, right here, I’ve copied a file named Hey.txt to the listing named Tux:

copy file to another directory in linux command line

And as you may see, the file has efficiently been copied to the Tux listing.

Copy the file however rename it

You’ll be able to select to rename the file whereas copying it. Simply give a distinct identify to the ‘goal file’.

cp Source_file Renamed_file

For reference, right here, I’ve copied a file named Hey.txt to the identical listing by renaming it to Renamed_Hello.txt:

rename a file while copying in a same directory in linux terminal

Why would you do this? Say, you need to edit a config file. apply is to make a backup of the config file in the identical location earlier than enhancing it. This fashion, you may revert to the previous configuration if issues do not go as deliberate.

Copy a number of information to a different location

To repeat a number of information to a different listing, execute the command within the following vogue:

cp File1 File2 File3 FileN Target_directory

Right here, I copy a number of information to a brand new location.

copy multiple files using the cp command in linux

📋

If you find yourself copying a number of information, renaming them wouldn’t be potential with simply the cp command.

Cope with duplicate information whereas copying

By default, the cp command will override the file if a file with the identical identify exists within the goal listing.

To keep away from overriding, you should use the -n choice with the cp command, and it will not override the present information:

cp -n Source_File Destination_directory

For instance, right here, I’ve tried to repeat two information that had been already there in my focused listing and used -v choice to showcase what’s being achieved by the command:

cp -n -v itsFOSS.txt LHB.txt LU.txt ~/Tux
how not to override files while copying in linux using the cp command

Interactively copy information

However what about whenever you need to override some information, whereas some must be stored intact?

Properly, you should use the cp command within the interactive mode utilizing the -i choice, and it’ll ask you every time whether or not the file must be overridden or not:

cp -i Source_file Destination_directory
how to use cp command in interactive mode

🖥️

Apply all of the above-discussed examples your self. You already find out about creating information and folders so recreate every little thing.

Copy directories in Linux command line

There may be mkdir command to make new directories, rmdir to take away (empty) directories. However there isn’t a cpdir command for copying directories.

You may have to make use of the identical cp command however with the recursive choice -r to repeat a listing with all its content material to a different location:

cp -r Source_dir Target_dir

For instance, right here, I’ve copied a listing named IF to LHB:

how to copy a directory in linux command line

Nevertheless it copied your entire listing 🤨

So, what do you do whenever you solely need to copy the listing’s contents, not the listing itself?

Here is what you are able to do:

Copy solely the contents of a listing (not the listing)

To repeat solely the contents of the listing, not the listing itself, you append /. on the finish of the supply listing’s identify:

cp -r Source_directory/. Destination_directory

Right here, I need to copy the contents of a listing named IF which comprises the next three information:

check the file contents of directory using the tree command

And I’ll execute the next command to repeat the file contents of the IF listing to LHB:

cp -r IF/. LHB
copy the file contents of directory not a directory itself in linux command line

You may also use Source_directory/* right here.

Copy a number of directories

To repeat a number of directories, you’ll have to execute the command within the following approach:

cp -r Dir1 Dir2 Dir3 DirN Destiniation_directory

For instance, right here, I’ve copied two directories named IF and LU to the LHB:

cp -r IF LU ~/LHB
copy multiple directories using the cp command in linux command line

You are able to do the identical whenever you need to copy information from a number of directories however not the listing itself:

cp -r Dir1/. Dir2/. Dir3/. DirN/. Destination_directory
copy files from multiple directories but not directories their self using the cp command

🖥️

You may also rename the directories the identical approach you renamed information. 

Check your data

Now, let’s examine how a lot you bear in mind the teachings discovered to this point.

  • Create a listing known as copy_practice
  • Copy the file /and so on/providers to this newly created folder
  • Create a folder named secrets and techniques underneath this listing and duplicate information /and so on/passwd and /and so on/providers in it
  • Copy the providers file in copy_practice to the secrets and techniques folder however do not overwrite it
  • Copy the secrets and techniques folder to your own home listing
  • Delete the secrets and techniques and copy_practice directories

That may provide you with some apply.

It is going properly to this point. You could have discovered fairly a couple of issues. Within the subsequent chapter, you will see about transferring information and folders with mv command.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments