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 ...
- 11-10-2008 #1Just 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?
- 11-10-2008 #2
You can do:
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.Code:cd /usr/share/postgresql cp pg_hba.conf.sample pg_hba.conf cd -
Linux user #126863 - see http://linuxcounter.net/
- 11-10-2008 #3Linux 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:
This function is used this way:Code:function mycp() { dest_dir="${1%\/*}" cp "$1" "${dest_dir}/$2" }
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.Code:mycp /home/i92guboj/foo/bar.sample bar
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".
- 11-10-2008 #4Code:
cp /usr/share/postgresql/{pg_hba.conf.sample,pg_hba.conf}
- 11-10-2008 #5Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Indeed, smart trick
- 11-10-2008 #6Linux 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
- 11-10-2008 #7Linux 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.
- 11-10-2008 #8Just Joined!
- Join Date
- Nov 2008
- Posts
- 2
Million thanks!!!
Thanks a lot for your suggestions, folks! Live is so much easier now!


