0

I need to rename a file in a multi-nested directory, that's very long location; I'll use mv command.
I'd like to save time by typing only once the full location.

Is there a way to shorten the destination directory when moving a file?

I'll explain it better with an example:
mv /dir1/dir2/dir3/dir4/dir5/file.txt /dir1/dir2/dir3/dir4/dir5/moved_file.txt ---> mv /dir1/dir2/dir3/dir4/dir5/file.txt moved_file.txt

mattia.b89
  • 3,142
  • 2
  • 14
  • 39
  • if you `cd` to the destination directory, you don't need to provide the full path of the destination file. – aviro Nov 03 '22 at 17:15
  • 1
    See [How can I rename a file in a nested path "in place," i.e. without re-typing the full path?](https://unix.stackexchange.com/questions/586018/how-can-i-rename-a-file-in-a-nested-path-in-place-i-e-without-re-typing-the) – steeldriver Nov 03 '22 at 17:16

2 Answers2

0

In the absence of a programmatic method of identifying the path and the filename....

SRC=/dir1/dir2/dir3/dir4/dir5
DEST=/dirA/dirB/dirC/dirD

mv "$SRC"/file.txt "$DEST"/moved_file.txt

Or, for just a rename...

mv "$SRC"/file.txt "$SRC"/moved_file.txt
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
symcbean
  • 5,008
  • 2
  • 25
  • 37
0

Use cd to change the working directory to /dir1/dir2/dir3/dir4/dir5, then call mv. Do it in a sub-shell to avoid having to cd back to the original directory (the change of working directory is local to the (...) sub-shell).

( cd /dir1/dir2/dir3/dir4/dir5 && mv file.txt moved_file.txt )

The && additionally causes the mv not to be performed if the cd fails for whatever reason.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936