David, On Saturday 03 December 2005 21:48, David Crouch wrote:
...
Let me clarify. I want to strip the first 8 characters off each file in a given file name. Sorry for the confusion. Cut works, partly, I've figured out how to strip the charazcters, but not how to rename with the results.
Here's one solution (call it "chop8"): -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- #!/bin/bash --norc for arg; do case "$arg" in ?????????*) newName="${arg#????????}" # echo "old=\"$arg\"; new=\"$newName\"" if [ -f "$newName" ]; then echo "chop8: Chopped name \"$newName\" names an existing file" >&2 continue; else mv "$arg" "$newName" fi ;; *) echo "chop8: File name \"$arg\" is less than nine characters long" >&2 ;; esac done -==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==- Testing: % touch 12345678ABC 12345678BCD 12345678CDE 1 12 123 1234 12345 123456 1234567 12345678 123456789 87654321ABC 87654321BCD 87654321CDE % ls -1 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678ABC 12345678BCD 12345678CDE 87654321ABC 87654321BCD 87654321CDE % chop8 * chop8: File name "1" is less than nine characters long chop8: File name "12" is less than nine characters long chop8: File name "123" is less than nine characters long chop8: File name "1234" is less than nine characters long chop8: File name "12345" is less than nine characters long chop8: File name "123456" is less than nine characters long chop8: File name "1234567" is less than nine characters long chop8: File name "12345678" is less than nine characters long old="123456789"; new="9" old="12345678ABC"; new="ABC" old="12345678BCD"; new="BCD" old="12345678CDE"; new="CDE" old="87654321ABC"; new="ABC" old="87654321BCD"; new="BCD" old="87654321CDE"; new="CDE" Now, running it for real (echo commented, comment on mv removed): % chop8 * chop8: File name "1" is less than nine characters long chop8: File name "12" is less than nine characters long chop8: File name "123" is less than nine characters long chop8: File name "1234" is less than nine characters long chop8: File name "12345" is less than nine characters long chop8: File name "123456" is less than nine characters long chop8: File name "1234567" is less than nine characters long chop8: File name "12345678" is less than nine characters long chop8: Chopped name "ABC" names an existing file chop8: Chopped name "BCD" names an existing file chop8: Chopped name "CDE" names an existing file % ls -1 1 12 123 1234 12345 123456 1234567 12345678 87654321ABC 87654321BCD 87654321CDE 9 ABC BCD CDE
David Crouch
Randall Schulz