Getting real path to directory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Getting real path to directory
# 1  
Old 12-18-2009
Question Getting real path to directory

Hi friends,

Need your help. The question is:
1) We have directory '/SomeDirectory/SubDirectory'
2) And have symlink pointing to it like '/AnotherOneDirectory/symlink'
'/AnotherOneDirectory/symlink' -> '/SomeDirectory/SubDirectory'

3) Need to get real path without symlinks knowing only '
/AnotherOneDirectory/symlink'

4) P.S. Using shell commands only.
Something like realpath() call for C.

Thanks, kandrewo.
# 2  
Old 12-18-2009
Is this a homework?
Please read the forum rules!
# 3  
Old 12-18-2009
Bug

What are you talking about? It isn't homework!!!
It is issue I found during my work with shell. If you have no idea how to resolve it I understand you, this is not trivial task, as it can appear at the first look.

We can use find, pwd and other tools, but they don't provide us with real path. Unfortunately Smilie

Thanks, kandrewo.
# 4  
Old 12-18-2009
This statement makes me think it's a homework:

Quote:
4) P.S. Using shell commands only. Something like realpath() call for C.
Why only shell commands?
# 5  
Old 12-18-2009
ls does it for me

Code:
admins-imac:~ tlarkin$ ls -al /etc
lrwxr-xr-x@ 1 root  admin  11 Sep  2 13:34 /etc -> private/etc
admins-imac:~ tlarkin$

bolded shows the parent folder. I am not using standard Unix though, I am using OS X. Might be some differences.
# 6  
Old 12-18-2009
Your question made be think about the real reason... Hm...
I found that I don't know how to retrieve full real path in shell and it confused me. Of course, I can write something like
Code:
char * szResult = realpath(sPath.c_str(), szResolvedPath);

in C++ program and use it. But it is not a solution for each system, where our additional binary is missed. Say at my friend home Smilie

---------- Post updated at 09:36 PM ---------- Previous update was at 09:29 PM ----------

Quote:
Originally Posted by tlarkin
ls does it for me

Code:
admins-imac:~ tlarkin$ ls -al /etc
lrwxr-xr-x@ 1 root  admin  11 Sep  2 13:34 /etc -> private/etc
admins-imac:~ tlarkin$

bolded shows the parent folder. I am not using standard Unix though, I am using OS X. Might be some differences.
So the 1st way I can see is to use something like
Code:
ls -l "directoryname" | cut -f2 -d '>' | sed 's/^ //'

It isn't graceful way, don't you think so? As for me: it is usable but depends on ls standard, and possible differences between UNIX systems. Doesn't it?
# 7  
Old 12-18-2009
Some hints:

not portable: realpath, readlink -f, somehow GNU find printf '%l\n'
portable: Perl has readlink, POSIX shell (cd -P <path> && pwd -P)


Also see:

www.stonehenge.com/merlyn/UnixReview/col27.html
groups.google.com/group/comp.unix.shell/msg/9c4b26b1b49a2ead
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 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 Dummies Questions & Answers

How to get real path to a file?

Hello, I need download a mp3 file from a site but I can not find the actual link. Thanks. Source codes <span title="what is" class="read"><embed type="application/x-shockwave-flash" src="au/audio.swf" width="17" height="13" align="texttop" quality="high" loop="false" menu="false"... (1 Reply)
Discussion started by: tara123
1 Replies

4. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

5. Shell Programming and Scripting

Keep last directory from path

Hello, I am looking for a command that will give me the last directory name from a path ex 1 : /dir1/dir/2/dir3/ output needed dir3 ex 2 : /dir1/dir/2/dir3/dir4/ output needed dir4 (1 Reply)
Discussion started by: Aswex
1 Replies

6. Shell Programming and Scripting

Copy real file from a shortcut preserving the time stamps and directory tree

I have directory with shorcuts of files. for example: gara@yn\short\name1 ( shortcut to gara@yn\FOLDER\OPT\GARA\1.jpg ) gara@yn\short\name2 ( shortcut to gara@yn\FOLDER\OPT\GARA\11.jpg ) gara@yn\short\name3 ( shortcut to gara@yn\MARA\URSA\2.jpg ) gara@yn\short\name4 ( shortcut to... (6 Replies)
Discussion started by: gogok_bg
6 Replies

7. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

8. 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

9. UNIX for Dummies Questions & Answers

How to get directory name from its path?

If I the path to a directory, what command can I use to return the actual name of that directory. test=`pwd`/folder1 > $test folder1 I'd rather avoid anything with regular expressions. Any ideas? (1 Reply)
Discussion started by: ordano
1 Replies

10. Shell Programming and Scripting

geting the real path

Hi there, Is there any way to obtain a real path from a weird path. For example : /foo/../bar/ -> /bar/ /foo/. -> /foo/ Thanks in advance Santiago (5 Replies)
Discussion started by: chebarbudo
5 Replies
Login or Register to Ask a Question