Posts Tagged ‘find’

Replace string in file names, bash one-liner

Saturday, December 5th, 2009

After running lame on a bunch of wav files I ended up with tons of files named:


song1.wav.mp3
song2.wav.mp3
song3.wav.mp3
song4.wav.mp3
...

Now, obviously I could have avoided this in this case with the proper options and arguments to lame, but it’s all in hindsight.

Here’s the bash line I use to strip out .wav from the middle of all these files in the current directory:


for filename in *.mp3; do newname=`echo $filename | sed 's/\.wav\.mp3$/.mp3/g'`; mv $filename $newname; done

It’s long for one line but I think it’s still understandable and manageable. The result is:


song1.mp3
song2.mp3
song3.mp3
song4.mp3
...

Note: I notice that I’m being overly carefully with my regexes and wildcards in the example above but it should make it more clear how to adapt it to your case.

Find and delete all .DS_Store files recursively

Friday, November 6th, 2009

This is posted all over the web, but sanity’s sake I’ll post it again here. This use of the unix command line program find will locate and remove all the .DS_Store files that Finder populates throughout your directories. The following removes all files by the name .DS_Store in the current directory recursively:


find . "-name" ".DS_Store" -exec rm {} \;

Remove all duplicate songs/mp3s iTunes creates using bash script

Tuesday, September 22nd, 2009

I gave up using iTunes to play music about year ago, but I haven’t found a free alternative to iTunes’s exceptional file management based on mp3 ID3 tags (If you know of one — I mean better than iTunes one — let me know). So occasionally I let iTunes organize my music library. I drop in folders containing new music and let iTunes go at it. The problem is that if I have folders containing an mp3 and an m3u playlist I get duplicates. If I don’t notice this right away the duplicates build up.

Here’s a bash script to delete all true duplicates. The files must be exactly the same and have almost the same name (the difference being the number iTunes appends on a copy: “song.mp3″ becomes “song 1.mp3″).

Verbose version:


#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"
      echo "$copy deleted..."
    else
      echo "$copy is different..."
      echo "  $copy not deleted..."
    fi
  else
    echo "$original does not exist..."
    echo "  $copy not deleted..."
  fi
done

Quiet Version


#!/bin/bash
find "$1" -regex ".* [0-9][0-9]*\.[^.]*" -print0 | while read -d $'\0' copy
do
  original=`echo "$copy" | sed "s/\(.*\) [0-9][0-9]*\(\.[^.]*\)/\1\2/"`
  # check that  the hypothetical original exists
  if [ -e "$original" ];then
    # check that the original is exactly the same file as the copy
    if diff "$original" "$copy" >/dev/null ;then
      rm "$copy"
    fi
  fi
done

Inverse regex find results

Thursday, July 23rd, 2009

This might seem obvious and there might even be a better way to do it, but to find all files in a directory that don’t match a specified regex just find all files and grep out those that don’t match.

So if you want to find all files in dir/ which don’t match ^.*\.mp3$ issue:


find dir/ -type f | grep -v "^.*\.mp3$"

Notice that the -type f forces find to only return files (no directories) and the -v inverts the results of grep.

One thing to be careful of is that now the regex is applied to the entire paths returned by find. Not just the name of each file.