CentOS

How do I Copy a File from One Directory to Another in Ubuntu Terminal?

How do I Copy a File from One Directory to Another in Ubuntu Terminal

The Ubuntu Terminal makes it more flexible to copy files and folders from one directory to another. As Linux is widely used by programmers and system administrators, copying files and folders from one place to another is a crucial task and may be hectic if copying is not done efficiently.

This article explains how files can be copied from one directory to another using the Ubuntu Bash or Terminal.

How do I Copy a File From One Directory to Another in Ubuntu Bash?

The “cp” command in Ubuntu copies files and folders to other Directories more efficiently and easily. Copying Files can be performed in multiple methods with the “cp” command discussed below.

Copy a File From One Directory to Another in Ubuntu Using the “cp” Command

Copying files to other Directories from one Directory can be done with the “cp” command having the following syntax:

cp <file-name> <./destination-path>

The “file-name” is the file you want to copy from the current directory to another directory, which in the above syntax, is the destination path.

Consider the example where you want to copy a file “abc.txt” from the current Directory to the FolderA in our case:

To copy the “abc.txt” file, use the cp command followed by the file name or complete path (if you are not in the same/current directory) and the destination directory path.

If you are in the current “Documents” directory, use the “cp” command as:

cp abc.txt ./FolderA

This will copy the “abc.txt” file from the Documents directory to the FolderA directory:

The copied file can be verified by opening the FolderA directory:

You can also verify using the Terminal by navigating to the FolderA directory using the “cd“ command and then listing the files using the “ls” command:

ls

To copy the file to a different directory (other than the current directory), you can mention the source file path with the “cd” command. In the example above, we will move the “abc.txt” file from the Home Directory to the “dirB” directory using the following cp command:

cp ./Documents/abc.txt ./Documents/dirB/

The file is moved to the “dirB” directory and can be verified using either the terminal or navigating to the directory using the GUI.

Navigating to the directory through GUI, you will see the copied “abc.txt” file in the “dirB” directory:

Copy Multiple Files from One Directory to Another Using the “cp” Command

Multiple files can also be copied to other Directories. In our case, we have to copy the “fileA.txt”, “fileB.txt”, and “fileC.txt” from the Documents directory to the “MultDir” directory:

To copy all three files, use the “cp” command as follows:

cp fileA.txt fileB.txt fileC.txt ./MultDir

This will copy all three text files from the Documents Directory to the MultDir Directory:

The copied files can be verified either with the command line or through GUI. List files in the “MulttDir” Directory with the “ls” command:

ls

This will display all the files available in the directory:

You can also verify through GUI by opening the directory:

Copy the Same Type of Files From One Directory to Another Using the “cp” Command

Multiple files with the same extension can also be copied to other Directories with the “cp” command. The syntax of the “cp” command for copying multiple files of the same type is:

cp .</DirectoryA-Path>*.txt .</DirectoryB-Path>

The *.txt will ensure all the files with the “.txt” extension are copied. Consider our case where we have multiple “.txt” files in the Documents directory:

 

To copy all these files to the “AllTextFiles” Directory, use the “cp” command as follows:

cp *.txt ./AllTextFiles

Upon doing so, all available “.txt” files will be copied to the “AllTextFiles” Directory and the cursor will move to the next line in the terminal:

To verify, change the directory using the “cd” command and then list all the files using the “ls” command. All the files are copied in our case:

cd AllTextFiles
ls

You can also verify through the GUI by opening the directory:

Bonus Tip: Copy Directory From one Directory to Another

Copying a Complete Directory to Other Directories is also achievable with the “cp” command. The “-r” flag is used with the “cp” command to specify the command to move the directory:

cp -r <source-directory> <destination-directory>

In our case, if we want to copy the “FolderA” directory to the “dirB” directory, the “cp” command will be executed as follows:

cp -r ./FolderA ./dirB

This will copy the directory and the cursor will move to the next line:

To verify, you can open the directory in the GUI:

Conclusion

Copying files from one directory to another is a crucial task for system administrators when copying/transferring useful data or creating a backup of data. The “cp” command can be used to copy a single file, multiple files, or all global files with the same extension from one directory to another. This article explained the methodologies in detail for copying files to other Directories.

Similar Posts