5

I am trying to remove a list of specific words from all my files with a certain directory and replace them with nothing.

So:

This Awesome Content 720p BLAH FOO BANG OOO - 30.9.2013.mp4

Becomes:

This Awesome Content - 30.9.2013.mp4

Now the following works great for a single find and replace one word.

find path/to/folder/ -maxdepth 3 -name '*.*' -execdir bash -c 'mv -i "$1" "${1//foo/}"' bash {} \;

I have also tried just multiple finds but this seems like a very long way of doing it and i seem to run into issues this way.

Couple of issues i have:

  • Want it to be case insensitive
  • Need "${1//foo/}" to refer to a list
  • Remove white spaces if greater than 1

Trying to run this as a bash script on a cronjob.

Unless there is a better to way to remove everything between "This Awesome Content" - "30.9.2013.mp4".

Much appreciated.

Keelan
  • 325
  • 1
  • 4
  • 12

4 Answers4

4

You can access a file's name as a variable using the 'echo' command. Once that is done, the most powerful way to make your desired changes is to use 'sed'. You can string together sed commands with the '-e' flag. As part of a for loop in bash, this line gives you a start. You can also use a line like this as part of your 'find' statement.

echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI'

Once you have your desired file names, you can then move them back to the original name. Let me know if you need more specific instructions.

UPDATE: Here is a more complete solution. You'll have to tune the script to your own file names, etc.

for fyle in $(find . -name "*.*")
do 
   mv -i $fyle `echo $fyle | sed -e 's/FOO//gI' -e 's/BANG//gI' `
done

Finally, to replace more than one whitespace character with one whitespace character, you can add another sed command. Here is a working command:

echo "file    input.txt" | sed 's/  */ /g'
philshem
  • 24,761
  • 8
  • 61
  • 127
  • Seems i am a complete newb. How would i implement this into my find command, replace the mv command? Also if i add I to -e 's/FOO//g' e.g. -e 's/FOO//Ig' will this ignore case sensitivity. – Keelan Sep 30 '13 at 15:18
  • 1
    To make sed case insensitive, put a gI at the end. I've updated my post. – philshem Oct 01 '13 at 07:15
  • 1
    I've added a more complete solution. Please let me know how it works for you. – philshem Oct 01 '13 at 07:34
3

One way to do this will be to add an intermediate step where you generate a file with mv commands to achieve this, and then execute that file. I am assuming you have a words_file file containing words you don't want.

cd to the folder before starting

# Create list of valid <file>s in file_list, and list of "mv <file> " commmands
# in cmd_file
ls | grep -f words_file | tee file_list | sed 's/\(.*\)/mv "\1" /g' > cmd_file

# Create the sed statements using the words_file, store it to sed_commands
# Then, apply the sed commands to file_list
sed 's/\(.*\)/s\/\1\/\/g/g' words_file > sed_commands
sed -f sed_commands file_list > new_file_names

# Combine cmd_file and new_file_names to produce the full mv statements
paste cmd_file new_file_names > final_cmds

# To verify the commands
cat final_cmds

# Finally, execute it
sh final_cmds

This is what I could come up with, it avoids manually writing sed -e for each word. Not sure if there is a simpler way using common bash utilities. Of course, you can use perl or python and write it more concisely.

Edit: Simplified it, did away with eval and xargs.

Hari Menon
  • 33,649
  • 14
  • 85
  • 108
1

A script for the task. It accepts at least two arguments, the first one is the list of words to delete from file names, and the rest are the files to process:

perl -MFile::Spec -MFile::Copy -e '
    $words = join( q{|}, split( q| |, shift ) );
    $words_re = qr{$words}i;
    for $path ( @ARGV ) {
        ($dummy, $dir, $f) = File::Spec->splitpath( $path );
        $f =~ s/$words_re//g;
        $f =~ s/\s{2,}/ /g;
        $newpath = File::Spec->catfile( $dir, $f );
        move( $path, $newpath );
        printf qq|[[%s]] renamed to [[%s]]\n|, $path, $newpath;
    }
' "720p BLAH FOO BANG OOO" tmp/user/*.mp4

In my test I had following output:

[[tmp/user/This Awesome Content 720p BLAH FOO BANG OOO - 30.9.2013.mp4]] renamed to [[tmp/user/This Awesome Content - 30.9.2013.mp4]]
Birei
  • 35,723
  • 2
  • 77
  • 82
  • I don't know perl at all so i think this maybe out of my realm. Can i specify a directory to search for files? – Keelan Sep 30 '13 at 15:29
  • 1
    @Keelan: Yes. But then use a substitute command is dangerous because can modify parts of the path. It will need a fix to use a module. – Birei Sep 30 '13 at 15:34
  • I get a bunch of compile errors and string found where operator expected. More than likely the formatting is wrong when i am copy and pasting i presume. – Keelan Sep 30 '13 at 16:05
  • @Keelan: What are your arguments to the script? – Birei Sep 30 '13 at 16:10
  • word for word what you put though i changed the path. http://pastebin.com/raw.php?i=mNgEmzFD – Keelan Sep 30 '13 at 16:52
  • @Keelan: I cannot reproduce your issue. Maybe your `perl` version is too old to support any of those modules? But I can't help much with it. Sorry. – Birei Sep 30 '13 at 17:06
0

prename can be useful for the file renaming part. stg similar:

find ... -exec prename 's/(deleteme1|deleteme2|…)//g' {} \;
David Cain
  • 16,484
  • 14
  • 65
  • 75
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56