Linux copy and paste folder

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 

Источник

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.

Читайте также:  Setting default file permissions in linux

Источник

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.

Читайте также:  Linux openjdk java home

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 .

Источник

how do you copy a directory and its contents to a new location under a new directory name?

I'm new to the Linux command line and am trying to get to grips with the copy command at the moment. Can anyone please tell me if it's possible to copy a directory with its subdirectories and associated files to a new directory with a new name, e.g. directory_backup ? Thanks for any and all help in advance.

7 Answers 7

You can use cp with the -r (copy recursively) flag and specify the new directory name:

cp -r /path/to/directory /path/to/location/new-name 

In the above command replace the given paths with the real ones.

For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:

cp -r ~/stuff ~/backup/stuff-backup 

~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt

zanna@monster:~$ ^--the ~ here is where I am - home! 

You can add the -v (verbose) flag to make cp report each copy being performed:

$ cp -vr stuff backup/stuff-backup 'stuff/thing1' -> 'backup/stuff-backup/thing1' 'stuff/thing2' -> 'backup/stuff-backup/thing2 . 

Sorry Zanna. I'll put your advice into practice and see how I go. Like the idea of the verbose flag . assume this will also work as a report while other command are being worked.. Cheers.

The command you need is simply cp which stands for "copy".

You can use it for example with one of these syntaxes:

cp SOURCEFILE TARGETFILE cp SOURCEFILE TARGETDIRECTORY 

The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.

However, cp by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r option:

cp -r SOURCEDIRECTORY TARGETDIRECTORY 

You can learn more about the cp command by typing man cp in your terminal.

Not used to this comment section Byte Commander. keep hitting return to change line but post instead. Very clear outline in your answer . Look forward to sorting out those directories now that I've been given good instruction on the correct usage of the available tools. Excellent.

Читайте также:  Astra linux программный raid

@OwenHines Just that comments strip any linebreak and additional whitespace and having them in the editor doesn't change anything in the result.

You can use the below command to copy directory from one place to another sub directory.

cp -Rvp /home/edmuntu/data /home/edmuntu/mydata/ 
  • -R = copy directories recursively
  • v = explain what is being done
  • p = It will help your to preserve your permission and ownership with timestamps

Thank you @Rakesh Chauhan."preserve your permission and ownership with timestamps" . Wow. I need to get to it. Yet another aspect of line commands to be explored. Thank you for your reply.

As an alternative to the generally useful cp command you can use rsync which has the added benefit of replicating the permissions/timestamps of the original directory structure:

note the slash after the source dir.

Had huge issues with rsync. cp -Rvp with a . after the directory to copy is working well for me (the . will copy hidden files/foders). And @HelloUniverse - if you use the p command with cp, it preserves permissions, ownership, timestamps

cp is the command that does the function that you're looking for. It stands for copy and it does just that. The command that would work for you in this situation is

This would copy the source/ folder and all of its sub-directories to destination/ .

If destination/ doesn't exist, it will be created.

The -R flag stands for recursive, which tells cp to copy all sub-directories.

@Edmuntu Glad I could help, if you found the solution to your issue, can you please mark the answer that worked for you as the correct one by clicking the check mark under it? This will help alert others who view the page of the question being solved.

I find it easier to change to the directory I'm copying from first. For this example change to a directory everyone has called /boot . Anyone can copy and paste the commands below into their Terminal.

cd /boot sudo mkdir /boot_backup sudo cp -r . /boot_backup du /boot_backup -h 752K /boot_backup/extlinux/themes/debian-wheezy 756K /boot_backup/extlinux/themes 832K /boot_backup/extlinux 2.5M /boot_backup/grub/i386-pc 20K /boot_backup/grub/locale 2.3M /boot_backup/grub/fonts 7.2M /boot_backup/grub 565M /boot_backup 

For the cp command the current directory is identified as . which is the /boot directory we changed to. The -r option makes it recursive to include all sub-directories.

To ensure it worked run du to list all sub-directories and total file sizes in the new directory /boot_backup in this case.

After finishing this walk-through, use: sudo rm -r /boot_backup to remove the new directory and it's sub-directories.

Источник

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