I frequently edit startup files such as my .bashrc file
Then I copy them to another direction such as ~/Dropnot/level1/setups/bash1

Is there any way I can cp such files to the above location with something like an alias?
I currently have an alias to cd to it with alias n='cd ~/Dropnot/level1/setups/bash1'

I'd like something that would handle any file I pass into it.


Oh yes, you can!

  1. Open your ~/.bash_aliases file and type the following to the end of the file(create a new ~/.bash_aliases if it doesn't exist):

    alias mycp='cp ~/.bashrc ~/Dropnot/level1/setups/bash1'

    This will create an alias mycp(you can give a different name for mycp)which will copy your ~/.bashrc file to the desired location.

  2. You could create a shell variable which contains the long path and then use the variable in place of the long path. For example, in your ~/.bashrc:

    export fav_path=/usr/share/help/nl/gnome-help/figures/

    and source ~/.bashrc and then use

    cp ~/.bashrc "$fav_path"

Remember to use the " for paths containing spaces.

  • +1 That's useful. I am looking for something more generic. – Michael Durrant Apr 20 '14 at 11:46
  • @MichaelDurrant: Added an alternative method, couldn't think of a more generic method :/ – jobin Apr 20 '14 at 13:00
  • Those both work. In the end I ended up using an alias to do all of what I really needed, i.e. alias bup='cp ~/.bashrc ~/Dropnot/setups; cd ~/Dropnot/setups; git add .bashrc; git commit -m".bashrc update"; git push origin master;' ! – Michael Durrant Apr 20 '14 at 13:02
  • Then I also made a bdown command to do alias bdown='cp ~/Dropnot/setups/.bashrc ~; . ~/.bashrc' – Michael Durrant Apr 20 '14 at 13:03

If you have GNU cp (on Linux is answer is most likely yes), you can use the -t option:

alias fcp="cp -t ~/Dropnot/level1/setups/bash1"

Doing fcp somefile will always put the file in the ~/Dropnot/level1/setups/bash1 directory. Multiple arguments will work as well (eg fcp somefile otherfile1 will both go to ~/Dropnot/level1/setups/bash1)

If GNU cp is not available, you can always use a function:

fcp () {
  cp "$@" ~/Dropnot/level1/setups/bash1

Do you mean something like this:

alias c='cp ~/.bashrc ~/Dropnot/level1/setups/bash1'

I would suggest to instead move the file there and use a symbolic link to avoid the copy altogether:

mv ~/.bashrc ~/Dropnot/level1/setups/bash1/
ln -s ~/Dropnot/level1/setups/bash1/ ~/.bashrc

I've been using this for my dotfiles for years, and it's been very helpful in keeping a backup.

  • I believe you the OP meant the ~/.bashrc file. So you'll always need to provide the ~/.bashrc file. Simply .bashrc will try to locate .bashrc in the current directory where the OP is. – jobin Apr 20 '14 at 10:10
  • I'm trying to avoid having to type out the long path that way and use something shorter, similar to the way aliases help. – Michael Durrant Apr 20 '14 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.