LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2007, 01:13 PM   #1
firsttux
LQ Newbie
 
Registered: Jan 2006
Posts: 4

Rep: Reputation: 0
getting directory in bash script


Hello,

I'm new in linux programming. I would like to write a script that will search for a specific file eg. test.txt .... This file si located in directory /home/user/1/test.txt, /home/user/2/test.txt ...

For searcing the files I'm using

FILE=$(find /home -iname '*.txt') -> When I echo $FILE, I get eg. /home/user/1/test.txt

Now I would like to get a folder under /home/user eg. 1, 2, 3, in an echo commadn without /home/user/ and file name eg.


File test.txt is in directory 1
File test.txt is in directory 2

Thanks for your answares

Last edited by firsttux; 10-07-2007 at 01:47 PM.
 
Old 10-07-2007, 02:09 PM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
The easiest thing would be to just use the command "basename":

Code:
basename $FILE
To do the opposite (show directory rather than the file) use "dirname".
 
Old 10-07-2007, 02:11 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Have a look at this:

find /home/ -iname '*.txt' | awk -F"/" '{ print "File " $4 " is in dir " $3 }'

This doesn't use the FILE=$(...) construct, all found by the find command is given to awk, which prints the relevant parts (fields 3 and 4 id the separator is set to /).

The | between the find and the awk command is called a pipe. This can be used to give the output of one command to the input of another command.

There are other ways to solve your problem, but if you do not need the output of the find command elsewere, there's no need to store it in a variable first.

Hope this helps.
 
Old 10-07-2007, 05:33 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Using find will probably return many results, not just one. If you want to perform some operation on each file found, you can do it like this:
Code:
find /home/ -iname '*.txt' |while read filename; do ... done
Where ... are the operations you wish to perform for each file found.

On the other hand, if you want to stop searching after the first file has been located, you can do it like this:
[CODE]find /home/ -iname '*.txt' -print -quit[CODE]
This is a good idea considering how long find can take on large directory structures.

Using basename is OK, but it invokes an external program, which means it is slow / will cause more system load, particularly if you have a lot of files you are interested in. Instead you can use some shell variable expansions to extract parts of the string. In this example. basename1 and basename2 will contain the same result, but the operation used to get basename2 is much faster. The same applies to dirname1 and dirname2.
Code:
mypath="/home/matthew/1/file.txt"
basename1=$(basename "$mypath")
basename2=${mypath##*/}

dirname1=$(dirname "$mypath")
dirname2=${mypath%/*}
You can find detailed descriptions of these and other variable expansions in the "Parameter Expansion" section of the bash manual page.

In summary, a good way to achieve what you describe in the second part of your OP is:
Code:
find /home -iname '*.txt' | while read f
do
    b=${f##*/}
    d=${f%/*}
    echo "File $b is in directory ${d##*/}"
done

Last edited by matthewg42; 10-07-2007 at 05:34 PM.
 
Old 10-08-2007, 01:07 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
 find /home -iname '*.txt' -printf "%h\n" | awk -F"/" '{print $NF}'
 
  


Reply


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
file or directory? bash script efus Programming 3 04-26-2007 06:11 PM
Directory variables in a bash script madtinkerer Programming 9 12-04-2006 12:30 PM
changing directory in bash (or other) script zefram Linux - General 2 09-16-2006 10:41 AM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

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