Unix/Linux Go Back    

Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Finding longest common substring among filenames

Shell Programming and Scripting

file, shell help, string manipulation

Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 12-12-2008
cmcnorgan cmcnorgan is offline
Registered User
Join Date: Nov 2007
Last Activity: 14 February 2012, 11:36 AM EST
Location: London, Ontario, Canada
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Finding longest common substring among filenames

I will be performing a task on several directories, each containing a large number of files (2500+) that follow a regular naming convention:


What I would like to do is automatically discover the part of the filenames that are common to all 2500 files, so that a script could use that as a base name. In practice, this will end up being "YYYY_MM_DD_XX.foo_bar."

I figured out as far as I'll have to use ls to get all the filenames, but there's no command that I know of that will find the largest substring that exists among a large number of strings. I thought perhaps there would be some sed guru out there that would find this problem trivial. You sed experts always blow my mind.
Old Unix and Linux 12-12-2008
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
Join Date: Mar 2007
Last Activity: 8 September 2015, 12:32 AM EDT
Location: Toronto, Canada
Posts: 2,896
Thanks: 0
Thanked 132 Times in 117 Posts

set -- *
while [ -n "$name" ]
  set -- $name*
  [ $# -eq $files ] && break
echo $name

Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
finding common numbers (contents) across 2 or 3 files Lucky Ali Shell Programming and Scripting 4 05-27-2011 03:08 PM
Finding longest line in a Record SEinT Shell Programming and Scripting 5 04-19-2011 03:17 AM
Finding Authors in Common Across Dozens of Lists Peggy White Shell Programming and Scripting 5 03-30-2009 10:45 AM
Finding the most common entry in a column Donkey25 Shell Programming and Scripting 12 11-22-2007 12:16 PM
finding the last substring... cutelucks Shell Programming and Scripting 7 11-04-2006 06:48 AM

All times are GMT -4. The time now is 10:41 AM.