I wrote a small shell script based on an example I found here: https://bbs.archlinux.org/viewtopic.php?id=36305
it takes this:
bash-3.2$ ls
test 001 test 002 test 003 test 004
and turns it into:
bash-3.2$ ls
001 002 003 004 rename.sh
However it gives me this error (even though it works):
bash-3.2$ ./rename.sh
mv: missing destination file operand after `rename.sh'
Try `mv --help' for more information.
`test 001' -> `001'
`test 002' -> `002'
`test 003' -> `003'
`test 004' -> `004'
Though it works correctly, it would be nice to see where I messed up, I assumed by default it would put the files in the same directory (this is the desired output).
#!/bin/bash
ls | while read -r FILE
do
mv -v "$FILE" `echo $FILE | awk -F ' ' '{print $2}'`
done
Thanks in advance for helping me correct my incorrect code.
Postman almost has it. For rename.sh, the awk command returns nothing so in effect you have the following command the shell attempts to execute:
hence the error message “missing destination file”
you can fix this by testing for the filename of the script, either hardcoded or $0, and executing the mv command only if $FILE does equal the script name.