Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Several.
Filenames with whitespace in them will be the bane of your life - FWIW I change them all to underscore.
Mangling the string that represents the filename is merely the first step - you still need to feed it all to mv. Better option is to see if you have the perl version of rename - it can do the whole deal, for all files, in a single command with a little regex.
Using "$IN" and "$OUT", you can likely build a "mv" command to rename the files to the new format.
But... as someone pointed out, spaces in filenames will drive you crazy. I often run a script on the files or directory tree to replace spaces with underscores. Downside? Yes, Occasionally, I'll find that some of the files refer to the "spaced" version of other files and/or directories. Fixing those when encountered, though, seems easier than having to account for spaces in every single utility I have to write to deal with them. Maybe take this as an opportunity to make a new format like, say:
Code:
Author_Name--The_Title_of_the_Book or
Author_Name:The_Title_of_The_Book
Last edited by rnturn; 06-09-2022 at 11:25 PM.
Reason: Typos
Better option is to see if you have the perl version of rename - it can do the whole deal, for all files, in a single command with a little regex.
I, too, would turn to the perl version of rename since it can do both Perl regular expressions and perl expressions (scripting) in the transformation of the name:
# For recursion
# topdirectory=$HOME/some/directory
# cd $topdirectory
# shopt -s globstar
# for f in **/*.epub; do
for f in *.epub; do
[[ "$f" =~ ([^(]+) (\([^)]+\))(.*) ]] && echo mv "$f" "${BASH_REMATCH[2]} ${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
done
Remove the 'echo' if happy.
PS - Damn this new code parsing stuff at LQ (It just swapped in a hyphen for tilde). "$f" =~ should read "$f" ="tilde character"
# For recursion
# topdirectory=$HOME/some/directory
# cd $topdirectory
# shopt -s globstar
# for f in **/*.epub; do
for f in *.epub; do
[[ "$f" =~ ([^(]+) (\([^)]+\))(.*) ]] && echo mv "$f" "${BASH_REMATCH[2]} ${BASH_REMATCH[1]}${BASH_REMATCH[3]}"
done
Remove the 'echo' if happy.
PS - Damn this new code parsing stuff at LQ (It just swapped in a hyphen for tilde). "$f" =~ should read "$f" ="tilde character"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.