Copying file in linux terminal

How to copy files via terminal?

1) By using -i for interactive you will be asked if you would like to replace the file:

cp -i /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive/ 

or you can use -b to create a backup of your file:

cp -b /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive 

2) Same as the above:

cp (-i or -b) /media/sda3/SkyDrive/untitelds.mpg /home/levan/kdenlive 

3) Use -R for recursive and -i for interactive:

4) This last one can be done via the mv command, move is like cutting:

mv -i ~/MyFile ~/OtherFolder/MyFile 

if you want to move a directory, use:

mv -Ri ~/MyDirectory ~/OtherDirectory/ 

@WarriorIng64 you can use four spaces identation for code blocks, or you can surround small pieces of code with `s.

@WarriorIng64 That is triggered by the bash tag in the question. highlighting will be enabled by default by the system. If you answer a question with bash tag, the hightlighting will follow bash style and so on.

@Anwar I was just partway through making a Meta post on this when I saw your comment. Of course, I credited you in the answer. 🙂

When ~/Dropbox/RECENT/ is your current directory:

And I want to copy input.txt with another name in my current directory.

Again with ~/Dropbox/RECENT/ as current directory:

Existing filenames can be auto-completed using TAB .

Long version of the same copy command (when you are not in ~/Dropbox/RECENT/ ):

cp /home/$USER/Dropbox/RECENT/input.txt /home/$USER/Dropbox/RECENT/SORT/ 

I put a / behind every directory. If SORT does NOT exist a cp will also create a file named SORT making you think something went wrong. Adding the / will have cp error out and not copy the file.

Copying a file something.txt to file folder : use cp something.txt folder/

Copying a file something.txt to the current directory as something2.txt : use cp something.txt something2.txt

ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:53 Folder1 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l Folder1/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:53 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 8 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:54 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 0 ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt folder/ ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l folder/ total 4 -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something.txt ubuntu@ubuntu-T100TA:~/TestFolder$ cp something.txt something2.txt ubuntu@ubuntu-T100TA:~/TestFolder$ ls -l total 12 drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 12 21:55 folder -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:55 something2.txt -rw-rw-r-- 1 ubuntu ubuntu 14 Mar 12 21:52 something.txt 

Источник

How to Copy Files and Directories in Linux

This guide will show you how to copy files and directories in Linux by executing commands from the command line. Furthermore, the commands listed below detail how to create system-wide backups or filter out and copy only specific files.

Читайте также:  Find string and replace linux

how to copy files and directories in linux

Note: These Linux commands can only be run from a terminal window. If your version of Linux boots to a desktop graphical interface, launch a terminal window by pressing CTRL-ALT-F2 or CTRL-ALT-T.

Using the cp Command to Copy Files and Directories in Linux

The cp command is the primary method for copying files and directories in Linux. Virtually all Linux distributions can use cp . The basic format of the command is:

cp [additional_option] source_file target_file
cp my_file.txt my_file2.txt

This Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt.

By default, the cp command runs in the same directory you are working in. However, the same file cannot exist twice in the same directory. You’ll need to change the name of the target file to copy in the same location. Some users will add _old, some will add a number, and some will even change the three-letter extension (e.g., .bak instead of .txt).

You may not get a warning before Linux overwrites your file – be careful, or see below for the –i option.

Additional Options

Additional options can be used in combination with the cp command:

  • –v verbose: shows the progress of multiple copied files
  • –ppreserve: keeps the same attributes, like creation date and file permissions
  • –f force: force the copy by deleting an existing file first
  • –i interactive: prompts for confirmation, highly advised
  • –Rrecursive: copies all files and subfolders in a directory
  • –u update: copy only if source is newer than destination

Note: The -p (preserve) option forces the system to preserve the following source file attributes: modification time, access time, user ID (UID), group ID (GID), file flags, file mode, access control lists (ACLs), and extended attributes (EAs).

How to Copy File to Another Directory in Linux

To copy a file from the directory you’re working in to a different location, use the command:

cp my_file.txt /new_directory

You don’t need to rename the file unless there’s already one with the same name in the target directory.

To specify a path for the source file:

cp /etc/my_file.txt /new_directory

This lets you copy without having to change directories. The cp command will create the /new_directory if it doesn’t exist.

To rename and copy a file to a different path:

cp my_file.txt /new_directory/my_file2.txt

Copying a file in Linux with cp.

This option is useful for creating backups of configuration files, or for copying data to a storage device.

Note: Learn how to move directories in Linux.

Copy Multiple Files from One Directory to Another in Linux

You may need to copy more than one file at a time.

List each file to be copied before the target directory:

cp my_file.txt my_file2.txt my_file3.txt /new_directory

This example created a copy of all three files in the /new_directory folder.

Use a wildcard to specify all files that share a string of characters:

cp /pictures/*.jpg /new_directory

This would find all the files with the .jpg extension in the /pictures directory, and copy them into the /new_directory folder.

Читайте также:  Cat text to file linux

To copy an entire folder and its subfolders and files, use the –R option:

cp –R /documents /new_directory

Copying and renaming a folder in Linux.

–R stands for recursive, which means “everything in that location.” This would copy all the files, as well as all the directories, to the /new_directory folder.

Copy Using rsync Command

The rsync command in Linux is used to synchronize or transfer data between two locations. Usage is similar to cp , but there are a few key differences to note.

To copy a single file, enter the following into a terminal:

rsync –a my_file.txt /new_directory/my_file_backup.txt
  • The –a option means all, and is included with rsync commands – this preserves subdirectories, symbolic links, and other metadata.
  • Replace the my_file.txt file in the working directory.
  • Replace /new_directory/ with the destination.
  • Using my_file_backup.txt as the target indicates the file will be renamed during the copy.

To copy a directory with rsync, enter the following:

rsync –a /etc/docker/ /home/backup/docker/

This copies the contents of the /etc/docker/ directory to /home/backup/docker/. Make sure to keep the slashes. Omitting the slash on the source directory will copy the contents into a subdirectory.

To omit files from being copied, check out our guide on how to exclude files and directories in data transfer using rsync command.

Other Options

The ls command is a handy partner to the cp command in Linux.

To list the contents of a directory enter the command:

The example above displays all the files in /directory. Use this command after copying to verify the files were copied successfully.

To change directories, use cd and the name of the directory. For example:

The command prompt will change to display that you’ve changed directories.

Now you understand how to copy files in Linux. The cp command is a versatile and powerful tool for managing and backing up files.

Источник

How to Copy Files in Linux: A Complete Guide

This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher.

The wikiHow Tech Team also followed the article’s instructions and verified that they work.

This article has been viewed 219,888 times.

This wikiHow teaches you how to copy and paste a file on a Linux computer. The command line can be used to copy and paste files, or you can use the keyboard shortcuts or your computer’s right-click function if you’re using a version of Linux that has a user interface.

  • Open the Terminal by pressing Alt + Ctrl + T on most Linux versions.
  • Type in cd path where «path» is the address of the folder in which the file you want to copy is located.
  • Type the «copy» tag cp followed by a file’s name and extension.

Using the Command Line

Image titled Copy Files in Linux Step 1

Image titled Copy Files in Linux Step 2

  • For example, to tell Terminal to look for your file in the Desktop folder, you would type cd Desktop into Terminal.
  • Make sure you capitalize the folder’s name if necessary.
  • If attempting to switch to a folder results in an error, you’ll need to enter the folder’s entire path (e.g., /home/name/Desktop/folder instead of just folder ) here.
Читайте также:  Php server linux ubuntu

Image titled Copy Files in Linux Step 3

Image titled Copy Files in Linux Step 4

  • For example, if you want to copy a file named «hello», you would type cp hello into Terminal.
  • If the file name has an extension on the end of it (e.g., «.desktop»), make sure you include the extension in the file’s name when typing it into Terminal.

Image titled Copy Files in Linux Step 5

  • For example, if you want to copy «hello» into a folder named «Hi» that’s stored in the Documents folder, you would have cp hello /home/name/Documents/Hi (where «name» is your username) typed into Terminal.

Image titled Copy Files in Linux Step 6

Press ↵ Enter . Doing so will run your command. Your file will be pasted into the folder you specified.

Using the Interface

Image titled Copy Files in Linux Step 7

  • Click the file you want to copy to select it, or drag your mouse across multiple files to select them all.
  • Press Ctrl + C to copy the files.
  • Go to the folder into which you want to copy the files.
  • Press Ctrl + V to paste in the files.

Image titled Copy Files in Linux Step 8

Image titled Copy Files in Linux Step 9

Image titled Copy Files in Linux Step 10

  • Some Linux versions will also display a menu bar at the top of the screen. If so, you can click Edit instead of right-clicking the selected file.

Image titled Copy Files in Linux Step 11

Image titled Copy Files in Linux Step 12

Image titled Copy Files in Linux Step 13

Image titled Copy Files in Linux Step 14

Community Q&A

What option will allow me to copy the contents of /dir1 to /dir2 while preserving the modification times, ownership, and permissions?

You can try «-a» for an archive to preserve as much as possible. You can also do «—preserve» followed by a comma-separated list of attributes to preserve (or «—preserve all»). Finally, try «cp —help» or «man cp» for more options.

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

If you want to move a file to a different folder rather than copying the file, you can type mv instead of cp when specifying the file’s name and its destination (e.g., mv hello Documents).

Holding down Ctrl while clicking individual files will select each file you click. Right-clicking one of the selected files and selecting Copy will then copy all of the selected files.

Not all versions of Linux have a user interface. If your version of Linux has only a command line, you’ll need to use the «cp» command to copy your files.

You Might Also Like

Move from Windows to Linux

Transfer Files from One Linux Server to Another

How to Transfer Files from One Linux Server to Another

Install Google Chrome Using Terminal on Linux

Can Linux Run Exe

Can Linux Run .exe Files? How to Run Windows Software on Linux

Add or Change the Default Gateway in Linux

Open Ports in Linux Server Firewall

How to Open Linux Firewall Ports: Ubuntu, Debian, & More

Take a Screenshot in Linux

Become Root in Linux

Execute INSTALL.sh Files in Linux Using Terminal

How to Run an INSTALL.sh Script on Linux in 4 Easy Steps

Ping in Linux

Use Ping in Linux: Tutorial, Examples, & Interpreting Results

Boot Linux from a USB on Windows 10

Delete Read Only Files in Linux

How to Delete Read-Only Files in Linux

Источник

Оцените статью
Adblock
detector