Vinothkumar
Vinothkumar

Reputation: 13

Is there option in cp command to reflect source path as destination path?

If source and destination path are being same, how to skip re-typing it? For example, I need to take a back-up of a file or rename it:

# taking back-up
$ cp ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img

# renaming it
$ mv ~/project/uboot/u-boot.img ~/project/uboot/ver1-u-boot.img

Upvotes: 1

Views: 328

Answers (2)

MrAlias
MrAlias

Reputation: 1346

It's all about the curly braces!

$ cp ~/project/uboot/{,ver1-}u-boot.img

or to be more verbose

$ mv ~/project/uboot/{u-boot.img,ver1-u-boot.img}

The shell will reproduce what you have explicitly written in your question, which means you can write out the full path once. Here's a good link for further reading.

Upvotes: 2

lab419
lab419

Reputation: 1258

How about

( cd ~/project/u-name-it; cp_or_mv file ver1-file )

or

base=base-path cp_or_mv $base/file $base/ver1-file

However this very much looks like a use case for a script or function to me. As these can become quite sophisticated of course (e.g. increasing the version would be nice) I prefer to give a simple example more as an incentive ;)

# Untested, test well before using; should work in zsh & bash IFIAC
# usage:
#   bup directory [files...]
function bup
{
     local dir=$1
     shift
     for file; do
       cp $dir/$file $dir/ver1-$file
     done
}

HTH Robert

Upvotes: 1

Related Questions