Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've got over 10,000 files that don't have extensions from older versions of the Mac OS. They're extremely nested in file structure, and they also have all sorts of strange formatting and characters. They don't have file types or creator codes attached to them any longer. A lot of these files have text in the file that will let me determine extensions (for example Word.Document.8 is in the text of every file created by that version of Word).

I found a script that looks like it would work for one of these file types at a time, but it erases parts of filenames after nefarious characters, which is not good.

find . -type f -not -name "*.*" -print0 |\
    xargs -0 file |\
    grep  'Word.Document.8' |\
    sed 's/:.*//' |\
    xargs -I % echo mv % %.doc

Should I clean the characters in the filenames first, or programmatically deal with those in the script in order to leave them the same? As long as I lose no information from the filenames, I don't see a problem cleaning out slashes and other problem characters. Also, if I clean the filenames, there are likely to be duplicates, so any cleaning script would have to add something like "-1" before the extension to make sure nothing gets lost.

I'm not tied to this script, but it is understandable, which is a pro. Mac OS X 10.6 is installed on this file server, but I've got access to any recent versions of OS X.

share|improve this question
    
Are you saying that your Word documents contain Word.Document.8, or are you saying that file MyTermPaper reports Word.Document.8 for your Word documents? –  Scott Sep 18 '12 at 21:20
    
What nefarious characters exactly? –  slhck Sep 18 '12 at 22:02
    
For the first, I'm saying the document * 12/03/99 Conclusions + TOC (COPY) has something like Word.Document.8 in it's content, which I could search for in order to name that document with a .doc at the end. The characters that appear to matter so far are slashes, but besides the ones I listed in that file name, there are block parentheses, apostrophes, periods, quotes, and most likely just about every character. I'm not sure yet which will matter besides the slashes. –  v8media Sep 19 '12 at 17:44
    
Just found Trid (mark0.net/soft-tridnet-e.html) from the related bar on the right. Is there something like this for OS X? –  v8media Sep 19 '12 at 18:02
    
Why are you saying xargs -0 file | grep 'Word.Document.8' if the file command isn’t reporting Word.Document.8? –  Scott Sep 19 '12 at 22:03

2 Answers 2

I'm still not 100% sure that I understand the question, but here are a couple of thoughts.

  1. sed 's/:.*//' will mangle any filename that has a colon in it.  Is colon a legal filename character on the Mac?  If not, then this won’t be a problem for you.  Otherwise, consider doing sed 's/: Word.Document.8$//' or. sed 's/: [^:]*$//'.
  2. The mv command spawned by xargs looks like a problem.  Try
    xargs -I % sh -c 'echo mv "%" "%.doc"'.
share|improve this answer
    
Looks like colons are not currently allowed, so probably won't have any of those unless they were allowed in Mac OS 9 or previous. –  v8media Sep 24 '12 at 16:31

2nd question is how do I change the script so that it will look for more than one file type at the same time and give each the proper extension?

Here are a couple of hints to get you started:

sed -e '/Word\.Document\.8/s/something/something else/;s/another/yet another/' \
    -e '/Excel\.Sheet\.8/s/something2/something else2/;s/another2/yet another2/' \
    ...

or

awk '
      /Word\.Document\.8/ {
            awk commands for Word
      }
      /Excel\.Sheet\.8/ {
            awk commands for Excel
      }
          
'
share|improve this answer
    
I think I'll skip that part of it for now. It's not going to be hard to do those separately and have 10 separate scripts for each format if I have to. –  v8media Sep 24 '12 at 16:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.