Relative path to source when copying or moving

8,313

This should do the trick:

mv long/path/to/a/folder{,.old}

Reference: search for Brace Expansion in bash manpage.

Share:
8,313

Related videos on Youtube

Alaa Ali
Author by

Alaa Ali

I walk, I talk, I sit, I sleep. Repeat.

Updated on September 18, 2022

Comments

  • Alaa Ali
    Alaa Ali over 1 year

    This might be trivial, but here goes.

    In terminal, I tend to find myself moving/renaming/copying files that are not in my current working directory instead of cd-ing to the directory of those files first. For example, I find myself doing:

    mv long/path/to/a/folder long/path/to/a/folder.old
    

    Sometimes, this can be a "long/path with spaces/to/a/folder".

    My question is: is there a way/shortcut for the <target path> to be relative to the <source path>? For instance, does something like this exist:

    mv long/path/to/a/folder ``/folder.old
    

    Where `` means "the same path or the same parent path of the file/folder I'm trying to move", so in my example `` would stand for long/path/to/a/.

    I know I could of course cd long/path/to/a/ and then mv folder folder.old, but that involves an extra command, and I'll end up in another working directory.

  • don.joey
    don.joey almost 11 years
    Beautiful. Is there also an alternative solution with arguments?
  • Eric Carvalho
    Eric Carvalho almost 11 years
    @Private I don't get it. Can you provide an example?
  • Eric Carvalho
    Eric Carvalho almost 11 years
    @Private I don't know if that's possible somehow. Surely it isn't possible using positional parameters ($0, $1, ...).