Link to home
Create AccountLog in
Avatar of RAH104
RAH104

asked on

Rename Script

Hello,

I'm having difficulties getting the following to work.  I believe I had this script working before but can't remember how.  The original creator said something about removing sed and tr.

I run as follows:
sh rename.sh /directory

rename.sh: command substitution: line 13: syntax error near unexpected token `|'
rename.sh: command substitution: line 13: `| sed 's/_-_/-/g''
#!/bin/bash
# Convert filenames and folders to lowercase
# and replace spaces recursively
#####################################
 
if [ -z $1 ];then echo Give target directory; exit 0;fi
 
find "$1" -depth -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
        if [ "$oldfilename" != "$newfilename" ]; then
                mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
                #echo "$directory"
                #echo "$oldfilename"
                #echo "$newfilename"
                #echo
        fi
        done
exit 0

Open in new window

Avatar of amit_g
amit_g
Flag of United States of America image

The script looks ok. Try it running as

/bin/bash rename.sh /directory

If that also doesn't work, add

set -x

as 5th line and run it again. Post the output that you get.
Avatar of RAH104
RAH104

ASKER

[director@rh-laptop Test]$ /bin/bash rename.sh /mnt/files/Test
rename.sh: command substitution: line 13: syntax error near unexpected token `|'
rename.sh: command substitution: line 13: `| sed 's/_-_/-/g''
rename.sh: command substitution: line 13: syntax error near unexpected token `|'
rename.sh: command substitution: line 13: `| sed 's/_-_/-/g''
mv: missing destination file operand after `/mnt/files/Test/Test Test'
Try `mv --help' for more information.
rename.sh: line 15: /mnt/files/Test/test_test: No such file or directory
/mnt/files/Test/Test Test --->
/mnt/files/Test/test_test
rename.sh: command substitution: line 13: syntax error near unexpected token `|'
rename.sh: command substitution: line 13: `| sed 's/_-_/-/g''
mv: missing destination file operand after `/mnt/files/Test'
Try `mv --help' for more information.
rename.sh: line 15: /mnt/files/test: No such file or directory
/mnt/files/Test --->
/mnt/files/test
Avatar of RAH104

ASKER

Not sure what you mean about using set x.
Edit the script to and rerun. Post the output.
#!/bin/bash
# Convert filenames and folders to lowercase
# and replace spaces recursively
#####################################
 
set -x
 
if [ -z $1 ];then echo Give target directory; exit 0;fi
 
find "$1" -depth -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
        if [ "$oldfilename" != "$newfilename" ]; then
                mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo ""$directory/$oldfilename" ---> "$directory/$newfilename""
                #echo "$directory"
                #echo "$oldfilename"
                #echo "$newfilename"
                #echo
        fi
        done
exit 0

Open in new window

They key is in this line of error message

mv: missing destination file operand after `/mnt/files/Test/Test Test'

Seems like something goes wrong when running tr and sed. Just echo the value of $newfilename without trying to rename the files and see if anything is horribly wrong.



find "$1" -depth -name '*' | while read file ; do
    directory=$(dirname "$file")
    oldfilename=$(basename "$file")
    newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
    echo "Dry run renaming $directory/$oldfilename to $directory/$newfilename"
    done

Open in new window

Please post the output of

bash -x  rename.sh /directory
/mnt/files/Test/Test Test --->
/mnt/files/Test/test_test

why is there a newline after the arrow?
The script is fine!

Are you running this script in a Linux box?

Have you copy-pasted the script? (through a windows box)

Also as mentioned you shouldn't run a bash script with sh. Either chmod the script and run it directly (./rename.sh) or run it by doing 'bash rename.sh'

//jonas
Avatar of RAH104

ASKER

cjl7:

"Are you running this script in a Linux box?"

Yes - ArchLinux

"Have you copy-pasted the script? (through a windows box)"

Copied and pasted within Linux.

"Also as mentioned you shouldn't run a bash script with sh. Either chmod the script and run it directly (./rename.sh) or run it by doing 'bash rename.sh'"

Have tried this without success.



Tintin:

[root@rh-laptop files]# bash -x  rename.sh /Test
+ '[' -z /Test ']'
+ find /Test -depth -name '*'
+ read file
find: `/Test': No such file or directory
+ exit 0
[root@rh-laptop files]# ls
$RECYCLE.BIN  Downloads  Program Files      Test  director      rename.sh  richard




amit_g:

Running modified script.

[root@rh-laptop files]# bash rename.sh /Test
+ '[' -z /Test ']'
+ find /Test -depth -name '*'
+ read file
find: `/Test': No such file or directory
+ exit 0
[root@rh-laptop files]# ls
$RECYCLE.BIN  Downloads  Program Files      Test  director      rename.sh  richard


Alundrez:

Running modified script:

[root@rh-laptop files]# bash rename.sh /Test
+ '[' -z /Test ']'
+ find /Test -depth -name '*'
+ read file
find: `/Test': No such file or directory
+ exit 0
[root@rh-laptop files]# ls
$RECYCLE.BIN  Downloads  Program Files      Test  director      rename.sh  richard

You are trying to rename files within a directory called "Test director"...

When you supply the directory to the script you need to enclose it with ", otherwise it will enterpet 'Test' as one argument and 'director' as another.

Try sh -x rename.sh "Test director"


cheers

Jonas
Avatar of RAH104

ASKER

Test and director are two seperate directories, perhaps the formatting above confused you.
There is no /Test when you ran the script. You should have used

bash -x  rename.sh Test
Avatar of RAH104

ASKER

amit_g:

OK, have done:

[director@rh-laptop files]$ bash -x  rename.sh Test
+ set -x
+ '[' -z Test ']'
+ find Test -depth -name '*'
+ read file
++ dirname Test/rename.sh
+ directory=Test
++ basename Test/rename.sh
+ oldfilename=rename.sh
++ echo rename.sh
++ tr A-Z a-z
++ tr ' ' _
rename.sh: command substitution: line 15: syntax error near unexpected token `|'
rename.sh: command substitution: line 15: `| sed 's/_-_/-/g''
+ newfilename=rename.sh
+ echo 'Dry run renaming Test/rename.sh to
Test/rename.sh'
Dry run renaming Test/rename.sh to
Test/rename.sh
+ '[' rename.sh '!=' rename.sh ']'
+ read file
++ dirname 'Test/Test Test'
+ directory=Test
++ basename 'Test/Test Test'
+ oldfilename='Test Test'
++ echo 'Test Test'
++ tr A-Z a-z
++ tr ' ' _
rename.sh: command substitution: line 15: syntax error near unexpected token `|'
rename.sh: command substitution: line 15: `| sed 's/_-_/-/g''
+ newfilename=test_test
+ echo 'Dry run renaming Test/Test Test to
Test/test_test'
Dry run renaming Test/Test Test to
Test/test_test
+ '[' 'Test Test' '!=' test_test ']'
+ mv -i 'Test/Test Test'
mv: missing destination file operand after `Test/Test Test'
Try `mv --help' for more information.
+ Test/test_test
rename.sh: line 19: Test/test_test: No such file or directory
+ echo Test/Test 'Test --->
Test/test_test'
Test/Test Test --->
Test/test_test
+ read file
++ dirname Test
+ directory=.
++ basename Test
+ oldfilename=Test
++ echo Test
++ tr A-Z a-z
++ tr ' ' _
rename.sh: command substitution: line 15: syntax error near unexpected token `|'
rename.sh: command substitution: line 15: `| sed 's/_-_/-/g''
+ newfilename=test
+ echo 'Dry run renaming ./Test to
./test'
Dry run renaming ./Test to
./test
+ '[' Test '!=' test ']'
+ mv -i ./Test
mv: missing destination file operand after `./Test'
Try `mv --help' for more information.
+ ./test
rename.sh: line 19: ./test: No such file or directory
+ echo './Test --->
./test'
./Test --->
./test
+ read file
+ exit 0
Can you execute this on the command line directly

echo "Test Test" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g'

and then if it works fine, create a new file (say rename_new.sh) and copy paste the attached script in to it and try to run the new script the same way you were running the original script.
#!/bin/bash
# Convert filenames and folders to lowercase
# and replace spaces recursively
#####################################
 
if [ -z $1 ];then echo Give target directory; exit 0;fi
 
find "$1" -depth -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
        if [ "$oldfilename" != "$newfilename" ]; then
                #mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo "$directory/$oldfilename ---> $directory/$newfilename"
                #echo "$directory"
                #echo "$oldfilename"
                #echo "$newfilename"
                #echo
        fi
        done
exit 0

Open in new window

Avatar of RAH104

ASKER

When I run that command it outputs:
test_test
Did you create a new script and try that? What was the result?
Avatar of RAH104

ASKER

I just ran the command.  I don't understand the output, it doesn't look right?
The command that you ran worked just fine and it is the same command that your script runs and gives error. I am suspecting something weird in your script file (rename.sh) and that is why I am asking you to create a new script file. I want you to copy and paste the code I posted in my previous comment exactly in to a new script file (rename_new.sh) and give that a try.
Avatar of RAH104

ASKER

[director@rh-laptop files]$ bash -x  rename-new.sh Test+ '[' -z Test ']'
+ find Test -depth -name '*'
+ read file
++ dirname Test/rename.sh
+ directory=Test
++ basename Test/rename.sh
+ oldfilename=rename.sh
++ echo rename.sh
++ tr A-Z a-z
++ tr ' ' _
rename-new.sh: command substitution: line 13: syntax error near unexpected token `|'
rename-new.sh: command substitution: line 13: `| sed 's/_-_/-/g''
+ newfilename=rename.sh
+ '[' rename.sh '!=' rename.sh ']'
+ read file
++ dirname 'Test/Test Test'
+ directory=Test
++ basename 'Test/Test Test'
+ oldfilename='Test Test'
++ echo 'Test Test'
++ tr A-Z a-z
++ tr ' ' _
rename-new.sh: command substitution: line 13: syntax error near unexpected token `|'
rename-new.sh: command substitution: line 13: `| sed 's/_-_/-/g''
+ newfilename=test_test
+ '[' 'Test Test' '!=' test_test ']'
+ Test/test_test
rename-new.sh: line 15: Test/test_test: No such file or directory
+ echo 'Test/Test Test --->
Test/test_test'
Test/Test Test --->
Test/test_test
+ read file
++ dirname Test
+ directory=.
++ basename Test
+ oldfilename=Test
++ echo Test
++ tr A-Z a-z
++ tr ' ' _
rename-new.sh: command substitution: line 13: syntax error near unexpected token `|'
rename-new.sh: command substitution: line 13: `| sed 's/_-_/-/g''
+ newfilename=test
+ '[' Test '!=' test ']'
+ ./test
rename-new.sh: line 15: ./test: No such file or directory
+ echo './Test --->
./test'
./Test --->
./test
+ read file
+ exit 0
That did not help. Execute the following commands one after the other and post result.

echo $SHELL
oldfilename="Test Test"
newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
echo "$oldfilename xxxxx $newfilename"

Avatar of RAH104

ASKER

[director@rh-laptop ~]$ cd /mnt/files/Test
[director@rh-laptop Test]$ echo $SHELL
/bin/bash
[director@rh-laptop Test]$ oldfilename="Test Test"
[director@rh-laptop Test]$ newfilename=$(echo "$oldfilename" | tr 'A-Z' 'a-z' | tr ' ' '_' | sed 's/_-_/-/g')
[director@rh-laptop Test]$ echo "$oldfilename xxxxx $newfilename"
Test Test xxxxx test_test
This is all good. Use the attached script and put it in rename_new.sh and run that and post the output.

What is this mounted system? Is it a windows file system?
#!/bin/bash
# Convert filenames and folders to lowercase
# and replace spaces recursively
#####################################
 
if [ -z $1 ];then echo Give target directory; exit 0;fi
 
find "$1" -depth -name '*' | while read file ; do
        directory=$(dirname "$file")
        oldfilename=$(basename "$file")
        newfilename=$oldfilename
	echo "1 - $newfilename"
        newfilename=$(echo "$newfilename" | tr 'A-Z' 'a-z')
	echo "2 - $newfilename"
        newfilename=$(echo "$newfilename" | tr ' ' '_')
	echo "3 - $newfilename"
        newfilename=$(echo "$newfilename" | sed 's/_-_/-/g')
	echo "4 - $newfilename"
        if [ "$oldfilename" != "$newfilename" ]; then
                #mv -i "$directory/$oldfilename" "$directory/$newfilename"
                echo "$directory/$oldfilename ---> $directory/$newfilename"
                #echo "$directory"
                #echo "$oldfilename"
                #echo "$newfilename"
                #echo
        fi
        done
exit 0

Open in new window

Avatar of RAH104

ASKER

It's an NTFS file system which I used to use with Windows Vista.

[director@rh-laptop files]$ bash -x  rename_new.sh
Test+ '[' -z Test ']'
+ find Test -depth -name '*'
+ read file
++ dirname Test/rename.sh
+ directory=Test
++ basename Test/rename.sh
+ oldfilename=rename.sh
+ newfilename=rename.sh
+ echo '1 - rename.sh'
1 - rename.sh
++ echo rename.sh
++ tr A-Z a-z
+ newfilename=rename.sh
+ echo '2 - rename.sh'
2 - rename.sh
++ echo rename.sh
++ tr ' ' _
+ newfilename=rename.sh
+ echo '3 - rename.sh'
3 - rename.sh
++ echo rename.sh
++ sed s/_-_/-/g
+ newfilename=rename.sh
+ echo '4 - rename.sh'
4 - rename.sh
+ '[' rename.sh '!=' rename.sh ']'
+ read file
++ dirname 'Test/Test Test'
+ directory=Test
++ basename 'Test/Test Test'
+ oldfilename='Test Test'
+ newfilename='Test Test'
+ echo '1 - Test Test'
1 - Test Test
++ echo 'Test Test'
++ tr A-Z a-z
+ newfilename='test test'
+ echo '2 - test test'
2 - test test
++ echo 'test test'
++ tr ' ' _
+ newfilename=test_test
+ echo '3 - test_test'
3 - test_test
++ sed s/_-_/-/g
++ echo test_test
+ newfilename=test_test
+ echo '4 - test_test'
4 - test_test
+ '[' 'Test Test' '!=' test_test ']'
+ Test/test_test
rename_new.sh: line 21: Test/test_test: No such file or directory
+ echo 'Test/Test Test --->
Test/test_test'
Test/Test Test --->
Test/test_test
+ read file
++ dirname Test
+ directory=.
++ basename Test
+ oldfilename=Test
+ newfilename=Test
+ echo '1 - Test'
1 - Test
++ echo Test
++ tr A-Z a-z
+ newfilename=test
+ echo '2 - test'
2 - test
++ echo test
++ tr ' ' _
+ newfilename=test
+ echo '3 - test'
3 - test
++ echo test
++ sed s/_-_/-/g
+ newfilename=test
+ echo '4 - test'
4 - test
+ '[' Test '!=' test ']'
+ ./test
rename_new.sh: line 21: ./test: No such file or directory
+ echo './Test --->
./test'
./Test --->
./test
+ read file
+ exit 0
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Just a thought,

Do you have another Linux box to test the script in?

The script is fine, it works for me. There is something in the environment that is happening, it might be the shell, wrong input or whatever.


I cut 'n' pasted the script from the original post and it worked at once.

//jonas
There was nothing wrong with the original script and the accepted one is just a variant of it. In fact it uses exact same commands, it is just doing it one at a time. The actual problem is somewhere else and that hasn't been figured out yet.