how to retrieve base name of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to retrieve base name of the file
# 1  
Old 06-12-2008
how to retrieve base name of the file

I have a file name stored in a variable.
A=/bb/data/f233pdb
How can I retrive the base name (f233pdb) and the path (/bb/data/) and assign them to two new variables, so the result will look like this
B=f233pdb
C=/bb/data/

Thansk a lot for any help -A
# 2  
Old 06-12-2008
using bash
Code:
# A=/bb/data/f233pdb
# IFS="/"
set -- $A
echo $1

# echo $2
bb
# echo $3
data
# echo $4
f233pdb
# echo $5

# 3  
Old 06-13-2008
Quote:
Originally Posted by aoussenko
How can I retrive the base name (f233pdb) and the path (/bb/data/) and assign them to two new variables, so the result will look like this
B=f233pdb
C=/bb/data/
Check the man for dirname and basename.
# 4  
Old 06-13-2008
$ A=/bb/data/f233pdb
$ c=`basename $A`
$ echo $c
f233pdb
$ d=`dirname $A`
$ echo $d
/bb/data
# 5  
Old 06-13-2008
or U could also..

full_path=/bb/data/f233pdb
pth=$( echo $full_path | sed "s,/[^/]*$,," )
file=$( echo $full_path | sed "s,/.*/,," )

Selam
# 6  
Old 06-13-2008
Using variable expansion:

Code:
filename=${A##/*/}

path=${A%/*}/

# 7  
Old 06-13-2008
Computer Another approach

Code:
> A=/bb/data/f233pdb
> B=$(echo "$A" | cut -d"/" -f4)
> C=$(echo "$A" | cut -d"/" -f1-3)"/"

> echo $A
/bb/data/f233pdb
> echo $B
f233pdb
> echo $C
/bb/data/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep: Retrieve two strings from one file to find them anyone on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

3. Linux

Split a large textfile (one file) into multiple file to base on ^L

Hi, Anyone can help, I have a large textfile (one file), and I need to split into multiple file to break each file into ^L. My textfile ========== abc company abc address abc contact ^L my company my address my contact my skills ^L your company your address ========== (3 Replies)
Discussion started by: fspalero
3 Replies

4. Shell Programming and Scripting

Copy same name data from other file base on column

HI I have input file A.txt X Y Z File B.txt 1 X 10 AAA 11123 2 Y 22 PlD 4563 3 Z 55 PlD 54645 4 Z 66 PlD 15698 5 F 44 PlD 154798 6 C 55 PlD 12554 7 Z 88 PlD 23265 8 C 99 PlD 151654 9 C 11 PlD 21546546 I need New File C.txt (1 Reply)
Discussion started by: pareshkp
1 Replies

5. UNIX for Dummies Questions & Answers

Hot to retrieve *.sql file names which we refer in .sh file.

Hi Guys, How to retrieve/get *.sql file names which we refer in all *.sh files. Can any one help me on this. Thanks, Kolipaka (3 Replies)
Discussion started by: lakshmanrk811
3 Replies

6. UNIX for Advanced & Expert Users

retrieve the file.

How will retrieve for a particular months file in UNIX say for example from January to February 2008. (1 Reply)
Discussion started by: rajesh08
1 Replies

7. Shell Programming and Scripting

retrieve value from a file

hi i have a cfg file,it contains lpdma520.dev.ipc.us.aexp.com=SUBMCORE.REQUEST.FT lpdma521.dev.ipc.us.aexp.com=SUBMCORE.REQUEST.FTREQ lpdma522.dev.ipc.us.aexp.com=SUBMITSECUREFILEFLOW i am retrieving the values using the function RetrieveCfgvalue() { CFG_VALUE=`grep "$2="... (1 Reply)
Discussion started by: satish@123
1 Replies

8. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

9. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

10. Ubuntu

How to build as web base File Server

Hi folks, Ubuntu 7.04 server amd64 apache2-2.2.3 postfix-2.3.8 mysql Ver 14.12 SquirrelMail version 1.4.11 I have a running Mail Server. I need to make it as a file server, in addition, allowing users who have accounts (Linux/shell) on the Mail Server to upload/download their files... (1 Reply)
Discussion started by: satimis
1 Replies
Login or Register to Ask a Question