LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linux issue renaming files - BASH script (https://www.linuxquestions.org/questions/programming-9/linux-issue-renaming-files-bash-script-908634/)

grail 10-26-2011 05:21 AM

In that case would you show us the entire script?

giantanakim 10-28-2011 09:09 AM

I have not included it in any other script at the moment, it is completely stand alone. The code that i copied into my post the other day includes the complete contents of the file.

grail 10-28-2011 12:42 PM

In that case, how are you executing it as it will not run as is?

giantanakim 10-28-2011 01:21 PM

The script does run as is. It is in a file called renamefiles.sh which I marked as executable. I run the command line, type the command ./renamefiles.sh which causes it to run. When I run it, it runs, but for each line says [[ cannot be found. This is the code that I am using exactly:

for file in *.wav;
do cnt=1;
newfile="${file%% *}";
while [[ -f "${newfile}.wav" ]];
do newfile="${file%% *}_$((cnt++))";
done;
mv "${file}" "${newfile}.wav";
done

It has been doing what it is supposed to do, but kicking up that error for every line. In addition, it adds an extra .wav on the end of files that have already been renamed by it, leaving the filename 1112223333.wav.wav.

grail 10-29-2011 02:38 AM

What happens if you place the following as the first line in the file:
Code:

#!/bin/bash

markush 10-29-2011 04:18 AM

I've had similar requirements some days ago. The thread is here: http://www.linuxquestions.org/questi...rogram-908658/

I've done it with perl because the normal rename-tool cannot handle regexp.

Additionally I've written a clean.pl script:
Code:

#!/usr/bin/perl

use strict ;
use warnings ;
use File::Copy ;

opendir THISDIR, "." ;
my @files = readdir THISDIR ;
my ( $old, $new ) ;
foreach $old ( @files ) {
      $new = $old ;
      $new =~ s/ - /-/g ;
      $new =~ s/ /_/g ;
      $new =~ s/ä/ae/g ;
      $new =~ s/Ä/Ae/g ;
      $new =~ s/ö/oe/g ;
      $new =~ s/Ö/Oe/g ;
      $new =~ s/ü/ue/g ;
      $new =~ s/Ü/Ue/g ;
      $new =~ s/ß/ss/g ;
      $new =~ s/,/./g ;
      $new =~ s/'/./g ;
      $new =~ s/\(//g ;
      $new =~ s/\)//g ;
      $new =~ s/&/und/g ;
      next if $old eq $new ;
      move ( $old, $new ) ;
}

I've used this script to substitute german Umlaute with more common characters. The script substitutes " " with "_". I used it for mp3 files. If you want to use it you'll have to add code which uses another filename if $new already exists in the directory. You'll find the solution in my rename.pl script in the other thread mentioned above.

Hope that helps.

Markus

giantanakim 10-31-2011 09:43 AM

Grail:

When I added the #!/bin/bash into the file, it stopped giving the error of [[ cannot be found. It does still add the unnecessary .wav at the end of the files.

Markush:

I will have to look into your script. I am not familiar with PERL at all, but I have a couple of books (Llama and camel) on the way to start picking it up. I have been doing so much programming lately at my job that I need to pick it up as BASH is great, but PERL seems to be a bit more powerful.

grail 10-31-2011 10:59 AM

Quote:

When I added the #!/bin/bash into the file, it stopped giving the error of [[ cannot be found.
This tells us that your default shell is not bash and hence the previous errors.
Quote:

It does still add the unnecessary .wav at the end of the files.
I am not sure I understand what you mean? Assuming the format you have previously supplied for a naming convention is correct
then the following two line:
Code:

newfile="${file%% *}";
newfile="${file%% *}_$((cnt++))";

Both of these will remove any current extensions and so the following is required to replace the .wav extension:
Code:

mv "${file}" "${newfile}.wav";
The only way this will add and additional .wav to the end is if any of the files you are parsing are not of the correct format you previously informed us of.

giantanakim 10-31-2011 02:22 PM

Grail:

What I mean by the unnecessary .wav is that when any file which has already been through the scripted process once is still in the folder when the script is run again has a .wav appended to the end.

grail 11-01-2011 02:37 AM

Quote:

What I mean by the unnecessary .wav is that when any file which has already been through the scripted process once is still in the folder when the script is run again has a .wav appended to the end.
100% correct :) The script is not designed to run over the same data. This is to do with my explanation of the fact that if the data is not in the original format (which it is not once the script
has been run once) it will not produce the desired results.

Original format (as you described it):
Code:

2033028167 by xxxxxx@xxxxxx @ 4_32_36 PM.wav
Once the script has run, and we will assume this to be the only file with the initial string '2033028167', this will look like:
Code:

2033028167.wav
Now if you run the script again and this file has already been changed:
Code:

for file in *.wav; # Assume we are up to our changed file, then file = 2033028167.wav
do
    cnt=1;
    newfile="${file%% *}"; # This says remove everything after a space in $file, but we have none so newfile = 2033028167.wav
    while [[ -f "${newfile}.wav" ]]; # Does 2033028167.wav.wav exist ... No so do not enter while
    do
        newfile="${file%% *}_$((cnt++))";
    done;
    mv "${file}" "${newfile}.wav"; # This line now says ... mv "2033028167.wav" "2033028167.wav.wav"
done


giantanakim 11-01-2011 12:21 PM

Thank you for the clarification, I really appreciate it. Given the situation, it is probably easiest just to move the file into a different folder as it is renamed, so that there are never any that have been renamed already.

Thanks again grail! You have been a lot of help!


All times are GMT -5. The time now is 03:01 AM.