Search code examples
linuxconsolecommand-line-interfacecopy-paste

Linux console equivalent to gui copy/paste file/directory scenario


How to simply recreate copy/paste functionality like in gui environments?

My typical scenario for copying file/directory in Linux console is:

cp source_path target_path

Sometimes paths are relative, sometimes absolute, but I need to provide them both. It works, but there are situations where I would like to recreate scenario from gui which is:

1. go to source directory
2. copy file/directory
3. go to target directory
4. paste file/directory

I imagine something like

cd source_directory_path
copy_to_stash source_name
cd target_directory_path
paste_from_stash [optional_new_target_name]

I know that there is a xclip app, but a documentation says that it copies content of a file, not a file handle. Also, I can use $OLDPWD variable and expand it when I copy file, but this is not a solution without some cumbersome.

Is there some simple, general, keyboard only, not awkward to use equivalent?


Solution

  • I've also asked the same question on superuser and answer that I've received is good enough for me.

    In short: two additional scripts and temporary variable to hold intermediate value.

    Below is a code and link to original answer.

    #!/bin/bash
    # source me with one of:
    # source [file]
    # . [file]
    
    # Initialize
    sa_file=
    
    sa(){
    # Fuction to save a file in the current PWD
    if [[ -e "$PWD/$1" ]]; then
        sa_file=$PWD/$1
        echo "Saved for later: $sa_file"
    else
        echo "Error: file $PWD/$1 does not exist"
    fi
    }
    
    
    pa(){
    # Paste if file exists, to $1 if exists
    if [[ -e "$sa_file" ]]; then
        if [[ $1 ]]; then
            cp -v "$sa_file" "$1"
        else
            cp -v "$sa_file" .
        fi
    else
        echo "Error: file $sa_file does not exist, could not copy"
    fi
    }
    

    https://superuser.com/a/1405953/614464