Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-27-2008, 02:51 AM   #1
LQ Newbie
Registered: May 2008
Posts: 10

Rep: Reputation: 0
Exclamation reading path from a text file using bash script

hi all

I have a text file which consist file names with complete path.
I want a script which takeout path of file one by one and store path in a variable and file name in other variable.

i am giving you some part of this text file:

/root/Documents/PDF BOOKS/handbook.pdf

with the help of script I want output like this

Old 06-27-2008, 03:21 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389

You could use the basename and dirname commands to split directories from filenames. The first one (basename) will strip the dir part, the other one does the opposite:


$ echo myPath

$ basename $myPath

$ dirname $myPath
A simple onliner to show it in action, using your example:
$ for THIS in `cat infile`
> do
> echo $THIS
> DIRNAME=$(basename $THIS)
> echo $DIRNAME
> FILENAME=$(dirname $THIS)
> echo $FILENAME
> done



Hope this helps.
Old 06-27-2008, 03:25 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can either use the dirname and basename commands into a loop which reads the file line by line, or use the following parameter substitution:
where $line is the current line read from the file.

An aside note: don't use PATH as name of a variable in your script, since it is a reserved word for the environment variable PATH. If you change its value inside your script, most likely you will get "Command not found" errors for subsequent statements.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
input into bash script(reading path) dalicros Linux - General 3 05-30-2007 06:54 PM
Reading a conf file from a BASH script dinolinux Programming 5 08-03-2005 04:18 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM
Bash script - reading from text file twantrd Programming 4 11-24-2004 12:38 AM
reading file, bash script marri Programming 3 11-15-2004 09:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration