Sponsored Content
Top Forums Shell Programming and Scripting Find out directory where command is located Post 302963564 by Scrutinizer on Sunday 3rd of January 2016 02:03:52 AM
Old 01-03-2016
You could try something like this, using pwd and ls -l to determine the absolute pathname:

Code:
file=$(cd -P -- "${0%/*}" 2>/dev/null; pwd -P)/${0##*/}  # get the physical path name of the file that is being specified
while :                                                  # iterate through symbolic links to arrive at the actual directory
do
  ls=$(/bin/ls -l "$file") 
  case $ls in
    l*) file=${file%/*}/${ls##*-> } ;;                   # if the file is a symbolic link then follow it, by replacing
    *)  break ;;                                         # the file name with what the link points to, if not then break the loop
  esac
done
actualdir=${file%/*}


--
You can never force this of course, since one could copy the script and make an adjustment of the variable, but it may help to make users aware....

--
On Linux one could also use readlink, but this is a more general approach, which should also work on most Unix platforms...

Last edited by Scrutinizer; 01-03-2016 at 04:16 PM..
This User Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

using find command only in current directory

I am trying to use the find command to find files in the current directory that meet a certain date criteria. find . -type -f -mtime +2 However, the above also checks the directories below. I tried -prune, but that seems to ignore this directory completely. I read about using -path w/... (5 Replies)
Discussion started by: jliebling
5 Replies

2. UNIX for Dummies Questions & Answers

how to find a file named vijay in a directory using find command

I need to find whether there is a file named vijay is there or not in folder named "opt" .I tried "ls *|grep vijay" but it showed permission problem. so i need to use find command (6 Replies)
Discussion started by: amirthraj_12
6 Replies

3. Shell Programming and Scripting

Find command, -name by directory and subdirectory?

Hi All, I'm trying to use the find command to return matches for a directory and file. For example, given the following directories: /one/two/three/file1.txt /one/three/two/file1.txt /one/four/two/three/file1.txt I'm expecting the following to be returned: ... (16 Replies)
Discussion started by: makodarear
16 Replies

4. UNIX for Dummies Questions & Answers

Command to find all soft links in a directory

I need the command to find all soft links in a directory. Can someone please help. Thank you. (2 Replies)
Discussion started by: jgeo01
2 Replies

5. UNIX for Dummies Questions & Answers

find command to look for current directory only

i have this find command on my script as: for i in `find $vdir -name "$vfile" -mtime +$pday` the problem with this code is that the sub-directories are included on the search. how do i restrict the search to confine only on the current directory and ignore the sub-directories. please advise.... (7 Replies)
Discussion started by: wtolentino
7 Replies

6. Shell Programming and Scripting

find command with wildcard directory

I want to look if there is any file inside a specific directory which was modified before 2 days. I wrote the find command, but the problem is there is one directory and that is a random directory generated by unix, so not sure on how to code for that on the find command. find... (5 Replies)
Discussion started by: srini0603
5 Replies

7. Shell Programming and Scripting

Find command with ignore directory

Dear All, I am using find command find /my_rep/*/RKYPROOF/*/*/WDM/HOME_INT/PWD_DATA -name rk*myguidelines*.pdf -print The problem i am facing here is find /my_rep/*/ the directory after my_rep could be mice001, mice002 and mice001_PO, mice002_PO i want to ignore mice***_PO directory... (3 Replies)
Discussion started by: yadavricky
3 Replies

8. Shell Programming and Scripting

Find Command Include Sub Directory

This script writes the output files to FILES but I don't want to exclude all directories from ABC_CHQ and LYS_ADV, I want to include one sub directory name process which is under ABC_CHQ and LYS_ADV in the search. Right now its excluding everything from prune directories such as ABC_CHQ, LYS_ADV... (10 Replies)
Discussion started by: John William
10 Replies

9. UNIX for Advanced & Expert Users

Linux command to show where bin files are located

I was having trouble remembering the linux command to show where bin files are located. I eventually figured it out from googling that it was "which". How to find command location in Linux? Since I saw a few other interesting commands like whereis and type it got me curious. Are there any... (5 Replies)
Discussion started by: cokedude
5 Replies

10. Shell Programming and Scripting

How-To Exclude Directory in find command

How can i tweak the below find command to exclude directory/s -> "/tmp/logs" find . -type f \( ! -name "*.log*" ! -name "*.jar*" \) -printNote: -path option/argument does not work with the version of find that i have. bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v (7 Replies)
Discussion started by: mohtashims
7 Replies
readlink(2)							System Calls Manual						       readlink(2)

NAME
readlink - Reads the value of a symbolic link SYNOPSIS
#include <unistd.h> int readlink ( const char *path, char *buffer, size_t buf_size); The following version of the buf_size argument does not conform to current standards and is supported only for backward compatibility: int buf_size STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: readlink(): XSH5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Specifies the pathname of the destination file or directory. Points to the user's buffer. The buffer should be at least as large as the buf_size parameter. Specifies the size of the buffer. DESCRIPTION
The readlink() function places the contents of the symbolic link named by the path parameter in buffer, which has size buf_size. If the actual length of the symbolic link is greater than buf_size, an error is returned. The length of a symbolic link will not exceed PATH_MAX. [Tru64 UNIX] If the actual length of the symbolic link is less than buf_size, the string copied into the buffer is null-terminated. For a readlink() function to complete successfully, the calling process must have search access to the directory containing the link. RETURN VALUES
Upon successful completion, the readlink() function returns a count of bytes placed in the buffer (not including any terminating null). If the readlink() function fails, the buffer is not modified, a value of -1 is returned, and errno is set to indicate the error. ERRORS
The readlink() function sets errno to the specified values for the following conditions: Search permission is denied on a component of the path prefix of the path parameter, or read permission is denied on the final component of the path prefix of the path parameter. The file named by the path parameter is not a symbolic link. An I/O error occurred while reading from or writing to the file system. Too many symbolic links were encountered in resolving path. The length of the path parameter exceeds PATH_MAX, or a pathname component is longer than NAME_MAX while {_POSIX_NO_TRUNC} is in effect. The file named by the path parameter does not exist or the path parameter points to an empty string. A component of the path prefix of the path parameter is not a directory. [Tru64 UNIX] The pathname in the symbolic link is longer than buf_size. For NFS file access, if the readlink() function fails, errno may also be set to one of the following values: [Tru64 UNIX] The owner or group ID is not a value supported by this implementation. [Tru64 UNIX] Indicates a stale NFS file handle. An opened file was deleted by the server or another client; a client cannot open a file because the server has unmounted or unexported the remote directory; or the directory that contains an opened file was either unmounted or unexported by the server. RELATED INFORMATION
Functions: link(2), stat(2), symlink(2), unlink(2) Standards: standards(5) delim off readlink(2)
All times are GMT -4. The time now is 08:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy