LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-23-2004, 05:33 PM   #1
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
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
 
Old 03-23-2004, 06:19 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
find -printf "$PWD/%P\n"


Cheers,
Tink
 
Old 03-24-2004, 03:57 AM   #3
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
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.
 
Old 03-24-2004, 05:01 AM   #4
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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
 
Old 03-24-2004, 05:12 AM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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
 
Old 03-24-2004, 05:30 AM   #6
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
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.
 
Old 03-24-2004, 05:55 AM   #7
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I dunno, you seem to be making life hard for yourself
 
Old 03-24-2004, 08:30 AM   #8
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
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.
 
Old 03-24-2004, 12:36 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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


Last edited by Tinkster; 03-24-2004 at 12:42 PM.
 
Old 03-24-2004, 02:16 PM   #10
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
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...
 
Old 03-24-2004, 04:27 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Try

locate \/usr\/local\/bin\/blah


Cheers,
Tink
 
Old 03-24-2004, 04:41 PM   #12
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
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>


Last edited by Bebo; 03-24-2004 at 04:42 PM.
 
Old 03-24-2004, 05:35 PM   #13
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
oops

Last edited by Looking_Lost; 03-24-2004 at 05:38 PM.
 
Old 03-24-2004, 05:37 PM   #14
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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:
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class listf{

	static void getListing(File dirPath)
	{
		
		    File[] directoryListing=dirPath.listFiles();
			
		    if( directoryListing != null){

              		Arrays.sort(directoryListing);
			 				
				
			for(int i=0, j=directoryListing.length; i<j; i++){

			if((directoryListing[i].canRead()) 
			    && (directoryListing[i].isFile()))		
			    try{	
				  System.out.println(directoryListing[i].getCanonicalFile());		
			       }catch(IOException ioe) { }
			       
			  if((directoryListing[i].canRead()) && (directoryListing[i].isDirectory()))
			              getListing(directoryListing[i]);
			}

		  }
	     }
	
     public static void main(String args[])
	{
	
		if(args.length!=1){
			System.err.println("Usage: listf directory");
			System.exit(1);
			}
			

		File theDirectory=new File(args[0]);
			
		if(theDirectory.canRead())
			getListing(theDirectory);	
          }	
}
 
Old 03-24-2004, 06:01 PM   #15
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Original Poster
Rep: Reputation: 30
Wow! 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() 
{
   BASEDIR=`pwd`

   SEARCHFILE=`basename $1`
   cd `dirname $1`
   SEARCHDIR=`pwd`
   cd $BASEDIR

   echo "$SEARCHDIR/$SEARCHFILE"
}
...unless someone's got a shorter way to do it.

Thanks a bunch guys
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bad Display Name balh.blah.blah.com in remove command 0.o *BSD 2 12-08-2004 02:37 PM
ln -s smbspool /blah/blah/hugh? wlfdgcrkz Linux - Networking 0 06-24-2003 01:29 AM
newbie issues....kernel panic: blah blah blah Rio Nishida Slackware 16 11-29-2002 01:08 AM
File System? Blah? KillerCheeto Linux - Newbie 1 11-15-2002 04:13 PM
apachectl configtest ... blah punt Linux - Software 7 07-18-2001 12:23 PM


All times are GMT -5. The time now is 09:34 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration