How to copy file using terminal in ubuntu?

by raphaelle , in category: General Help , 2 years ago

How to copy file using terminal in ubuntu?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by margarett , 2 years ago


Copying within a directory

To copy a file to another file within the same working directory, once can simply run

cp file1 file2

Copying across directories

You can first checkout the directory where you would like the file copied to. Then you can run the copy command to copy the file to that current directory. i.e

cd ~/some_directory


cp ~/another_directory/some_file.txt

This basically will copy the some_file.txt file to the current directory which I checked out, which is the some_directory folder.

You can also copy a file across directories using the absolute path-name of the file, starting from root. If you specify all the directories and path for the original as well as the destination, you can perform this same instruction as above without having to checkout any directory first. For example

cp /some_directoryA/some_directoryB/usera/sampel.txt  /some_directoryA/some_directoryB/usera/some_directoryC/sampel.txt

If you are copying a subdirectory and its files, you will need to do

cp -r  ~/some_directoryA/some_directoryB/* ~/some_directoryA/some_directoryB/new_copy_directory_B

This command will copy everything in the some_directoryB to the new_copy_directory_B directory.


by alivia , 7 months ago


Another method to copy files using the terminal in Ubuntu is by using the 'cp' command with the source file path and the destination file path.

  1. Open the terminal and navigate to the directory where the file you want to copy is located. You can use the 'cd' command to change directories. Example: cd /path/to/source_directory
  2. Use the 'cp' command followed by the source file name and the destination file path. Example: cp file.txt /path/to/destination_directory This will copy the file "file.txt" to the specified destination directory.
  3. If you want to copy multiple files or directories recursively, use the '-r' option for recursive copy. Example: cp -r directory /path/to/destination_directory This will copy the "directory" and all its contents to the specified destination directory.
  4. If you want to preserve file attributes such as timestamps and permissions, use the '-p' option. Example: cp -p file.txt /path/to/destination_directory

Note: To copy files to system directories or protected locations, you may need to use the 'sudo' command before the 'cp' command to elevate your permissions.