Script to rename and replace content depending on filename end
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Script to rename and replace content depending on filename end
I make VLC playlists (.xspf) on my main drive in the same directory as the media contained in those lists. After a while I move everything to a backup drive. Sometimes I make a variation of those playlists pointing to the new drive by
renaming the original with an identifier of the main drive at the end then
changing the parts of the playlist pointing to the old drive to point to the new drive then finally
save the modified content to a name beginning the same but its end is instead an identifier of the new drive;
both file name drive identifiers (at the end) start the same way, with a '-', eg -main_drive.xspf & -backup_drive.xspf
I'd like for the process to be automated, something like: For playlists ending with neither drive identifier, check if one of its location tag content start with the path for the main drive (<location>/path/main/drive/), and if so start by renaming it to identify its for the old drive, then replace the location tag to point to the new drive (<location>/path/backup/drive/), then save the modified content with a name ending with an identifier of the backup drive.
Just to understand, have you written any scripts in the past?
Shell scripts are mostly sequences of commands you can enter into the command line.
One challenge is to derive a command that detects the source drive of a file, and be able to use that in the command to rename the file.
Typically as I develop something like this, I iterate on ideas and variations and what I do is to not rename the file, instead I copy it to the final name, so my input is not modified yet until I've refined my command options and choices.
What's your inklng here? Do you plan to start some form of script or sequence of commands to use?
This would be a good place for Python. The "os" module has alot of helpful things for this. You could use "os.listdir()" and walk thru the directory. The "re" module can help you match files.
This would be a good place for Python. The "os" module has alot of helpful things for this. You could use "os.listdir()" and walk thru the directory. The "re" module can help you match files.
Perl!
Haven't understood your problem yet, but you can solve it in Perl!
both file name drive identifiers (at the end) start the same way, with a '-', eg -main_drive.xspf & -backup_drive.xspf
I'd like for the process to be automated, something like: For playlists ending with neither drive identifier,
Quote:
check if one of its location tag content start with the path for the main drive (<location>/path/main/drive/),
grep,
Quote:
then replace the location tag to point to the new drive (<location>/path/backup/drive/),
sed
Quick and dirty,
Code:
find <path> -type f -not -iregex '.*\(main\|backup\)_drive.xspf' -print0 | \
while read -d $'\0' fn
do
if grep -m 1 'path_to_main' "${fn}'
then
<do copy/rename file & replacement of paths>
fi
done
(1) finds files any playlist without your drive identifier, -print0 will delimit with null for funky file names
(2) goes through the resulting list
(4) check if the playlist has lines pointing to the main drive, -m 1 quits after first match (we only want to know if it's there or not)
(8) do whatever you want to the file
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.