Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Finding longest common substring among filenames

Shell Programming and Scripting

file, shell help, string manipulation

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.
Sponsored Links
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: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts

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

Sponsored Links

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 02:08 PM
Finding longest line in a Record SEinT Shell Programming and Scripting 5 04-19-2011 02:17 AM
Finding Authors in Common Across Dozens of Lists Peggy White Shell Programming and Scripting 5 03-30-2009 09:45 AM
Finding the most common entry in a column Donkey25 Shell Programming and Scripting 12 11-22-2007 11:16 AM
finding the last substring... cutelucks Shell Programming and Scripting 7 11-04-2006 05:48 AM

All times are GMT -4. The time now is 03:48 AM.