Find exact path of a file/directory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Find exact path of a file/directory
# 1  
Old 12-14-2010
Find exact path of a file/directory

Hello Folks,

A wrapper takes an argument of file or directory name.
I want to allow paths that reside within the current directory only.

Can simply discard the paths like "/A" & "../" as they go outside the current by looking at the path beginning.
How to validate this one:
A/../../../b
The above one also goes out & I am unable to catch this

Is there a way to translate it to normal path by removing the relative ones.
What are my options?

Thanks in advance
# 2  
Old 12-14-2010
What O/S are you running, and what are you scripting/programming in ?
If, for example, you are using script, and have access to something like "perl", you can write a simple perl script to change paths to absolute. For example:
Code:
#!/usr/bin/perl
use Cwd qw(realpath);
print realpath($ARGV[0]) . "\n" || die "no such file or directory\n";]

Save the above script to a file, and "chmod 755" the file.
You can then call the script with an argument and it will change it into an absolute path.
I hope this helps...

Last edited by citaylor; 12-14-2010 at 08:09 AM.. Reason: Sorry - mis-edited
# 3  
Old 12-14-2010
This may help (If input file/dir to function is in inputDir variable)
Code:
 
pwd=$(pwd) # Save current dir value
# Get the dir value in input if it is a filename
dir="$inputDir"
if [ -f "$inputDir" ]; then
     dir=$(dirname "$inputDir")
fi
if [ -d "$dir" ]; then
    # Translate to normal path
    dir=$(cd $dir;pwd)
    cd $pwd
    # Check if dir starts with pwd
    valid=$(echo "$dir" | grep "$pwd.*" | wc -l)
    if [ valid -gt 0 ]; then
       echo "Input parameter is in current dir"
    else
       echo "Input parameter is out of current dir"
    fi
else
   echo "$dir not exists"


Last edited by anurag.singh; 12-14-2010 at 08:38 AM..
# 4  
Old 12-14-2010
I am using shell & will prefer not switching to perl if it can be done in shell Smilie

Will try the 2nd shell approach & get back.
# 5  
Old 12-16-2010
Thanks that works

I missed the Punch line:
You can get the real path by cd'ing into it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Find if a directory exist from a list of the path

Hello, i want to script on sh to check from a path if the directory exist and isn't empty. I explain: path is : /aaa/bbb/ccc/ccc_name/ddd/ Where the cccc_name is in a list, so i think it's $1 My command find -name /aaa/bbb/ccc/$1/ddd/ didn't work because my $1 is the same and not... (5 Replies)
Discussion started by: steiner
5 Replies

3. Shell Programming and Scripting

Find file and zip without including directory path

Does anyone know of a way to zip the resulting file from a find command? My approach below finds the file and zips the entire directory path, which is not what I need. After scanning the web, it seems to be much easier to perform gzip, but unfortunately the approach must use zip. find `$DIR`... (5 Replies)
Discussion started by: koeji
5 Replies

4. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

5. Shell Programming and Scripting

a little help with find and directory path for application

Just a little backgroud, I have a library of mp3 files in the following structure: /mp3/artist/album/track.mp3 Also contained in each album directory is a cover.jpg which contains the cover art file for that particular album. I want to add the cover.jpg to the mp3 tag and have been using... (8 Replies)
Discussion started by: barrydocks
8 Replies

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

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

8. UNIX for Dummies Questions & Answers

how to find a path within unix root directory

I need to know whether nyfile/mypath exists on the file system in the root directory. How to do this (1 Reply)
Discussion started by: ramky79
1 Replies

9. UNIX for Dummies Questions & Answers

Exact path to a file

I uploaded a database file to Public_html of a virtual server using Unix and a dedicated ip address. I can reach it by browser in typing the address as xxx.xxx.xxx.xxx/Directoryname/databasename.dat . I am asked what I want to do, download or open. When I try to reach the file via a program... (2 Replies)
Discussion started by: timmendorf
2 Replies

10. UNIX for Dummies Questions & Answers

Question about Restricting Search path of FIND to current directory

Hi, By default FIND command searches for matching files in all the subdirectories within the specified path. Is there a way to restrict FIND command's search path to only the specified directory and NOT TO scan its subdirectories. Any help would be more than appreciated. Thanks and Regards (2 Replies)
Discussion started by: super_duper_guy
2 Replies
Login or Register to Ask a Question