Visit Our UNIX and Linux User Community


splitting up of absolute filepath


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting splitting up of absolute filepath
# 1  
Old 01-09-2007
splitting up of absolute filepath

Hi,

I am using the bash shell on suse.I need to split the absolute file path
into filename and directory path.

eg: /cas/43fg/temp/sample.txt

i want to split the above into
sample.txt(file) and /cas/43fg/temp(directory path)

I want to do this by using shell commands.Would anyone help ?
# 2  
Old 01-09-2007
A solution :
Code:
full_path=/cas/43fg/temp/sample.txt
file=$(basename $full_path)
dir=$(dirname $full_path)

Jean-Pierre.
# 3  
Old 01-10-2007
Code:
full_path=/cas/43fg/temp/sample.txt
pth=$( echo ${full_path%/*} )
file=$( echo ${full_path##/*/} )

or
Code:
full_path=/cas/43fg/temp/sample.txt
pth=$( echo $full_path | sed "s,/[^/]*$,," )
file=$( echo $full_path | sed "s,/.*/,," )

# 4  
Old 01-10-2007
MySQL

All solutions are working fine,Thank you all for your valuable comment and time.

anbu,

would you pls explain the first solution of yours.It's working fine,but i am curious to know the chemistry.So pls.
# 5  
Old 01-10-2007
Code:
pth=$( echo ${full_path%/*} )

Remove the shortest match from / to end of the input string

Code:
file=$( echo ${full_path##/*/} )

Remove the longest match /*/ from the start of the string
# 6  
Old 01-10-2007
Quote:
Originally Posted by anbu23
Code:
full_path=/cas/43fg/temp/sample.txt
pth=$( echo ${full_path%/*} )
file=$( echo ${full_path##/*/} )

Why the $(..) and the echo ?

This will work as well.
Code:
pth=${full_path%/*}
file=${full_path##/*/}


Previous Thread | Next Thread
Test Your Knowledge in Computers #674
Difficulty: Medium
Wi-Fi does not use any parts of the IEEE 803 protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract filepath names between two strings

OS : Fedora Linux 26 Shell : bash I have a file with around 5000 lines like below. file /usr/share/icons/Papirus/16x16/actions/papirus-icon-theme-20180501-1.noarch conflicts with file ... file /usr/share/icons/Papirus/16x16/actions/align-horizontal-left-to-anchor.svg conflicts between... (7 Replies)
Discussion started by: John K
7 Replies

2. UNIX for Beginners Questions & Answers

Copy filepath along with filename at end of another file

Hi , Please help me out for the below problem - I have 2 files in a directory - $ ls -ltr total 4 -rwx------+ 1 abc Domain Users 615 May 31 17:33 abc.txt -rwx------+ 1 abc Domain Users 0 May 31 17:33 ll.sh I want to write the filename of abc.txt along with the directory to the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

3. Shell Programming and Scripting

How to add filepath to filename when moving it?

Hi I have a cron job setup that searches for 'core dump' files in a fixed set of directories and moves the core files to a separate file system. I am able to add date and time info to the file name. How can I add the source directory path to the file name? In case anyone is wondering why I... (5 Replies)
Discussion started by: dexter126
5 Replies

4. Solaris

How to get the filepath and filenames in pfiles PID command?

Hi, When I use pfiles PID, it displays the below output and i just see the inode, can i able to get the full path and file name? pl help me on this. Current rlimit: 8192 file descriptors 0: S_IFCHR mode:0620 dev:308,0 ino:12582968 uid:1001378434 gid:7 rdev:24,26 O_RDWR ... (3 Replies)
Discussion started by: balamv
3 Replies

5. Shell Programming and Scripting

Problem while using grep (multi-level) with the space-separated filepath.

Hi, I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for... (3 Replies)
Discussion started by: NanJ
3 Replies

6. Shell Programming and Scripting

Grab a number at end of filepath

Hello, I have a file path such as: /path/to/whatever/30 and I want to get the number off the end. My problem is that there might be other numbers in the path and the last number can be 1 or 2 digits. I tried something like: sed 's/.*\(\/\{1,2\}\).*/\1/' which seems to work fine for single digit... (7 Replies)
Discussion started by: phreezr
7 Replies

7. UNIX for Dummies Questions & Answers

absolute value

is there any function in unix which will convert a integer to absolute value with a single decimal point. suppose x=15232 y=x/1024=14.875 i want y to be 14.8 Similarly if y=6.29452 it should come as 6.3 (3 Replies)
Discussion started by: dr46014
3 Replies

8. Shell Programming and Scripting

tar: extract single file to different filepath

Hi, This is my first post here - I'm hoping I can get some help! I have searched these forums and othersand not getting anything that works. I am trying to extract a single file from a tar archive to a diffierent location than it will default to. For example my tar log shows me ... a... (3 Replies)
Discussion started by: littleIdiot
3 Replies

9. Shell Programming and Scripting

Absolute value

Is there a function in awk to get the absolute value of a-b which can be negative or positive, I just care for the absolute value... (5 Replies)
Discussion started by: placroix1
5 Replies

10. UNIX for Dummies Questions & Answers

Length of a Unix filepath max length

Hi Guys, Could anyone shed some light on the length of a Unix filepath max length pls ? thanks ! Wilson (3 Replies)
Discussion started by: wilsontan
3 Replies

Featured Tech Videos