- How to copy and paste a file?
- 4 Answers 4
- copy THEN paste FILES in terminal
- 4 Answers 4
- How to Copy and Paste Text, Files and Folders in Linux Terminal
- Content
- Copy and Paste Text
- Copy and Paste a Single File
- Copy and Paste a Folder and Its Contents
- Copy and Paste Multiple Files
- Copy and Paste All Files of the Same Type
- Make Copying More Interactive
- Rsync vs CP
- Move a File or Folder
- Frequently Asked Questions
- Is rsync better than cp?
- Why can't I just use Ctrl+C to copy items from my terminal?
- Copy and paste a file/directory from command line
- 3 Answers 3
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: /usr/share/octave/3.2.4/m/miscellaneous/mkoctfile.m I want to paste it to the following path /usr/bin/mkoctfile-3.2.4 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.
4 Answers 4
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:
sudo cp /usr/bin/octave/3.2.4/m/miscellaneous/mkoctfile.m /usr/bin/mkoctfile-3.2.4
For more information on the cp or mv commands you can run:
To @BKSurgeon, I would suggest to use the tab key to see the paths/directories available, or type ls to see them all at once printed.
I think it is better to use cp -a than just cp , if you want to have the same affect as when copy-pasting in desktop GUI.
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 realpath . Then, append these functions to the end of your ~/.bashrc file:
copy() < # if the number of arguments equals 0 if [ $# -eq 0 ] then # if there are no arguments, save the folder you are currently in to the clipboard pwd | xclip else # save the number of argument/path to `~/.numToCopy` file. echo $# >~/.numToCopy # save all paths to clipboard # https://stackoverflow.com/q/5265702/9157799#comment128297633_5265775 realpath -s "$@" | xclip fi # mark that you want to do a copy operation echo "copy" > ~/.copyOrCut > cut() < # use the previous function to save the paths to clipboard copy "$@" # but mark it as a cut operation echo "cut" >~/.copyOrCut > paste() < # for every path for ((i=1; i
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.
By using the above script, you are overriding the default functionality of these commands:
If you use one of those commands default functionality, just modify the script function names accordingly. For example, use p instead of paste .
copy THEN paste FILES in terminal
Is there any package that I can use to copy one (or more) file/folder THEN paste into another directory? I am using Ubuntu, and I have the standard terminal + Terminator. For example, I am looking for a functionality like:
Folder1$ COPY a.txt Folder1$ cd ../Folder2 Folder2$ PASTE (a.txt -- optional)
4 Answers 4
Here's my dumb way to do this, you can add them to your rcfile:
This doesn't copy any data to the clipboard, since you want to do this only for not referencing folders, and this works for copying folder as well.
It uses xclip to interface the desktop environment clipboard and provides handy commands like ccopy and cpaste that does exactly what was asked for here. As the desktop environment clipboard is used, the commands interact with the copy/pasting in file managers and other programs that use the clipboard for files. At least it works on Gnome-like desktops.
Full disclosure, I wrote the script after giving up finding something like it out there 🙂
I just created a tool that can do this.
Use the sap command followed by a list of file paths (either relative or absolute) to add paths.
To use one of the saved paths just write pap followed by the command you want to use the path in and hit enter. You will then be prompted to confirm the command, and can use the arrow keys to modify the command: left/right moves the path insert location and up/down switches between different saved paths.
How to Copy and Paste Text, Files and Folders in Linux Terminal
Copying and pasting is one of the most used actions on a computer. While it is easy to do so with the Ctrl + C and Ctrl + V keyboard shortcuts, on the Linux terminal it is not so straightforward. You have several options to get the job done. Here is how you can copy and paste text, files and directories in Linux terminal.
Content
Copy and Paste Text
If you just want to copy a piece of text in the terminal, all you need to do is highlight it with your mouse, then press Ctrl + Shift + C to copy.
To paste it where the cursor is, use the keyboard shortcut Ctrl + Shift + V .
The Paste shortcut also applies when you copy a section of text from a Word document (or any other application) and want to paste it in the terminal. For example, you can copy a command from a web page in your browser and use the Ctrl + Shift + V shortcut to paste it in the terminal.
If for some reason these keyboard shortcuts don’t work, the terminal might not be properly configured for it. Look for options that allow you to customize keyboard shortcuts. These are instructions for the most popular terminals:
- Gnome Terminal – Click the hamburger menu and click “Preferences.” Navigate to “Shortcuts” and scroll to “Edit.” The options for “Copy” and “Paste” will appear there ready to be configured.
- Konsole – Go to “Settings” and then click on “Configure Keyboard Shortcuts.” Search for “Copy,” configure it to use the key combos mentioned previously, then do the same for “Paste.”
Most other terminals will lock you into the default universal combination for copying/pasting in the console without the ability to configure them.
Copy and Paste a Single File
Any time you want to copy a file or folder in the Linux command line, the above keyboard shortcut won’t work. You have to use the cp command. cp is shorthand for copy. The syntax is simple, too. Use cp followed by the file you want to copy and the destination where you want it moved.
That, of course, assumes that your file is in the same directory you’re working out of. You can specify both.
cp ~/Downloads/your-file.txt ~/Documents/
You also have the option of renaming your file while copying it. Specify the new name in the destination.
cp ~/Downloads/your-file.txt ~/Documents/new-name.txt
Copy and Paste a Folder and Its Contents
In order to copy a folder and its contents, you’re going to need to tell the cp command to copy recursively. That’s simple enough with the -r flag.
cp -r ~/Downloads/pictures-directory ~/Pictures/family-vacation-pics
All the rest of your syntax is exactly the same. The -r flag serves to tell cp that it’s working with a directory and should copy its contents.
If you want the paste action to overwrite existing files, you can add the -f flag:
cp -rf ~/Downloads/pictures-directory ~/Pictures/family-vacation-pics
Copy and Paste Multiple Files
You can also copy multiple files. The Linux command line lets you target multiple items at once with brackets <> . You can use them to list the names of each file to be copied separated by commas.
cp ~/Downloads/{file1.txt,file2.jpg,file3.odt} ~/Documents/
All three files of differing file types will be copied to the Documents directory.
Copy and Paste All Files of the Same Type
If you have a ton of files of the same type to copy, you can use the wildcard character * . The asterisk/wildcard tells the Linux command line to accept absolutely anything in that place. So, if you tell Linux to copy *.jpg , it’ll copy all JPG files, regardless of the name or whatever comes before the .jpg part.
If you want to use multiple file types, say JPG and PNG, you can use the brackets from before.
cp ~/Downloads/*.{jpg,png} ~/Pictures/
Make Copying More Interactive
As you may have noticed while following this guide, using cp does not give you any sort of confirmation or prompt after finishing what it’s doing unless it encounters an error. This may cause a bit of discomfort especially when you’re doing very large file copy operations on slower hardware that could take a long time to complete.
By attaching the -v flag to your command, you tell cp to give you its most detailed output on what it does. The -i flag provides confirmation prompts so that you have a chance to cancel certain operations like overwriting certain files.
cp -rvi ~/Downloads/*.jpg ~/Pictures/
Generally the -v and -i flags are available in a large range of commands that are common to Linux.
Rsync vs CP
If you need to do several copy operations that overwrite files (such as creating a backup that you will update once in awhile) for some reason, rsync might just put a smile on your face.
rsync is a little more advanced in that it goes into the minutiae of each file, does a full comparison of the data in both, and within the file itself, only overwrites the data that has changed. This is especially useful if the destination folder is in an SSD, as it saves write cycles and doesn’t needlessly degrade the drive’s health by overwriting entire files.
Move a File or Folder
If you came here looking to move a file from one place to another without making a duplicate, you can do that easily too, but moving a file requires the mv command. The syntax is very similar to cp.
mv ~/Downloads/your-file.txt ~/Documents/
mv ~/Downloads/your-file.txt ~/Documents/renamed.txt
There is one major difference, though. You don’t need the -r flag to move a whole folder.
mv ~/Downloads/downloaded-folder ~/Pictures/vacation-pics
Frequently Asked Questions
Is rsync better than cp?
Neither is better than the other generally. rsync will make full comparisons of files in both the source and destination folders, taking up some computing resources to save on bandwidth or write cycles. It’s great if you’re constantly updating a backup of a particular folder, but less than ideal if you’re just doing a single copy operation.
cp functions more as a Swiss Army knife that just wants to get the job done. If you have to overwrite something, cp does it for you. Because of its capability for interactivity, it’s also more beginner-friendly and gives you a bit more control over the copying process.
Why can't I just use Ctrl+C to copy items from my terminal?
In Linux terminal applications, pressing Ctrl + C on your keyboard sends a SIGINT (interrupt) signal to the kernel. It’s a simple way of telling Linux to stop whatever is running on that particular terminal.
If you configure the “Copy” function in your terminal to respond to that key combination, you will encounter problems when copying a line of text from an executing command while it’s still in the process of doing something.
Image credit: Neron Photos on Pexels
Miguel has been a business growth and technology expert for more than a decade and has written software for even longer. From his little castle in Romania, he presents cold and analytical perspectives to things that affect the tech world.
Our latest tutorials delivered straight to your inbox
Copy and paste a file/directory from command line
I want to be able to copy a file into the clipboard, and paste it somewhere else, in another directory. something like this:
/usr/local/dir1# cp
/usr/local/dir1# cd /usr/local/dir2
/usr/local/dir2# paste
3 Answers 3
I think you should do something like the GUI applications do. My idea for doing this is to write two functions for Copy and Paste, where Copy writes path of files to be copied to a temporary file and Paste reads those paths and simply calls cp command. My implementation (to be put in .bashrc file) is like below:
function Copy < touch ~/.clipfiles for i in "$@"; do if [[ $i != /* ]]; then i=$PWD/$i; fi i=$; i=$ printf '%s\n' "$i" done >> ~/.clipfiles > function Paste < while IFS= read src; do cp -Rdp "$src" . done < ~/.clipfiles rm ~/.clipfiles >
Better scripts could be written for implementing this idea, I tested my own and it works very well for files and folders (I don't know how xclip could work for copying folders!!)
/usr/local/dir1# Copy a.txt *.cpp /usr/local/dir1# cd /usr/local/dir2 /usr/local/dir2# Paste /usr/local/dir1# Copy *.h *.cpp b.txt subdir1 /usr/local/dir1# cd /usr/local/dir2 /usr/local/dir2# Paste /usr/local/dir1# Copy a.txt b.txt /usr/local/dir1# cd /usr/local/dir2 /usr/local/dir2# Copy c.txt d.txt /usr/local/dir2# cd /usr/local/dir3 /usr/local/dir3# Paste