Find the answer to your Linux question:
Results 1 to 8 of 8
Hi folks, I am trying to copy a file from a remote (non-current) directory to the same directory, just changing the name: cp /usr/share/postgresql/pg_hba.conf.sample /usr/share/postgresql/pg_hba.conf Is there a way to ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2

    [SOLVED] cp from remote directory to same one with less typing?

    Hi folks,

    I am trying to copy a file from a remote (non-current) directory to the same directory, just changing the name:

    cp /usr/share/postgresql/pg_hba.conf.sample /usr/share/postgresql/pg_hba.conf

    Is there a way to avoid typing the full path to the same directory? If I was to copy it into the current directory, I could use a "." sign, but how about a remote one?

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    You can do:

    Code:
    cd /usr/share/postgresql
    cp pg_hba.conf.sample pg_hba.conf
    cd -
    It doesn't seem much less typing, though, your original should be quite easy if you're using TAB to complete paths and file-names.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Not as such (that I know of). But you could use a bash function for that:

    Code:
    function mycp() {
      dest_dir="${1%\/*}"
      cp "$1" "${dest_dir}/$2"
    }
    This function is used this way:

    Code:
    mycp /home/i92guboj/foo/bar.sample bar
    The result will be a copy of /home/i92guboj/foo/bar.sample into /home/i92guboj/foo/bar. I think that's what you wanted, if not just let me know.

    You can put this function in your ~/.bashrc and/or your ~/.bash_profile file(s). Create them if they do not exist. For them to load, you will have to exit your current shell and open a new one, or just reload it with "exec bash".

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Code:
    cp /usr/share/postgresql/{pg_hba.conf.sample,pg_hba.conf}

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Indeed, smart trick

  6. #6
    Linux Newbie
    Join Date
    Sep 2007
    Posts
    161
    coopstah13, that's way cool!
    where is this documented? is it specific to cp or sh or bash or something else?

    cheers, kai

  7. #7
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    It's a shell feature. Bash does many kind of expansions, this one is called brace expansion. You can read about it in the bash man page.

    The basic usage that we see here expand a given string into many different ones, each of them uses one of the substrings contained within the braces.

    So, "foo{1,2}bar" is expanded to "foo1bar foo2bar". The ingenious thing about this concrete case is that cp considers the last argument as the destination, regardless of how many parameters you give to it. So, that's probably the shorter way to do it.

  8. #8
    Just Joined!
    Join Date
    Nov 2008
    Posts
    2

    Million thanks!!!

    Thanks a lot for your suggestions, folks! Live is so much easier now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...