33

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

58

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:

man cp

or

man mv

To view the manual pages

3
  • 1
    thanks! but typing out the full file path is difficult. is there a better way?
    – BenKoshy
    Jan 19, 2016 at 23:16
  • 1
    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. Aug 23, 2019 at 2:41
  • 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.
    – Alexey
    Dec 14, 2020 at 7:48
3

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 <= $(cat ~/.numToCopy); i++))
    do
        # get the nth path
        pathToCopy="$(xclip -o | head -$i | tail -1)"

        if [ -d "$pathToCopy" ] # If it's a directory
        then
            cp -r "$pathToCopy" .
        else
            cp "$pathToCopy" .
        fi

        # if it was marked as a cut operation
        if [ $(cat ~/.copyOrCut) = "cut" ]
        then
            # delete the original file
            rm -r "$pathToCopy"
        fi
    done
}

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.

0

Go to that directory from where you want to copy file i.e /usr/bin/octave/3.2.4/m/miscellaneous

cd /usr/bin/octave/3.2.4/m/miscellaneous

Then type

`cp mkoctfile.m ../../../mkoctfile-3.2.4`

../../../ means you are going backward to bin folder and type whatever directory in which you want to copy your file.

0

An interesting solution based on @M Imam Pratama's idea (Copy / Cut now and paste later).

Consider using xclip-copyfile, xclip-cutfile and xclip-pastefile.

$ xclip-copyfile file1
$ xclip-cutfile file1
$ xclip-pastefile file1

You can also copy an entire tree structure.

WARNING: xclip-cutfile immediately remove the file, does not wait until you paste it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .