Calling specific characters from a find variable

I'm trying to do something like this:

find . -name blablabla -exec  ln -s ./"{:53:14} blablabla" \;

The idea is find blablabla and create a symbolic link to it using part of it's path and then it's name, "blablabla."

I just don't know if I can call characters out of a find variable. Obviously the above doesn't work.

Basically, it'll be creating sylinks for every directory named blablabla like this:

hd_90_050 blablabla
hd_67_010 blablabla
hd_10_777 blablabla

Any help is appreciated.

With what you have shown us there is little hope of us being able to help you.

You seem to be saying that you want to extract 14 characters starting in position 53 from the 11 character string "./blablabla". We have no idea whether you are expecting other characters to come from subdirectory names, or if you have wildcard characters in "blablabla" that you aren't showing us, or something else.

If you tell us what operating system you're using, what shell you're using, show us some actual samples of the pathnames being produced by the command find . -name blablabla (in CODE tags), and show us exactly what ln commands you hope to produce from those pathnames (in CODE tags); we would have a much better chance of coming up with something that might accomplish what you're trying to do. Note that an ln -s command needs two operands; not just one (and your sample code seems to be trying to only produce one).

Please help us help you.

1 Like

I honestly do not quite understand what it is about, but can it help?

find $(pwd) -name "blablabla" | while read d; do ln -s $d ${d//\//_}; done

Ah, yes I see. Your assumptions are right. I do expect a long string to be returned from find, and I do need to extract characters from that long path and pass them to the ln command.
I'm using a bash shell on CentOS Linux.

find . -name blablabla -exec  ln -s  {} ./"{:53:14} blablabla" \;

I think that command is more like what I need. Find blablabla and create a symlink in this directory with a prefix of part of it's path.
The moderator put my desired results in "code," in the original post which made the post quite confusing. In my opinion.

Each folder needs the unique characters from it's path because each directory "blablabla" will have the exact same name.

I don't think the code suggested by nezabudka will work. I don't see it extracting characters :53:14 and piping them to ln.

Hopefully, I've cleared things up a bit.
Thanks in advance.

Give us some data to work upon - input data and desired results.
Your find command will definitely NOT work. Parameter expansion can't be used the way you wrote it.

1 Like

scribling
I showed a variant with a string variable from which you can extract any part of the substring

do ln -s $d ${d:5:4}

or change in this way

LESS=+/"Parameter Expansion" man bash

--- Post updated at 20:04 ---

And even so ))

do ln -s $d "./${d:5:4} "$(basename $d)
1 Like

That is good to know and will help us give you suggestions that will work in your environment IF you give us some representative sample data to use to verify that we understand what you're trying to do and to verify that the code we suggest will produce the results you want.

The code seems to be you're trying to extract 14 characters from the pathnames find finds, but your sample data shows that you only expect to get 9 characters. We can't come up with any sample data that will trim the 14 characters you could extract from any pathname that would create the 9 characters shown in the sample output you say you want to produce.

And, as has already been said three times before in this thread, the find utility does not perform variable expansions on pathnames it outputs as -exec primary operands. The above find command will attempt to link each found pathname to the literal name {:53:14} blablabla . I'm sure that isn't what you want, but your refusal to give us a self-consistent description of what you're trying to do and your refusal to give us representative sample data that you will be processing leaves us confused and only able to make wild guesses at what you really want to do.

The moderator who added the CODE tags to post #1 in this thread for you did not in any way add any confusion to what you had in your original post. Not having CODE tags makes leading and trailing <space> and <tab> characters disappear from view and coalesces sequences of <space> and <tab> characters in the middle of a line to single <space> characters. If you had had sequences of <space>s in your sample output, the CODE tags would have made that clear for us and then we might have been able to make sense of your sample output. Unfortunately, with or without the CODE tags, your sample desired output doesn't fit the description of the problem you say you're trying to solve.

Note also that this is the first time you've said anything about blablabla being a directory. There is nothing in your find command that restricts the pathnames it returns to be pathnames of directories. I have no idea whether or not this might cause you problems with the results your code will produce, but if it is a constraint on the output you want to produce, it would have been helpful to know this up front.

Unfortunately, you haven't.

I'm guessing that nezabudka has come closest to guessing at what you're trying to do, but her code won't quite work for you yet. Hopefully, it is close enough that you can work out the rest. If not, you MUST give some concrete actual sample pathnames that the find command I showed you in post #2 in this thread produces as output AND show us the exact ln commands you hope to produce from each of those sample pathnames.

Please help us help you.

1 Like

Thank you for taking the time to reply, but I really don't understand what the big confusion is.

I need to search several directories for a specific directory name, which will occur in every directory and then make a symlink in the working directory and append it's name with a part of the file name that will tell me where it came from. Those characters are 53:14 ... I'm very sorry my "example" was only nine - it was only an example as I said.

The deal is that these directories are 5 or 6 levels deep in each and I have to go to them often and I'm sick of digging through each folder each time. If I have smylinks in one directory that tells me where they came from I have a single click option to get to them.

I understand that find doesn't expand a variable or whatever which is why I'm asking for help on how to do it.

I'm sorry I haven't done this in a while so I'm a bit rusty.

I need the result of find to be set as a variable that I can extract 53:14 from and use it in the file name of the symlink.

I work in production. We have hundreds of shots and each one has a render directory. I would like to make one directory that has symlinks to all of them with their names which happen to be in the path at 53:14.
The final result would look exactly like this: BT_907_725_150 Renders

I don't think I can be any more clear than that.

I'm really frustrated, sorry.

You have at least three people who have responded to your question asking for a very easy to provide sample of the pathnames you're trying to process and the corresponding ln commands you hope to create from those pathnames. Imagine how frustrated we are to see that after being asked for this data four times you still refuse to give us any sample data to work with.

If you would have given us the information I requested in post #2 in this thread 22 hours ago, I'm absolutely positive that you would have had a working script at least 14 hours ago. Since you refuse to give us the information we need to be sure we have something that will meet your needs, maybe we should all just give up. :mad:

Please show us a couple of sample pathnames produced by:

find . -type d -name 'blablabla'

(presumably replacing blablabla by whatever directory filename or filename pattern you're really using), and show us the corresponding exact ln commands you hope to produce from those pathnames.

If you're unable to meet this simple request in your next post, I'll close this thread, remember the amount of time I've wasted trying to help you, and never respond to any of your future posts again.

In production we have NDAs. I can't just go posting stuff like that. I'll have to make up similar paths. Yes, even the names of the production are protected.

I still don't understand why you need the sample path. All I need to do is extract the characters from 53:14 and put them into the symlink name. Why is that so hard to understand?
I can do it by setting each shot as a variable but that's a hassle. I figured someone here would know how to do it much easier.

I really don't see what supplying a bunch of big long paths are going to do for anyone. I've already explained where the characters are that I need to extract and pipe in the name.

If I knew more of what you need I'd already know how to do it. I don't. I'm trying.

What supplying a bunch of big long paths with corresponding sample ln commands that you want to produce from those big long paths would do is give us some actual sample data that we could use to make the rest of your inconsistent and confusing requirements make sense. You have shown us examples of strings containing less than 53 characters and you say you want to extract 14 characters from those strings, and then you show us samples where those 14 characters are only 9 characters.

You know exactly what we need. We have asked for it several times and shown you exactly how to produce it. If you couldn't show us the actual data, you could easily have run the requested find command and obfuscated sensitive parts of the generated path (while keeping the same number of characters) and showing us the ln commands with identical obfuscations.

Instead you tell us we are frustrating you by not being able to guess at what you want from the inconsistent samples you have shown us.

I will make one last attempt based on nezabudka's earlier suggestions. With no sample data to work from, I have absolutely no confidence that this even some close to what you want. Try using it at your own peril:

find . -type d -name 'blablabla' | while read -r path
do	ln -s "$path" "${path:53:14} blablabla"
done

and be warned that creating directory names that contain <space>s (whether actual directories or symbolic links pointing to directories) frequently leads to broken shell scripts falling far short of their intended goal. Nonetheless, a <space> has been included in the symbolic link names this script produces as you seem to require.

Without trying that code, it looks to be exactly what I need. Thank you. I'm fairly confident it will work. Also, I can easily use an underscore instead of a space, which I will per your recommendation.
I didn't mean that I was frustrated by this page and the people helping me, what I meant that was I was frustrated trying to work it out in the shell, alone. Sorry for that.
I really appreciate everyone's help, although I didn't think it would be this much trouble. I would imagine what I'm seeking to do would be fairly common.

Ok, this code,

find ../ -type d -name 'Renders' | while read -r path ; do ln -s "$path" "${path:53:14}_Renders� ; done

results in nothing. I just get a ">." I'm sure there's a name for the non-result but I don't know what it is.
I'm running this as a single line from a bash shell. Should it go in a script? I'll try that.

Well, putting it into a script and running that, I discovered that the quotes didn't match, but once I fixed that, it works!

Thank you, Don and everyone, for your help.

Yes. Having mismatched quotes will cause the shell to give you a secondary prompt and then it will wait for you to type in the rest of the string and its terminating quote.

Fantastic success! The script works even better than I'd hoped after I set some variable for other files I want links to.

Thanks so much! This code is truly awesome.

#!/bin/sh
#Variables
where='/mnt/xxxx/xxxxxx/Season_02/xx_xxx/Shots'
what='Render'

find $where -type d -name $what | while read -r path
do ln -s "$path" "${path:53:14}_$what"
done

I'm glad it is working for you.

Note, however, that this is one of those broken shell scripts that can fail miserably if the directory name assigned to where or the string assigned to what contains any <space> or <tab> characters. To make your code is safe to run even under those conditions, please consider changing your code to:

#!/bin/sh
#Variables
where='/mnt/xxxx/xxxxxx/Season_02/xx_xxx/Shots'
what='Render'

find "$where" -type d -name "$what" | while read -r path
do	ln -s "$path" "${path:53:14}_$what"
done

You might also want to ask yourself whether or not the 53 and 14 will remain constant if you change the value assigned to $where ???

1 Like

Yes. Thank you. I'll do that. I was thinking of adding some interactivity to the script for the 53:14 but it works so well, I only need to use it once per item.

The code is genius, works like a charm and saves me a lot of time already.

Thanks again!

Ok, here's a crazy question: Does anyone know how to do this on windoz that will produce windoz shortcuts?