- How to copy and paste a file?
- 3 Answers 3
- How to copy a file’s content from Linux terminal?
- How can I Copy Files in a Folder into Another Folder on Linux Terminal
- File copying in terminal
- Cp Command
- mv command
- Rsync Command
- Final thoughts
- About the author
- Sidratul Muntaha
- How do I copy folder with files to another folder in Unix/Linux? [closed]
- 3 Answers 3
- How to Copy files and folders in Linux
- What is cp command?
- Common syntax for cp command:
- Common options for cp command:
- 1) Copying files with cp command
- 2) Copying files with a different name
- 3) Copying multiple files in Linux
- 4) How to copy directories recursively
- 5) Copying multiple directories recursively
- 6) Copying specific format files in Linux
- 7) Copying all files at once in Linux
- 8) How to copy hidden files (‘.’ dot files) in Linux
- 9) Backup the file with cp command
- 10) Copying files with attributes
- 11) How to avoid file overwriting with cp command?
- 12) Prompting confirmation with cp command
- 13) Copying link files in Linux
- Conclusion:
How to copy and paste a file?
I want copy and paste a file. The name of the file is mkoctfile.m .
The path of this file is:
I want to paste it to the following path
I have made the directory by using following commands:
sudo su mkdir -p /usr/bin/mkoctfile-3.2.4
but I don’t know how to copy and paste mkoctfile.m in this path.
Please tell me what command I have to use.
3 Answers 3
Use the cp command to copy a file, the syntax goes cp sourcefile destinationfile . Use the mv command to move the file, basically cut and paste it somewhere else.
The exact syntax you would use for your example is:
For more information on the cp or mv commands you can run:
To view the manual pages
Go to that directory from where you want to copy file i.e /usr/bin/octave/3.2.4/m/miscellaneous
../../../ means you are going backward to bin folder and type whatever directory in which you want to copy your file.
You can cut, copy, and paste in CLI intuitively like the way you usually did in the GUI, like so:
- cd to the folder containing files you want to copy or cut.
- copy file1 file2 folder1 folder2 or cut file1 folder1
- close the current terminal.
- open another terminal.
- cd to the folder where you want to paste them.
- paste
To be able to do so, make sure you have installed xclip and readlink . Then, append these functions to the end of your
If you don’t know what .bashrc file is and never modify it before, just open the file explorer, go to Home, press Ctrl+H (show hidden files), search for .bashrc and open it with a text editor like gedit.
Note
By using the above script, you are overriding the default functionality of these commands:
- copy is a reserved PostgreSQL command.
- cut and paste are reserved Linux command.
If you use one of those commands default functionality, just modify the script function names accordingly. For example, use p instead of paste .
Источник
How to copy a file’s content from Linux terminal?
This article shows the alternative method to copy the content of the file onto the clipboard, via the Linux terminal. In OSX, the commands pbcopy and pbpaste are available by default. Thus, to copy a file onto the clipboard via OSX terminal, type:
Since, in Ubuntu, pbcopy and pbpaste commands are not available by default, installing xclip will serve our purpose.
The next step is to open the bash_aliases file.
Next, type in the following inside bash_aliases file and save it.
This will set pbcopy and pbpaste as the aliases for xclip’s copy and paste commands respectively. Now we are ready to test the commands in the terminal. Let’s create a file named ‘file.txt’ and add some content into it.
Save the file and exit from the text editor. Let’s try copying and pasting its content from the terminal with our pbcopy and pbpaste commands.
Thus, now we are able to copy and paste the contents of our file using the terminal.
Note: One can use pbcopy command in the terminal to copy the c++/python template file used for competitive programming. Instead of the opening and copying the template file every time you attempt a question, just run the command in the terminal to get the work done and save time.
Источник
How can I Copy Files in a Folder into Another Folder on Linux Terminal
File copying in terminal
Copying the content of one directory into another is a very simple task. However, you have plenty of options of doing it. Linux is blessed with numerous tools to manipulate files and directories through various actions.
All the steps are demonstrated on Ubuntu 18.04.1. At first, let’s create a handful of dummy files to work with.
Now, let’s create the dummy files using touch.
Let’s create another folder within dir_A with a handful of dummy files.
Now, the entire structure of the directory dir_A looks like this.
Cp Command
The cp command is, by far, the most common tool for copying files. It’s a tool that comes by default with any Linux system. Copying files and directories are the sole purpose of cp. It supports a wide range of features.
This is the basic structure of the cp command.
For example, let’s make a copy of the dummy1 file with the name dummy1_copy.
Now, let’s try copying multiple files at once. For such an operation, cp requires the destination be a directory. Here, I’ll be using wildcard expression to grab all the dummy files and put them into the
How about copying a directory?
Here, we used two different flags. Let’s have a quick breakdown of them.
- -r: Stands for a recursive copy (including any hidden file(s)). Must-have flag if copying involves an entire directory or more.
- -v: Stands for verbose. The cp tool will output every single action it’s performing.
Want to follow the symbolic links? Add the “-L” flag.
Let’s say the destination directory already has a file with the same name. If you want to avoid overwriting, use the “-n” flag. It’ll ensure that in case of conflict, cp won’t overwrite the file.
If you’re performing a sensitive copy/paste action, it’s better to have the finest control over what happens, right? In such a case, use the “-i” flag. It stands for an interactive mode where cp will ask for confirmation every time a conflict arises.
If you’re more interested, the man page is always one of the best documentation. Learn more about cp.
mv command
The mv tool is similar to the cp tool. However, instead of copying, mv moves files and directories. Compared to cp, it’s a simpler one.
The command structure of mv is similar to cp.
To move the contents of dir_A to dir_A_copy, run the following command.
Here, the “-v” flag is for verbose mode. If you want to move just the contents of dir_A, use the following one instead. In this case, the target directory must exist beforehand.
The mv tool doesn’t have much to work with. For all the available options, check out the man page.
Rsync Command
This is one of the most popular and powerful tools for file copying, used mostly for backup. It holds the ability to copy files over a remote connection. Unlike cp and mv, it also comes with a ton of advanced file copying options that offer the utmost customization of its behavior.
Rsync is mostly famous for its delta-transfer algorithm that dramatically reduces the amount of data that needs to be exchanged. In the case of remote syncing, this saves up a lot of bandwidth.
Rsync doesn’t generally come pre-installed in any Linux distro. However, because of its popularity, it’s now available on all the major Linux distros. Use Pkgs.org to find out the rsync package for your distro. Assuming that you got rsync installed, let’s get started.
First, the basic command structure of rsync. It’s quite similar to cp.
Before getting started, here are a handful of popular rsync arguments.
- -v: Verbose mode, outputs whatever action being performed on the console screen.
- -vv: Verbose mode with more details
- -r: Recursive mode, must be present if copying directories
- -z: Compress data
- -a: Archive data
- -n: Dry run, should be run in combination with verbose mode. Simply outputs the actions if the command was to run for real
- –delete: Delete the file(s) and directory(s) on the destination directory if the source directory doesn’t contain them any longer
- -no-whole-file: Forces rsync to use its delta transmission algorithm, no matter what. Useful in scenarios where you must ensure the least amount of data write.
- –remove-source-file: Delete source file(s) after copying.
Let’s check them out in action. First, the directory copy. Run the following command.
For further detailed output, let’s use the “-vv” flag.
Now, let’s check out the cool part of rsync. We discussed how rsync intelligently copies only the files that are modified, saving bandwidth and disk write. It’s time to check it out in action. In this example, the action will be performed within the dir_A directory.
Despite delta-transmission enabled, rsync may not apply it when you’re performing local data transfer.
Now, let’s check out the dry run feature. Before running any rsync command for real, it’s always important that you test it out beforehand. This is where the dry run comes in. Rsync will output what will happen if the command was to actually run.
Rsync can also work with remote directories, no problem. All it needs is a proper SSH connection with SSH keys configured.
Want to perform a move operation? To put that in rsync’s perspective, rsync will copy the contents of the source directory and then, delete the source content.
Check out the result of the operation.
Final thoughts
The cp and rsync are the most common tools you can use for copying files in a folder on the Linux terminal. Both of them are also really good for scripting. Just make sure to test the functions out before implementation.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
Источник
How do I copy folder with files to another folder in Unix/Linux? [closed]
Want to improve this question? Update the question so it’s on-topic for Stack Overflow.
Closed 8 years ago .
I am having some issues to copy a folder with files in that folder into another folder. Command cp -r doesn’t copy files in the folder.
3 Answers 3
The option you’re looking for is -R .
- If destination doesn’t exist, it will be created.
- -R means copy directories recursively . You can also use -r since it’s case-insensitive.
- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 ‘s / @Anton Krug ‘s comment):
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you’re copying is called dir1 and you want to copy it to your /home/Pictures folder:
Linux is case-sensitive and also needs the / after each directory to know that it isn’t a file.
is a special character in the terminal that automatically evaluates to the current user’s home directory. If you need to know what directory you are in, use the command pwd .
When you don’t know how to use a Linux command, there is a manual page that you can refer to by typing:
at a terminal prompt.
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you’ve started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
Источник
How to Copy files and folders in Linux
Being a Linux user, copying files and directories is one of the most common tasks.
Linux users don’t spend a day without using the cp (copy) command according to my personal experience. cp command is used to copy a single file or group of files or directory.
To perform the copy operation, you must have at least read permission in the source file and write permission in the target directory.
In this article, we will explain how to use the cp command.
To perform remote file copy, use the commands such as rsync command or scp command or pscp command.
What is cp command?
‘cp’ command is one of the basic and most widely used Linux commands for copying files and directories from one location to another.
When copying files from source to destination, the source file remains the same and the target file may have the same or different name.
Common syntax for cp command:
Common options for cp command:
Options | Description |
---|---|
-v | Verbose mode (Show progress) |
-r/R | Copy directories recursively |
-n | Don’t overwrite an existing file |
-d | Copy a link file |
-i | Prompt before overwrite |
-p | Preserve the specified attributes |
-b | Make a backup of each existing destination file |
To demonstrate file copy operation in detail, we will be performing a copy operation between user1 and user2 .
1) Copying files with cp command
Use the following command to copy a file from one location to another.
In the following example, we will copy the source file “/home/user1/cp-demo.txt” to the target directory “/home/user2” with the same name:
You can check whether the specific file is copied to the target directory using the ls command as shown below:
2) Copying files with a different name
Use the following command to copy a file from one place to another with a different name. You need to give a new name to the target file.
In the following example, we will copy a file named “/home/user1/cp-demo.txt” to the target directory with a new name “/home/user2/cp-demo11.txt” :
Use the ls command to verify this:
3) Copying multiple files in Linux
The following cp command copies multiple files from one location to another.
In this example, we will copy the following three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt” to the target.
No additional option is required to copy multiple files, and the file name(s) must be separated with a space as shown below:
Use the ls command to verify the copied files. You should see them in the target directory as shown below:
4) How to copy directories recursively
To copy a directory recursively from one location to another, use the -r/R option with the cp command. It copies everything, including all its files and subdirectories.
In the following example, we will copy the ‘/home/user1/cp-demo-folder’ directory to ‘/home/user2/’ and the target directory name will remain the same:
Use the ls command to verify the outcome. You should see them in the target directory as shown below:
5) Copying multiple directories recursively
Use the following command to copy multiple directories at once. In the following example, we will copy the public_html & public_ftp folders to the target directory named /home/2daygeek/cp-test.
Output can be verified using the ls command:
6) Copying specific format files in Linux
Sometimes you may have to copy files with a specific extension for a certain purpose. If so, you should use the cp command with the «wildcard (*)» option and the file extension name.
In the following example, we will copy all the files containing the *.sh extension from the source to the target directory. Similarly, you can copy any file extensions such as .jpg, .png, .txt, .php, and .html.
This can be verified using the ls command:
7) Copying all files at once in Linux
To copy all the files except the directory from one location to another, use the following cp command with wildcard (*) option.
cp command excludes directories by default, and the -r option must be added to copy them.
In this example, we will copy all the files from ‘/home/user1/’ to ‘/home/user2′ :
Verify the output using the ls command as shown below:
8) How to copy hidden files (‘.’ dot files) in Linux
By default, the cp command does not copy “dot (.)” or ‘hidden’ files in Linux.
Use the following command to copy all types of files, including link files (soft link or hard link), folders and hidden or dot files from source to destination.
This example is very useful to copy user’s home directory which has several hidden files:
You can verify this by using the ls command with the “-a” option:
9) Backup the file with cp command
cp command allows you to backup the file if the same file name exists in the target directory using the —backup option.
In this example, we will copy the /home/user1/passwd-up.sh file to the target directory /home/user2/.
It will backup the “passwd-up.sh” file in the target directory during the copy operation, if you say “yes” when you see the message below:
It adds the “Tilde (
)” symbol at the end of the old file, the same can be verified in the following output.
10) Copying files with attributes
By default, Linux replaces file attributes such as username, group name, date and time when copying files from a user, and does not carry the original file attributes.
To preserve the original attributes of the files, use the -p option with the cp command.
Upon querying we can see that the file “service-1.sh” has retained its original attributes as shown below:
11) How to avoid file overwriting with cp command?
When copying a file, use the -n option with the cp command to avoid overwriting an existing file.
It will only copy the source file, if the target directory does not have a file with the same name. If a duplicate file exists in the target, then the command will run, but won’t make any changes:
Upon querying using the ls command, it can be seen that no action is taken against the file because it still has the old attributes, which can be seen in the above example (refer section 10):
12) Prompting confirmation with cp command
Use the -i option with the cp command to prompt for confirmation before overwriting the file, in case the file exists in the target location.
The following example prompts for confirmation when copying the “cp-demo-9.txt” file because the file already exists in the target directory:
13) Copying link files in Linux
By default, the cp command excludes link files while performing the copy operation. Use the -d option with the cp command to copy the link files as shown below:
It has been successfully copied, and the soft link file can be found in the below output:
Conclusion:
In this tutorial, we have demonstrated 13 methods that are widely used by the Linux administrators in their daily operations. If you would like to explore other options, please visit the man page of the cp command by using the following command:
Источник