Rsync copying current directory with name

13,862

Solution 1

I like your script but, if needed, you can do it by command line directly from the current directory:

rsync -a "$PWD" ~/backup/

or in a way similar to your script approach with

rsync -a "$(pwd -P)" ~/backup/

Notes:

  • It is needed to quote the current directory if in the path is present, for example, one or more spaces.

  • In case of symbolic links in the path it is possible to obtain the physical path avoiding all the symlinks specifying the option -P in the pwd command invocation ($(pwd -P)), or calling the executable with its full path ($(/bin/pwd)).
    Indeed there exists the built-in pwd that by default shows the symlinked path, and the executable /bin/pwd that by default shows the physical path.

  • Both commands refer to the variable $PWD that contains the the present working directory when they are asked for the version of the path with the eventual symlinks: so if you do not strictly need the physical path, you can avoid to call the subshell and use directly the variable $PWD.

    rsync -a "$PWD" ~/backup/
    

Solution 2

Found a solution:

Created a new shell script like this:

current_dir=`pwd`
dir_name=`basename $current_dir`
rsync -a . ~/backup/$dir_name

and when executing this it will create a new directory at the destination and copy current folder contents.

Share:
13,862

Related videos on Youtube

B.A.B
Author by

B.A.B

Updated on September 18, 2022

Comments

  • B.A.B
    B.A.B over 1 year

    I want to copy current directory to my backup directory:

    I tried this command rsync -a . ~/backup/ but it just copies the current directory contents instead of creating new directory at the destination.

    I know creating new directory at the destination end can be achieved by avoiding the trailing slash in source path. But it does not work in my case since I am using a period(".") instead of directory name.

    I want rsync to create a new directory at the destination path with name same as the current directory. How can I achieve this?

  • Hastur
    Hastur about 9 years
    Clear and understandable script. Usually you can find the suggestion to use the syntax current_dir=$(pwd) instead of current_dir=`pwd` for compatibility reasons even if they perform the same action.
  • B.A.B
    B.A.B about 9 years
    This is better solution..! Actually I just wanted to create an alias like ("backupme") So I can easily take backup of current directory by executing that command. This command is very useful and now I am going to remove that script and use this instead. Thank You..!
  • B.A.B
    B.A.B about 9 years
    This command works! But when I am creating an alias and executing that alias it just takes the backup of my home directory instead of current directory.
  • Hastur
    Hastur about 9 years
    Try to use single quote ' in the alias definition: alias BACKUPNOW='rsync -a $(pwd) ~/backup/'
  • Hastur
    Hastur about 9 years
    Examples often are better than words. It is not in aliasing, it is in bash. ;) Till you are curious do a wide use of man bash (or another command) or help alias if the command is a built-in... Ok man bash is a wide output to start with... but in general you can do it.
  • B.A.B
    B.A.B about 9 years
    Oh! when I use double quotes it expanded inside the alias definition okay understood. Thanks for your help
  • Navid Ht
    Navid Ht about 8 years
    Thanks, this works great. But when path of current directory contains space it breaks. So it's better to always use pwd with double quotes "$(pwd)".
  • Hastur
    Hastur about 8 years
    @NavidHt Thanks for the spot. Updated. Note that you can avoid at all the subshell with the use of the variable PWD, "$PWD".
  • tsukimi
    tsukimi over 6 years
    I had to add / to "$PWD" for it to work for delete as well