LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 01-22-2024, 05:25 AM   #1
DynV
Member
 
Registered: Feb 2020
Location: Montreal, Canada
Distribution: Ubuntu 22.04.2
Posts: 51

Rep: Reputation: 1
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
  1. renaming the original with an identifier of the main drive at the end then
  2. changing the parts of the playlist pointing to the old drive to point to the new drive then finally
  3. 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.

Thank you
 
Old 01-22-2024, 10:25 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
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?
 
Old 01-23-2024, 01:52 PM   #3
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: Slackware, Termux
Posts: 795

Rep: Reputation: 255Reputation: 255Reputation: 255
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.
 
Old 01-23-2024, 03:51 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,824

Rep: Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213Reputation: 1213
Talking

Quote:
Originally Posted by jayjwa View Post
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!
 
Old 01-23-2024, 05:03 PM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by DynV View Post
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:
  1. find <path> -type f -not -iregex '.*\(main\|backup\)_drive.xspf' -print0 | \
  2. while read -d $'\0' fn
  3. do
  4. if grep -m 1 'path_to_main' "${fn}'
  5. then
  6. <do copy/rename file & replacement of paths>
  7. fi
  8. 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

Last edited by goumba; 01-23-2024 at 05:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bulk rename all 'filename.mp3?NonsenseString' to 'filename.mp3' iwtbf Linux - Newbie 4 08-07-2017 06:12 PM
[SOLVED] Bash script: extract filename from txt and rename files Lexus45 Programming 6 11-01-2015 10:44 AM
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
if and sed - how to check character and then replace depending on result redhatuser1 Linux - Newbie 36 10-28-2010 07:03 AM
forward mail to different accounts depending on the content MaKess Linux - Server 1 05-10-2009 01:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration