How To Get The String Right After the Directory Name

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How To Get The String Right After the Directory Name
# 1  
Old 11-04-2009
How To Get The String Right After the Directory Name

i have this basic line of code that accepts for an input parameter. Example of input string is "/u02/app/ccalloc/dev/out/*.txt", a directory name with a wild card filename. my code below can get the directory name by using the `dirname $p1` command. when i attempted to use the `basename $p1` to get the filename with wild card character it failed.

$ sh "/u02/app/ccalloc/dev/out/*.txt"
Usage: basename String [Suffix]
the directory is /u02/app/ccalloc/dev/out

the file string is


below is the code

pdir=`dirname $p1`
pfile=`basename $p1`
echo "the directory is $pdir\n"
echo "the file string is $pfile\n"

please help. thanks.
# 2  
Old 11-04-2009
Do you have more than one .txt file in that directory?

By quoting this "/u02/app/ccalloc/dev/out/*.txt" all text files will become $1 to the script. Basename can only handle one file at a time.

Either unquote the argument so basename can use the first one, or use a loop to process all of the .txt files:


for FILE in $@; do
  basename "$FILE"

# 3  
Old 11-04-2009
great that works. i need to add a second parameter when i added a second parameter it appears that it is also including the second parameter string.

$ sh "/u02/app/ccalloc/dev/out/*.txt" "/export/home/ccalftdv/data"

the last display data is coming from the second parameter "/export/home/ccalftdv/data". how do i exclude that? thanks.
# 4  
Old 11-04-2009
In the for-loop, change $@ to $1.

$2 is "/export/home/ccalftdv/data"
# 5  
Old 11-04-2009
wonderful that works. many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search directory for string and print file name

Good Evening Folks - I have a need to search a specific directory and ALL sub-directories for the following string: Data Load UpdatedIf this string is found, I need to print content from the line directory above it between symbols, as well as the file name where it is found. Here is a... (1 Reply)
Discussion started by: SIMMS7400
1 Replies

2. Shell Programming and Scripting

How to search for a string in all the files irrespective of directory.?

Hi All, How to search for a string in all the files irrespective of directory. If I use grep -i 'hello' *.* It will search for the string hello in the files of current directory. But I want to check for the string hello in the files of all the directories. Thanks (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

3. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all * files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

4. Shell Programming and Scripting

How to check if a filename in a directory starts with a certain string

Hello, Trying to iterate over set of file in current directory and check if the file name in that folder matches certain string. This is what I have so far. Here I am checking if the file name starts with nexus, if so echo file name to log file. Getting weird syntax errors. Any help is... (7 Replies)
Discussion started by: scorpioraghu
7 Replies

5. Shell Programming and Scripting

check if a given string is a file or directory

hi i want to know how to do this if the given is /tmp/ and it is a valid directory then it will echo directory if the given is /tmp/file.txt and is a valid file then it will echo file.. thanks! (5 Replies)
Discussion started by: h0ujun
5 Replies

6. UNIX for Dummies Questions & Answers

How to Find a String in Each File in a Directory

Hi, I have the ff. list of files in the dir named /home/joule/unix/archive: $ /home/joule/unix/archive joule1.gz joule2.gz joule3.gz joule4.gz joule5.gz joule6.gz joule7.gz joule8.gz What I would like to do is to find this string in a group of files in the directory above: String to... (1 Reply)
Discussion started by: Joule
1 Replies

7. Shell Programming and Scripting

Replace a string in all files under a directory and its subdirectories

Hello Friends, I've been trying to write a script which finds a string and change it with another string. For this i want to search all files (with its arguments) under a spesific directory and its subdirectories. For example lets assume i want to replace an IP= with another... (4 Replies)
Discussion started by: EAGL€
4 Replies

8. UNIX for Dummies Questions & Answers

Script to find a string in a directory/sub-directory

I'm trying to find this string 'preparing string IBE_Quote_W1_Pvt.SaveWrapper for quote_header_id’ in my Apache log file directory. The log file that contains this string may be in a parent direcotry or a sub-directory. I have tried 'grep' and 'awk' with no success. I would like to get the path... (3 Replies)
Discussion started by: gross
3 Replies

9. Shell Programming and Scripting

Appending a string to all files in a directory

Hi, I will have to append a common string at the beginning of each and every line to all files in the same directory. How do i do this? (1 Reply)
Discussion started by: ragavhere
1 Replies

10. UNIX for Dummies Questions & Answers

searching for a string in directory

Hi, I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found. e.g. all logs begin om20020927 what I have been... (4 Replies)
Discussion started by: warrend
4 Replies
Login or Register to Ask a Question