Script to create softlink for specific file in every dir

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
c.monty

Script to create softlink for specific file in every dir

Post by c.monty »

Hello!

My music library is organised in this structure:
/music/<artist>_<album>/

In every directory there's a file named
front_<artist>_<album>.jpg

The script should search in every directory for this file and create a softlink front.jpg on it.

Can you provide some input for the syntax of a bash script?
That would be highly appreciated.

THX
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Script to create softlink for specific file in every dir

Post by xenopeek »

Not so hard, just one command:

Code: Select all

find . -iname front_\*.jpg -type f -printf "\"ln -s '%f' '%h/front.jpg'\"\n" | xargs -l bash -c
Open a terminal on the music directory, and run it from there. It will find all the files beginning with front_ and ending in .jpg (case-insensitive, make it -name instead of -iname to make it a case-sensitive search). At the directory where it found those it will create a symbolic link to each and call it front.jpg. If you are in doubt, first only run the first part:

Code: Select all

find . -iname front_\*.jpg -type f -printf "\"ln -s '%f' '%h/front.jpg'\"\n"
It will give you a list of all the commands it will run to create the symbolic links.

Hack on :D
Image
Locked

Return to “Scripts & Bash”