Extracting Directory From Path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting Directory From Path
# 1  
Old 04-15-2011
Extracting Directory From Path

Hi guys. I'm doing some bash scripting and have run into a snag.

Say I have the path:
/home/one/two/three/

All I need is the 'three' while making a filename.
Is there an easy way to do this? I've tried using grep (because I'm that smart.) cut (as I'm unable to tell how many fields there will be in a path, that doesn't work) and than I gave up and started scouring the internet but can't find anyone trying to do the same thing. Any help would be appreciated.
# 2  
Old 04-15-2011
Code:
$ echo "/home/one/two/three/" | ruby -e 'puts gets.gsub(/\/$/,"").split("/")[-1]'
three

$ echo "/home/one/two/three/" | ruby -e 'puts gets.split("/")[-2]'
three

This User Gave Thanks to kurumi For This Post:
# 3  
Old 04-15-2011
You may stick to grep and cut:
Code:
echo "/home/one/two/three/" | grep -Eo '[^/]+/?$' | cut -d / -f1

Or just perl:
Code:
echo "/home/one/two/three/" | perl -pe 's|^.+?([^/]+)/?$|$1|'

This User Gave Thanks to kevintse For This Post:
# 4  
Old 04-15-2011
Thanks a lot both of you Smilie Got it now.
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 Beginners Questions & Answers

Extracting directory portion.

Dear Experts, I have some directory structure something like follows. I would like to cut portion of it. Would you please help me? I have to run this on several sql's. The directory path is dynamic. I have cut what comes after first "sql" string. Input:... (3 Replies)
Discussion started by: srikanth38
3 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

"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

6. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 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

Extracting parts from an absolute path

Hi, How can I extract parts from an absolute path? For example : The absolute path is /dir1/dir2/dir3/dir4/dir5.I need the relative path starting with directory given as parameter : for instance if the parameter is dir3 then the result should be dir3/dir4/dir5 I need generic solution... (9 Replies)
Discussion started by: mortanon
9 Replies

9. Shell Programming and Scripting

Extracting file name from path

if I have a variable value "filetoprocess" equal to "/aaa/bbb/ccc" How can I simply extract the file name of "ccc" (4 Replies)
Discussion started by: ytakbob
4 Replies

10. Shell Programming and Scripting

extracting a field from directory path ??????

Hi I'll be getting a directory path as the input to the script. E.g. 1 abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad e.g. 2 sadfg/sadgjhgds/sd/dtuc/cghcx/dtyue/dfghsdd/XXX/qytq This input will be stored in a variable. My query is how to extract the field in a variable VAR which occurs... (15 Replies)
Discussion started by: skyineyes
15 Replies
Login or Register to Ask a Question