Bulk renamer shell script - Programming On Unix
Users browsing this thread: 1 Guest(s)
|
|||
I have just written an interactive POSIX shell script for bulk renaming files.
I think it might be useful for more people. When you call `bulkrename *`, it opens your favorite editor with the names of the files in the current directory for you to edit. Then, after closing the file, it renames the files to the names you edited. `ls | bulkrename` also works. Code: #!/bin/sh bulkrename(1) receives filenames from its arguments (one per argument) or from stdin (one per line) and opens an editor to edit them; then, it renames each file to the corresponding edited one. If there are no arguments, bulkrename operates on stdin, reading one filename per line. If there are arguments, each argument is read as a filename. The environment variables $EDITOR and $VISUAL are checked, in this order, for an editor program. If both are unset, use vi(1) by default. |
|||
|
|||
(19-04-2020, 12:06 PM)phillbush Wrote: I have just written an interactive POSIX shell script for bulk renaming files. It is always good to have more alternatives to GNU rename. (Here's mine, not a shell script though.) (19-04-2020, 12:06 PM)phillbush Wrote: If both are unset, use vi(1) by default. Whichever default you choose, it is safe to assume that your first actual user will have a system which does not support that. ----------------- Looks like your script does not support directories though: Code: + mv TECOC TECOC -- <mort> choosing a terrible license just to be spiteful towards others is possibly the most tux0r thing I've ever seen |
|||
|
|||
(20-04-2020, 08:04 AM)jkl Wrote: Looks like your script does not support directories though: It does work with directories, but you tried to rename a directory to itself. I will fix that, thanks for reporting the issue! EDIT: Here is the new, fixed version. I just had to play with the read shell built-in and with file descriptors: Code: #!/bin/sh |
|||
|
|||
This is not something I do quite often to be fair, so I never really made a dedicated script for it. This falls into the "one-shot scripted operation" for me, which is pretty close to what your script is doing actually.
When faced with your problem, I generate a script and run it afterwards like so: Code: find . -type f -exec printf 'mv "%s" "%s"\n' {} {} \; | vis - | sh There's no safety net of course, and I assume that "rename samename samename" will generate errors, but I'll ignore them as the script will do what I want anyway. The advantage is that you seen the actual script that will be executed, rather than having the "mv" command added afterwards. What You See Is What You Run, hehe ! That is a good use of `cmp` there so kudos for that ! You don't get to use it that often ;) |
|||
|
|||
(20-04-2020, 10:21 AM)z3bra Wrote: When faced with your problem, I generate a script and run it afterwards like so: That's basically what my script does, but with more tests. Basically, I run this one liner as: Code: ls | bulkrename I also use bulkrename as a command in my filemanager, lf, so I visually select the files to rename and type `:bulkrename`. (20-04-2020, 10:21 AM)z3bra Wrote: That is a good use of `cmp` there so kudos for that ! You don't get to use it that often ;)Yeah, most people don't know cmp and use diff for that cases... |
|||
|
|||
I finally remember what your tool made me think of ! vidir(1) from moreutils !
It opens the "content" of a directory in $EDITOR, end "commits" the changes when you quit, which means that your can rename entries, delete them or create new ones. And btw, moreutils should be installed everywhere, be it only for "sponge(1)"… |
|||
|
|||
-- <mort> choosing a terrible license just to be spiteful towards others is possibly the most tux0r thing I've ever seen |
|||
|
|||
(20-04-2020, 03:46 PM)jkl Wrote: Tested with Schily's latest bosh shell: Works just fine. :) Thanks for testing! I'm really thankful of that. (20-04-2020, 03:45 PM)z3bra Wrote: And btw, moreutils should be installed everywhere, be it only for "sponge(1)"… Moreutils is indeed very useful. I use ifne(1) for a dmenu of manpages. It makes zathura not open if the input is empty. Code: man -k . | sort -t ' ' -k2,2 -k1,1 | dmenu -i -l 8 -p man: | sed -E 's/^([^ ]+) \(([^ ]+)\).*/\2 \1/' | xargs -r man -Tpdf | ifne zathura - I had no idea of the existence of vidir(1), even though I have moreutils installed. I only have moreutils installed for ifne, but now, after you have opened my eyes, I will check the other utilities of its. For sponge(1), I have yet another shell script that do changes inplace: Code: #!/bin/sh So the following two one-liners are equivalent: Code: inplace file sed ... inplace(1) is based on overwrite(1), a script I found on the book The UNIX Programming Environment. |
|||
|
|||
(20-04-2020, 03:45 PM)z3bra Wrote: I finally remember what your tool made me think of ! vidir(1) from moreutils ! I originally installed moreutils years ago precisely because I wanted something like sponge. But oddly enough, the thing from the suite that I've used more than any other is vidir. Next most used is probably parallel, which I prefer over the GNU utility by the same name. (Re moreutils-parallel vs. GNU-parallel, see this interesting Debian bug report & discussion: https://bugs.debian.org/cgi-bin/bugrepor...bug=597050. The final message from Joey Hess is particularly amusing.) |
|||