Search filenames in directory, return filename only


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search filenames in directory, return filename only
# 1  
Old 07-06-2011
Search filenames in directory, return filename only

Howdy,

I am trying to search for a string within a directory, and then return the filename only, without the directory itself.

I am using korn shell and the command

x=$(find /XXXXX/XXXXX/XXXXX/I2 -exec grep -c "$line" {} /dev/null \Smilie

which returns the directory and the filename:


XXXXX/XXXXX/XXXXX/filename

when I need just

filename

does anyone know an easy way ot do this?
# 2  
Old 07-06-2011
Try this,

Code:
find . -name "*.log" -exec grep -l "Ramesh" {} \;

# 3  
Old 07-06-2011
Code:
$ for i in `find . -type f | xargs grep -l test`; do basename $i; done

# 4  
Old 07-06-2011
I'm sorry, maybe I used the wrong syntax on those commands, however, neither of them returned just the filenames.
I'm looking to give the script a directory, and search all the filenames in the directory for a specific string represented by a variable: $line
# 5  
Old 07-06-2011
Code:
for i in `find . -type f | xargs grep -l test`; do basename $i; done > output.txt

The above command will give only the filename (not the absolute path from the current directory)

In the above command, i am grep the word "test"

check the output in the output.txt file
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

There are multiple filenames in the directory.How to return the the lastest files for each file name

there are mutiple file nams in the directory. How to return the the lastest files for each file name. ex. abc1234_050201 abc1234_050206 abc1234_050208 xyz34_050204 xyz34_050210 xyz34_050218 thanks (4 Replies)
Discussion started by: grand_sam
4 Replies

2. UNIX for Dummies Questions & Answers

ls command with wild card can't return filenames starting with dot

I came across the following behaviour in Solaris 10 (x86-64bit) and RHEL 5.6 (Tikanga) -a option in ls command is supposed to list all files starting with a dot. From the man page of ls -a, --all do not ignore entries starting with . $ touch .mytestfile $ vi .mytestfile $... (2 Replies)
Discussion started by: kraljic
2 Replies

3. UNIX for Advanced & Expert Users

Recursive search for filenames containing ..

Hi all, Since my gopher server doesn't like filenames containing 2 or more consecutive dots in a filename, I'd like to do a search for them and replace them with... well, let's say underscores... I've tried a oneliner or 2 from other posts, but they don't seem to work well with locating dots.... (3 Replies)
Discussion started by: Evert
3 Replies

4. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

5. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

6. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

7. Shell Programming and Scripting

move filenames with to another filename without the last character ~

Hi, I need to move filenames with the following format below into filenames without the ~ like sample below. I hope you can help me create a simple unix script that will do this. Thanks in advance! move filename from: AIRS20081225-235641.BSP~ AIRS20081225-235648.BSP~ AIRS20081225-235640.BSP~... (3 Replies)
Discussion started by: ayhanne
3 Replies

8. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

9. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies

10. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies
Login or Register to Ask a Question