[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
mv command usability: rename file and create dest dir
From: |
Sergey Ponomarev |
Subject: |
mv command usability: rename file and create dest dir |
Date: |
Sat, 1 Jul 2023 15:12:13 +0300 |
To rename a file a user need to use mv command and specify the DEST dir:
mv /some/very/long/path/file /some/very/long/path/
This makes it not so easy to use when typing a command but also makes a
script line longer.
Newcomers like students may not figure out quickly that renaming is
indeed moving.
What about simplifying this operation?
The first idea that came to mind is adding a new flag like -N to rename a
file:
mv -N /some/very/long/path/file file2
It will move to /some/very/long/path/file2
or move to subfolder
mv -N /some/very/long/path/file ./bak/file
It will move to /some/very/long/path/bak/file
The problem here is that the mv command may have accept multiple files to
move:
mv /dir1/file1 /dir2/file2 /destdir
So the -N flag should assume only one file which makes it not consistent.
Instead we may use some placeholder in dest dir that will represent a
folder of the original file.
For example we may use asterisk * which anyway makes no sense for dest dir:
mv /dir1/file1 *file.bak
It will rename /dir1/file1 to /dir1/file.bak
Moving to parent folder should be also be easy:
mv /dir1/file1 *../file.bak
It will rename /dir1/file1 to /file.bak
More complicated example with moving multiple files to subfolders:
mv /dir1/file1 /dir2/file2 */bak
It will move /dir1/file1 to /dir1/bak/file1 and /dir2/file2 to
/dir2/bak/file2
Exactly the same rule may be applied to cp command:
cp /dir1/file1 *file.bak
Currently to imitate the behaviour the easiest way is just to cd into
directory and make commands from it
cd /dir1/; cp file1 file.bak
Another option is to call basename to get the containing folder.
With the simple placeholder we can simplify and make shorter scripts. The
change would be easy to port to BusyBox or some "suckless" shells.
The problem remains when you want to change the file name or path itself.
Here the only option is to use regexps.
I found the rename program (apt install rename) that is indeed a Perl
script with renaming by a regexp. From it's man:
To rename all files matching "*.bak" to strip the extension:
rename 's/\.bak$//' *.bak
To translate uppercase names to lower:
rename 'y/A-Z/a-z/' *
This also looks good but it's not part of the coreutils. Using regexps may
be to bloat change but maybe it is worth considering.
In the list I found same mention of another rename command but it looks
like it wasn't added:
https://lists.gnu.org/archive/cgi-bin/namazu.cgi?query=rename&submit=Search%21&idxname=coreutils&max=20&result=normal&sort=score
Last but not least. One thing that is needed too often is to create a
destination folder if it doesn't exist.
In many scripts I have a lot of mkdir -p calls but maybe it would be better
to add the same flag to the mv and cp? The -p and -P are already used so
maybe -F (folder):
cp -F file1 /bak
It will create the /bak folder if it doesn't exist.
This looks like it violates some unix principle but if something needs so
often then it totally makes sense to include.
The coreutils has so many dependants so it's better not to change,
especially they devs may not know that the new flag is not supported yet on
a target OS.
But people anyway need some simple way to perform daily tasks.
Please let me know your thoughts.
Regards,
Sergey Ponomarev
- mv command usability: rename file and create dest dir,
Sergey Ponomarev <=