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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

I am creating a sh script and I need some help on how to make symlinks...

I have a folder /mnt/e/xxxxxx/ which contains several rar files. I also have /mnt/d/xxxxxx/ and I want to symlink all rar files (*.r*) to /mnt/d/xxxxx/... And I also want to rename them at the same time. So original.r01 becomes new.r01 and original.r02 becomes new.r02...

How can I do this?

I cant get ln -s to do it so I was thinking I might need to do a loop to do a ls | grep .r and then get one filename at the time and link it. But I am new to scripts and I am unsure how to do a loop to do that.

I got the script to work I just have one problem now. Some times winrar will name files like part01.rar, part02.rar, part03.rar and so forth. I cant get it to work when they are named partxx.rar... Here is my script so far...

That doesn't change much, just use .r?? in your glob. Also to turn off case sensitive globbing use shopt -s nocaseglob

I'm having a hard time trying to figure out what you want to do. I suspect you you have files and some are named bla.bla.partxx.rar and some are named bla.bla.part01.rxx. The way it looks to me you want to rename all the files bla.bla.part01.rxx to bla.bla.partxx.rar. And also make a symlink. Then for the files with the rar extension you just want the symlink?

The file that I already have is /mnt/e/glftpd/site/TV-HRHD/Lost.S02E01.HR.HDTV.AC3.5.1.XviD-NBS/lost.s02e01.hr.hdtv.xvid.nbs.partXX.rar and it should be linked to the new location /mnt/tv/tv/TV/Latest/Lost.partXX.rar....

Also .rXX shouldnt be renamed to partXX.rar... It should still keep the original file extension, the only thing I want to change is the name ("lost.s02e01.hr.hdtv.xvid.nbs" to just "lost")...

I tried the script now, the good part is that linking .rXX files will now work. With the exception of a bit being added to the end linking /mnt/e/glftpd/site/TV-HRHD/Las.Vegas.S03E01.HR.HDTV.AC3.5.1.XviD-CTU/Las.Vegas.rar with /mnt/tv/tv/TV/Latest/Las.Vegas/ctu-lv301.part00.rar....

When it comes to the partXX.rar part it doesnt work at all, it will link as following:
/mnt/e/glftpd/site/TV-HRHD/Lost.S02E01.HR.HDTV.AC3.5.1.XviD-NBS/lost.s02e01.hr.hdtv.xvid.nbs.part01.rar to /mnt/tv/tv/TV/Latest/Lost/lost.s02e01.hr.hdtv.xvid.nbs.part01.rar

edit: Just to be sure I will post a list of all the files in /mnt/e/glftpd/site/TV-HRHD/Lost.S02E01.HR.HDTV.AC3.5.1.XviD-NBS

Remember that the script will run on all kinds of folders, not just the two examples I have provided. So the script has to first check what situation it is and the run the appropriate actions...

If thats the case you'll need to set up some type of config file which you can edit to add/remove paths and filenames in. I was bored so I went ahead and wrote something that you could use as a starting point and modify as you need.

The script symlink reads a config file symlink.conf which contains 5 entries:
# Copy this to symlink.conf and uncomment the examples or create your own.
# situation = tells program wether to rename the extension when linking.
# FROM = Directory where files are stored.
# TO = Directory where simlinks will be made
# ORIGINAL = The part of the original filename to be removed
# NEW = This will replace the ORIGINAL string in new symlink filename
# situation FROM TO ORIGINAL NEW
# Example 1
# 1 /mnt/e/dir2/show1 /mnt/tv/show/Lost lost.s02e01.hr.hdtv.xvid.nbs Lost
# Example 2
# 2 /mnt/e/dir1/show1 /mnt/tv/show/LasVegas ctu\-lv301\.r?? Las\.Vegas\.part
# Example 3
# 1 /mnt/e/dir1/show2 /mnt/tv/show/Simpsons name.of.show-S03E01 Simpsons
# Example 4
# 2 /mnt/e/dir2/show2 /mnt/tv/show/AnotherWorld S02E01\.r?? part

So we have both types of situations here. In the 2 #1-Situations you have partXX.rar files, so all we do is rename the symlinks. We also have 2 #2-Situations where we have .rXX + one oddball .rar files, in this situation we have to rename the file extension after we increment the sequence by 1 so we have room for the oddball file with the .rar extension.

KNOWN BUGS:
As you can see the script works: It will give an error if you designate a FROM directory as situation2 and it has more than 1 .rar file.If there are more than one .rar in a situation2 directory files it will only find and link the one which matches the other files in the directory.

# Symlink config file.
# situation = tells program wether to rename the extension when linking.
# FROM = Directory where files are stored.
# TO = Directory where simlinks will be made
# ORIGINAL = The part of the original filename to be removed
# NEW = This will replace the ORIGINAL string in new symlink filename
# situation FROM TO ORIGINAL NEW
# Examples
# 2 mnt/e/dir1/show1 mnt/tv/show/LasVegas ctu\-lv301\.r?? Las\.Vegas\.part
# 1 mnt/e/dir1/show2 mnt/tv/show/Simpsons name.of.show-S03E01 Simpsons
# 1 mnt/e/dir2/show1 mnt/tv/show/Lost lost.s02e01.hr.hdtv.xvid.nbs Lost
# 2 mnt/e/dir2/show2 mnt/tv/show/AnotherWorld S02E01\.r?? part

<edit> The comments about troubleshooting are for trying to get your ORIGINAL and NEW strings set in the config file. Just comment the line with the ln command and uncomment the echo command and you can run the script untill you figure out how to make it work the way you want.

If I want to link a .partXX.rar situation to .rXX, how would I do this? Also note that there is not .part00.rar... Winrar will start the first file of with .part01.rar... It is just in the "old" style (.rXX) that it uses 00...

Also, a great feature would be if it unlinked all the previous files beforing linking, using -f in ln would overwrite if it exists. But the problem would arise if there is 49 rar files but 50 rar files the in old one. This would remove all old 49 but leave an old file called .part50.rar....

I did some testing of winrar and a problem accured. For some reason it isnt possible to rename .rXX files to .partXX.rar, winrar will not open them. However it is possible to rename .partXX.rar files to .rXX. So I think to goal should that they should end up in the TO path as .rXX always regardless of previous format.
Thanks!

Also, a great feature would be if it unlinked all the previous files beforing linking, using -f in ln would overwrite if it exists. But the problem would arise if there is 49 rar files but 50 rar files the in old one. This would remove all old 49 but leave an old file called .part50.rar....

If I want to link a .partXX.rar situation to .rXX, how would I do this? Also note that there is not .part00.rar... Winrar will start the first file of with .part01.rar... It is just in the "old" style (.rXX) that it uses 00...

Thats the nice thing about the config file. Now you can create more new situations and add them to the script.

And the config file wouls look something like this:
Using the lost example...
To link lost.s02e01.hr.hdtv.xvid.nbs.part00.rar to Lost.r00
symlink.conf
3 mnt/e/dir2/show1 mnt/tv/show/Lost lost.s02e01.hr.hdtv.xvid.nbs.part Lost.r

NOTE: I havent tested this, but I'll do that tomorrow. Gotta get up early to go to work, need sleep.... zzzzzzz

Sorry I have been swamped with stuff to do this week. I finally had some time to test the script and it looks good. However the only problem with linking .partXX.rar to .rXX is that the oddball .rar is not created...

.part01.rar needs to be symlinked to .rar
and part02.rar needs to be symlinked to .r00
and part03.rar needs to be sylinked to .r01

and so forth.

Any quick fix to solve this?

Also I am getting confused, I thought that it was possible to link .rXX to .rXX but I cant get it to work now, did I mess something up or was I dreaming that it worked? :P