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,238

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
  • 333,661
symcbean
  • 5,540
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
  • 333,661