how to check the actual path instead of link path


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to check the actual path instead of link path
# 1  
Old 10-01-2006
how to check the actual path instead of link path

Hi

I have a path link
/test/df/link1/actual/file1

here link1 is actually a softlink link1= a/b

i need to print the ACTUAL FULL path instead of a linked path

is there any direct command to print the actual path of any linked path
eg
showPhyscialPath /test/df/link1/actual/file1 and it prints the full path

i know that i can do using
cd /test/df/link1/actual/file1/
and then pwd -P
shows the actual full path, i dont want to change the directory and then used pwd

Thaknks
Rel
# 2  
Old 10-02-2006
You can use 'ls' with the -L option.
Code:
       -L, --dereference
              when showing file information for a symbolic link, show information for  the
              file the link references rather than for the link itself

# 3  
Old 10-02-2006
thanks for reply
but ls -L just works for link and does not work for any directory after link or before link

if i do
ls -L link1 then it will show the actual right side value of link1

but if we do
ls -L link1/newdir then it wont show anything

in my case i dont even which one is link, but i want to know the actual path if there is any link anywhere in the path
# 4  
Old 10-04-2006
well, maybe it going to be little hard to understand, but I hope it helps in another case.

When I want to catch somebody's real path of a process execution (I already know the process ID) I used to use "pwdx PID" this is a Solaris utility (you can check it doing a: man proc)

"proc" is a set of tools some times weird, some times very helpful.

Hope it works
# 5  
Old 10-04-2006
There's a reason for this. To find out what a link "really is" requires making a system call, readlink. In most circumstances, open (the system call used to open a file) is the only call that does this automatically. I don't know of a command line utility this does the readlink. A long time ago, we needed something to do this - here is a small utility to do that. This could be written in other languages, it happens to be C.
Code:
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stddef.h>
#include <pwd.h>

#define ck_null(x) if((x)==NULL){perror(""); exit(EXIT_FAILURE);}
#define ck_ltz(x) if((x)==(-1)){perror(""); exit(EXIT_FAILURE);}
#ifndef PATH_MAX
#define PATH_MAX 255
#endif
#define DEPTH 16

size_t signal_arglen =0;
char signal_buffer[PATH_MAX+1]={0x0};

void sig_int(int signo)
{
	write(STDERR_FILENO,signal_buffer,signal_arglen);
	exit(EXIT_FAILURE);
}

void expand_path(char *path)
{
	char *prepend=malloc(2*PATH_MAX);
    char *ptr=path;    
	ck_null(prepend);

	if(memcmp(path,"~/",2)==0 || memcmp(path,"./",2)==0)
	{
		struct passwd *pw=NULL;
	    char tmp[PATH_MAX+1]={0x0};	    
	    
		ptr++;       
		switch(*path)
		{
			case '.':				
				sprintf(prepend,"%s%s", getcwd(tmp,PATH_MAX), ptr);
				break;
			case '~':  /* not normally executed ~/ expanded by shell */
				pw=getpwuid(getuid() );
				sprintf(prepend,"%s%s", pw->pw_dir, ptr);
				break;
			default:
				break;			
		}
		strcpy(path,prepend);
    }
    free(prepend);	
}


void get_link_info(char *path)
{
	char buf[PATH_MAX+1]={0x0};
	ssize_t buflen=readlink(path,buf,PATH_MAX);
	
	if(buflen>0)
	{
	    char *p=strrchr(path,'/');	
	    if(p==NULL) 
	        p=path;	   	   
	    buf[buflen]=0x0;
	    strcpy(p,buf);
    }	
}

void recurse_path(char elements[][PATH_MAX],char *path)
{
	int i=0;
    char *p=path;
    char *q=NULL;
        
    if(*p=='/') i=(-1); 
    if(strchr(path,'/')==NULL)
    {
    	strcpy(elements[0],path);	
    }
    else
    {  
		for(p=path;*p;p++)
		{
			if(*p=='/')			
				q=elements[++i];				
    	    *q++=*p;
		}	
     }
}

char  *get_path_elements(char *dest, char *path)
{
	char *working=calloc(1,PATH_MAX*2);
	int i=0;
	char elements[DEPTH][PATH_MAX]={0x0};
	
	ck_null(working);	
	recurse_path(elements,path);
	while(*elements[i])	
	{
		strcat(working,elements[i++]);
		get_link_info(working);	
	}
	strcpy(dest,working);
	free(working);
	return dest;
}

int main(int argc, char *argv[])
{
	char path[PATH_MAX*2]={0x0};
	char working_path[PATH_MAX*2]={0x0};
	int i=1;
    char *p=argv[i];

	sprintf(signal_buffer,"%s interrupt\n",argv[0]);
    signal_arglen=strlen(signal_buffer);	
	signal(SIGINT,sig_int);
	if (argc >1)
	{
	    while(*p)
	    {
	    	strcpy(working_path,p);
	    	expand_path(working_path);
	    	printf("%s\n",
	    	        get_path_elements(path, working_path));
	    	i++;
	    	p=argv[i];
	    }
	}
	else
	{
		while(fgets(working_path,sizeof(working_path),stdin)!=NULL)
		{
			expand_path(working_path);
	    	printf("%s\n",
	    	        get_path_elements(path, working_path));		
		}
	}
	return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Makefile missing include path Although the path exists and defined

i have make file which i try to make them generic but it keeps to compline it missing include directory this is the makefile : CXX=g++ CPPFAGS= -Wall -O0 -g -std=c++14 INCLUDES = -I/home/vagrant/libuv/include -Isrc LIBS_DIRS = -L/home/vagrant/libuv/build LDFLAGS=... (7 Replies)
Discussion started by: umen
7 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

4. Shell Programming and Scripting

Generate class path dynamically based on source path

Hi experts, I have multiple file names ending with .jsp located in $SOME_DIR, $SOME_DIR/f1/,$SOME_DIR/f2/test,$SOME_DIR/f3/fa and there are equivalent class files in $SOME_DIR/WEB-INF/classes/_pages,$SOME_DIR/WEB-INF/classes/_pages/_f1,... (0 Replies)
Discussion started by: oraclermanpt
0 Replies

5. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. Web Development

Apache mod_rewrite: from 'friendly' url to actual path

I'd like to translate a friendly url such as: http://www.xxxyyyzzz.com/page/12345678/ to: http://www.xxxyyyzzz.com/page/12/34/56/78/ Seems simple enough, but I cannot figure out how. Any one done this before? (2 Replies)
Discussion started by: markericksen
2 Replies

8. Shell Programming and Scripting

Executing Commands From Non-Standard Path (Changing user's PATH secretely???)

Hi: I have a requirement as below: I have some standard Unix commands modified and kept them in a directory say /usr/clsh/bin. For example I have a script named "ls" kept here which is modified version of "ls" (say it always gives long listing i.e. ls -l). When any user logs on and types... (2 Replies)
Discussion started by: ramesh_samane
2 Replies

9. Shell Programming and Scripting

check if specified path is in $PATH

echo $PATH | grep "\/usr\/ucb" is not working using sh-posix The problem is very simle. I want to check '/usr/ucb' is in the PATH environment variable. If i simply grep '/usr/ucb' i might got wrong result eg '/usr/ucb/bin'. After the path an end of line or colon character should be. In... (4 Replies)
Discussion started by: fpeter75
4 Replies

10. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

Hi, I have a file with about 60 lines of path: app-defaults/boxXYZ....... I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command. Is there any other quick way to do this? Thanks ;) (2 Replies)
Discussion started by: Yinzer955i
2 Replies
Login or Register to Ask a Question