Find the length of a path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the length of a path
# 1  
Old 06-11-2012
Find the length of a path

Hey,

I got a question: how do I find the length of a path ?

this is the context:

I used this funtion find:
Code:
DIR="/home/name/directory"
find DIR -name "*.extension"

The output is then:
Code:
/home/name/directory/filename.extension

What I'd like to have is only the last part: filename.extension

I tried with
Code:
find $DIR -name "*.extension" | cut -c${#DIR}-

or even this
Code:
DIR="/home/name/directory"
LENGTH=$(expr length "$DIR")
find $DIR -name "*.extension" | cut -c$LENGTH-

but both gave me a result like this:
Code:
y/filename.extension

Is there any other way to do it or am I doing something wrong ??

Thanks Smilie

Last edited by Scrutinizer; 06-11-2012 at 12:18 PM.. Reason: code tags
# 2  
Old 06-11-2012
Have a look for commands basename and dirname

Assuming you are using ksh script, you can generate the same with:-
Code:
fullpath=/a/b/c/d/file.name

file="${fullpath##*/}"
dir="${fullpath%/*}"

echo $file
echo $dir



I hope that this helps.


Robin
Liverpool/Blackburn
UK
# 3  
Old 06-11-2012
Quote:
Originally Posted by Miki1579
find DIR -name "*.extension"
If you're using GNU find (almost certainly true if you're using Linux) and aren't concerned with other find implementations on other operating systems, you could use:
Code:
find DIR -name '*.extension' -printf '%P\n'

Regards,
Alister
# 4  
Old 06-11-2012
Works great thanks guys !
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find length of multidimension array ???

Does anyone know how to find length of multi dimension array of following type A Afor simple array I is to do for (i in A)n++ to find length of array but if it is multi dimension how to find the length ? (2 Replies)
Discussion started by: nex_asp
2 Replies

2. Programming

Fastest way to find the length of string in c

Hi all, We use strlen() fun provided by library to find the length of a string. Looking inside of it, it has some different mechanism to find the length of string. Normally, we scan the string byte by byte until the '\0' character. It takes a logn time to count length. The Library strlen()... (2 Replies)
Discussion started by: yogeshrl9072
2 Replies

3. Shell Programming and Scripting

awk to find the length of each record.

Hi Guys, I wanted to print the length of each record and the record itself. I tried the following awk ... awk 'a=length(); {print $a,$0}' file1 But it is giving me the records instead of length. and also, it giving me each record twice. Means the value of a is not the length of the... (0 Replies)
Discussion started by: mac4rfree
0 Replies

4. UNIX for Dummies Questions & Answers

Maximum length of a path given as an argument to a shell script

hi, I am using solaris10. I have to write a bourne shell script, which copies files for the said destination path which is passed as an argument to the script. it looks like this myscript.sh /var/test -->destination path now i would like to know what is the maximum length i can... (3 Replies)
Discussion started by: raghu.amilineni
3 Replies

5. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

6. Shell Programming and Scripting

Create path which has length=1023.

Hi, Please help me.. PATH_MAX=1023 I need output as below: /mansa/filesssssssssssssssssssssssssssssssssssssssss Please send me code in ksh shell script.. while ] ; do done Thanks, Mansa (2 Replies)
Discussion started by: mansa
2 Replies

7. UNIX for Dummies Questions & Answers

find length of a parameter

I want to check the length of a parameter passed to the korn shell. I've tested: xxx=hello echo $xxx cnt=`echo $xxx |wc -c` echo "cnt $cnt" (xxx will be the parm). $cnt is 6, it should be 5. I keep getting 1 more then I should. Why is that? How do I code to get the length of a parameter? (4 Replies)
Discussion started by: sboxtops
4 Replies

8. Shell Programming and Scripting

help need to find the length of a variable

Hi frnds, i have a file which contains multiple lines. each line size may come around 64MB. and i want to read line by line and nee to find the length of each line, at present am doing it in the following way, while read LINE do len=`expr length "$LINE"` # and using the variable... (7 Replies)
Discussion started by: smr_rashmy
7 Replies

9. UNIX for Dummies Questions & Answers

Need find a file based length

Can some please help me? Want to find files over 35 characters in length? I am running HPUX. Would it be possible with find? Thanks in advance (8 Replies)
Discussion started by: J_ang
8 Replies
Login or Register to Ask a Question