determining actual directory of a symlinked directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting determining actual directory of a symlinked directory
# 1  
Old 09-24-2007
determining actual directory of a symlinked directory

Hello all,
I have files at /var/dir1/dir2/fil1.log etc.,.

dir2 is symlinked to /export/xxx/dir3


I am trying to monitor the disk space of the mount where these log files are present.
How do I determine dynamically the actual directory of the log files and corresponding mount when I use df command.

Thanks,
Chiru
# 2  
Old 09-24-2007
Some platforms provide a "readlink" program.

If the platform doesn't, it would be simple to write a readlink C program, there is an API function called readlink.

Alternatively, find the inode and volume of the file and attempt to match that way.
# 3  
Old 09-24-2007
Lightbulb Shell script to readlink

Quote:
Originally Posted by chiru_h
Hello all,
I have files at /var/dir1/dir2/fil1.log etc.,.

dir2 is symlinked to /export/xxx/dir3


I am trying to monitor the disk space of the mount where these log files are present.
How do I determine dynamically the actual directory of the log files and corresponding mount when I use df command.

Thanks,
Chiru
The following is a fairly simple little "readlink" shell script using the find command:

Code:
#!/bin/sh
LINK="$1"
until [ -z "$LINK" ]; do
    cd $(dirname "$LINK");
     LAST="$LINK"
     LINK=$(find "$LINK" -maxdepth 0 -printf "%l")
    [ "$LAST" != "$LINK" ] || { echo "Looped Links detected!" >&2 ; exit 1; }
    done
echo $LAST

I won't promise that there isn't some sort of degenerate chain of links on which this would choke. But it works in my tests. I should probably also have put in
a maximum number of links to follow (in case following the look with a
system call such as open would return "-ELOOP").

I'll leave that as an exercise to the student.

JimD (former Linux Gazette AnswerGuy
# 4  
Old 09-24-2007
Thanks guys, I got a direction in implementing this from your responses.

I am seeing one issue.

There is a symlink within a symlinked directory which has to be determined dynamically. For example say -
/var/dir1/dir2 is symlinked to /export/xxx/yyy/dir3
and in turn /export/xxx/yyy is symlinked to /opt/zzz
In some places /export/xxx is symlinked to /opt/zzz

My aim is to determine dynamically the path of var/dir1/dir2 and get the result as /opt/zzz/dir2

I'm burning my head to put a script to recursively check all the directories, but going long and long.

Please advise.

Thanks,
Chiru
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zipping contents without the actual directory

Hi , I want to zip files present in the directories listed under a parent directory without zipping the directory itself my parent directory path is /informatica/DGDMN/PowerCenter1011/server/infa_shared/SrcFiles/OTE/Final_Directory I have the below directories named as 1,2,3,4,5 listed... (9 Replies)
Discussion started by: paul1234
9 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 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. Web Development

Directory index forbidden by Options directive error on specific directory with indexing disabled

I am seeing the following error appear numerous times in my Apache error log: I have my Apache config configured as below, so I would expect indexing not to occur on this directory as it falls under the parent /web directory. Strangely all the IP address, including this example, all... (5 Replies)
Discussion started by: crmpicco
5 Replies

6. Shell Programming and Scripting

Catching the xml tag when only parent directory is known ..not the actual directory

Hi folks, I have an query that is let say i have to search in an xml file an tag that is <abcdef> now this xml file is at /opt/usr/local so one fastest way to achieve this is go to this location by cd /opt/usr/local and then do grep like this... grep -i abcdef but for this I must know the... (4 Replies)
Discussion started by: punpun66
4 Replies

7. Shell Programming and Scripting

Automatically determining directory path for scripts and programs

I have some C++ code in the following directory structure /home/chrisd/tatsh/trunk/hstmy/ ├── baseLib ├── bin │ ├── awk │ ├── bash │ ├── diag │ ├── ksh │ │ └── TAG201011 │ ├── old │ ├── perl │ ├── prog │ ├── py │ └── tcsh ├── docs ├── fortran ├── others... (0 Replies)
Discussion started by: kristinu
0 Replies

8. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

9. Shell Programming and Scripting

Determining directory path

Hello, I have a script where I get the full directory path of the script being executed: BASE=$0 echo "BASE:" $BASE The output looks like this: BASE: /webapps/appsdev/ACURA/rlz/oses3.sh I'd like to truncate the shell name, leaving just the directory path. The directory path can be... (2 Replies)
Discussion started by: cwalsek
2 Replies
Login or Register to Ask a Question