Expanding file names in a script? ../blah -> /home/blah
Hello,
Is there a way to expand "the dots" in file/directory names to the proper path, so that the reference is absolute and not relative? For instance, if I use find ., all the files and directories in the resulting listing will start with ./. If I instead want to change this to the full path, is there a special command for it, or do I have do write my own script function? Cheers, Bebo |
find -printf "$PWD/%P\n"
Cheers, Tink |
Thanks Tink, but no, this doesn't work all the way. If I would invoke it like this find ../../usr/local/bin/ -printf "$PWD/%P\n", or even like this find ../../usr/local/bin/ -printf "%h/%P\n", I would get the wrong path.
|
The $PWD format he gave was to overcome the find . (current directory) problem, if you want the format of output for any other directory without the leading dot you can just give it the full path to the directory, why use .. ?
find /usr/local/bin |
your find ../../usr/local/bin/
should work like this find ../../usr/local/bin | cut -f5- -d'.' just change the value of -f5 to f7 or whatever depending on how many ../ you put in |
Yes, of course you're right, Looking_Lost. Hm... OK then, I guess I picked another bad example. So, what if I want to do the same thing from /usr/src/linux instead? Then I could issue find ../../local/bin/, and I would not get the full path either by using the -printf stuff or cut.
At the moment, it seems the way to solve this is to pick out the first part, with all the ../'s, cd to that place wherever it is, pwd, and cd back. I was hoping there would be a better way. |
I dunno, you seem to be making life hard for yourself :)
|
Yeah maybe :) What I'm trying to do is that I want to be able to check that any two files aren't the same, just by checking their paths and names.
|
Just out of curiosity: does my archiving script
work on your machine? I see that VisionZ has problems with it :} [edit] back to your problem: Have you considered using locate? It returns the full path ... [/edit] Cheers, Tink |
Hi Tink,
I tested your second script and it worked just fine. Heh, I just realized that I was writing almost exactly the same script - oops ;) It seems VisionZ has problems with the file command, so your script won't work in his cygwin environment. I had to change my first script to sidestep that. Well anyway, no I haven't tried to use locate *testing* Well, if I point it at a certain file, like locate ../../usr/local/bin/blah, then it won't return anything. Doesn't surprise me, though, since noone would need to use locate if they already knew the location of the file :) Well, if there isn't a better way than dirname - cd - pwd - cd, then that'll have to do... |
Try
locate \/usr\/local\/bin\/blah Cheers, Tink |
Well, locate works if the search string doesn't contain any dots, but returns nothing if it does.
What I'm looking for is a command or anything that will expand any stupid directory referal; even ./../../usr/local/../share. <EDIT> BTW, thanks a lot for trying to help me on this :) </EDIT> |
oops
|
Well, I battered this together in java.
Limitations: only prints out filenames and paths, can be changed easily to print out dirs resolves links to their full paths, doesn't print the link rather the thing it's pointing too ignores broken links if you can be bothered you can compile it with gcj gcj --main=listf listf.java -o listf or plain old java compiler save it as listf.java Code:
|
Wow! :D That's a very nice thingy you've got there, but it still doesn't do what I want :)
I think I'll settle for something like this: Code:
redir() Thanks a bunch guys :) |
Quote:
the output is NOT an error ... and if you don't want it to do this, fix it by appending << endl to the cout statement ;) I think that's what you wanted? :D Cheers, Tink P.S.: Qt rocks!! ;) |
Again, wow! :) Thanks Tink, that is what I wanted. But, oh I'm so stupid... Ever heard of the -ef test? It checks if two files are the same... Now I can solve VisionZ's problem :)
|
Ummm ... -ef checks for a hard link. Not for
file identity? In other words, whether to file descriptors point to the same inode? Cheers, Tink |
Err, maybe? :) Good or bad? :scratch: ;)
|
You could just use
cmp which works on binary and text files. |
why not just use realpath?
-michael |
Does that work in shell scripts?
|
yes, assuming it's installed on your system.
-michael |
Thanks a lot for the suggestion. Hm, it seems I don't have it. But on the other hand, from the man page it seems like a C command.
LOL, look at this - from the man page: Quote:
|
Quote:
on a side note, the reason that the c routine's man page says that it is broken is because it is impossible to know ahead of time that a particular path name will be small enough to alloc enough memory to hold it. thus, even if you alloc PATH_MAX (or whatever) space for the output of your path, you may overflow it, which is obviously a security risk. however, you can't avoid this problem, no matter which routine you choose for the purpose. -michael |
All times are GMT -5. The time now is 08:50 AM. |