Searching File Names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Searching File Names
# 1  
Old 07-10-2011
Searching File Names

I am interested in writing a really simple alias to search for duplicates in file names in a given directory.

As an example, the file names follow a convention like:
Code:
TGIFRIDAY_55566_RESTAURANT
TGIFRIDAY_98744_RESTAURANT
TGIFRIDAY_67778_RESTAURANT
TGIFRIDAY_55566_RESTAURANT

These are all files in some directory, and I am looking to have the alias show the two 55566 duplicates.

If possible, I would prefer that it not write to a flat file or anything, just a basic search if possible. Thanks in advance for your help!

Last edited by Scott; 07-10-2011 at 04:43 PM.. Reason: Code tags
# 2  
Old 07-10-2011
Are those filenames stored in some file?
# 3  
Old 07-10-2011
No, that's what I was talking about with the flat file thing, if I can avoid creating a file of the filenames that would be preferred, so I don't have to create a file just to delete it in 10 seconds.

If that's the only way to do it though, maybe I have to go that route.
# 4  
Old 07-10-2011
But the thing is, there can't be files with duplicate filenames in single directory, so I'm not sure how you want to get list of those files?
# 5  
Old 07-10-2011
Yeah you are correct on that, let me rephrase the example file names (the confusion was my misswording what I meant)
Code:
TGIFRIDAYS_55566_RESTAURANT
WENDYS_99874_RESTAYRANT
CAFE_87465_RESTAYRANT
QDOBA_55566_RESTAYRANT

And I would like the Alias to find/separate out
Code:
TGIFRIDAYS_55566_RESTAURANT
QDOBA_55566_RESTAYRANT

So basically, finding file names that have a commonality. NOTE: In what I am specifically searching through, the numbers that would have commonality are all the same number of characters into the file name (ie, don't have to worry that TGIFRIDAYS and QDOBA are different character lengths)

Last edited by Scott; 07-10-2011 at 04:44 PM.. Reason: Code tags
# 6  
Old 07-10-2011
Try:
Code:
ls | perl -F"_" -ane 'push @{$h{$F[1]}},$_;END{for $i (keys %h){print @{$h{$i}} if $#{$h{$i}}>0}}'

# 7  
Old 07-10-2011
That is exactly what I was looking for, thank you so much

---------- Post updated at 02:28 PM ---------- Previous update was at 02:15 PM ----------

So as a follow up, how can I adjust the code to search for duplicates in different columns separated by an underscore.

An example of what I mean:
Code:
TGIFRIDAYS_55566_FHRTS_RESTAURANT
WENDYS_94561_AAABB_RESTAURANT
CAFE_55566_PGOWH_RESTAURANT
QDOBA_45125_AAABB_RESTAURANT

So finding duplicates in the second column yields:
Code:
TGIFRIDAYS_55566_FHRTS_RESTAURANT
CAFE_55566_PGOWH_RESTAURANT

Finding duplicates in the third column yields:
Code:
WENDYS_94561_AAABB_RESTAURANT
QDOBA_45125_AAABB_RESTAURANT

From the original line, I think this logic is in the "_" section right after perl -F, but I wasn't able to get it to work like I originally thought it was.

Thanks again!

Last edited by Scott; 07-10-2011 at 04:44 PM.. Reason: Code tags, please...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

2. Shell Programming and Scripting

Searching for file names with variables

Hello everyone We have a problem about searching and copying files with variables. we have variables like $year $jday $date and we want to search the files whose name contain these variables. we tried *$year*$jday*$date or with ? instead of * thank you everyone!!! (4 Replies)
Discussion started by: miriammiriam
4 Replies

3. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

8. AIX

Simultaneous searching for files (names matching certain criteria) in several directo

Hello everyone, My OS is AIX 5.2 I would like some help in getting command syntax that does the following: 1. Searches simultaneously several directories downward; 2. Checks every subdirectory in each directory (and so on...) for file names that contain certain characters such as “~”, start... (0 Replies)
Discussion started by: Hopeful
0 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question