Previous | Table of Contents | Next |
What command can I use to rename all the aaa* files to bbb* files?
The technique used in the last question can be used to solve this problem as well. In this case you will use the variables OLDPREFIX to hold the prefix a file currently has and NEWPREFIX to hold the prefix you want the file to have.
As an example, you can use the following for loop to rename all files that start with aaa to start with bbb instead:
OLDPREFIX=aaa NEWPREFIX=bbb for FILE in "$OLDPREFIX"* do NEWNAME=`echo "$FILE" | sed -e "s/^${OLDPREFIX}/$NEWPREFIX/"` mv "$FILE" "$NEWNAME" done
How can I set my filenames to lowercase?
When you transfer a file from a Windows or DOS system to a UNIX system, the filename ends up in all capital letters. You can rename these files to lowercase using the following command:
for FILE in * do mv -i "$FILE" `echo "$FILE" | tr '[A-Z]' '[a-z]'` 2> /dev/null done
You are using the mv -i command here in order to avoid overwriting files. For example, if the files APPLE and apple both exist in a directory you do not want to rename the file APPLE.
How do I eliminate carriage returns (^M) in my files?
If you transfer text files from a DOS machine to a UNIX machine, you might see a ^M before the end of each line. This character corresponds to a carriage return.
In DOS a newline is represented by the character sequence \r\n, where \r is the carriage return and \n is newline. In UNIX a newline is represented by \n. When text files created on a DOS system are viewed on UNIX, the \r is displayed as ^M.
You can strip these carriage returns out by using the tr command as follows:
tr -d '\015' < file > newfile
Here file is the name of the file that contains the carriage returns, and newfile is the name you want to give the file after the carriage returns have been deleted.
Here you are using the octal representation \015 for carriage return, because the escape sequence \r will not be correctly interpreted by all versions of tr.
In this chapter I have looked at some common questions encountered in shell programming. These questions and their answers will help you write bigger and better scripts.
Now that you have finished all 24 chapters, you have learned about using both the basics of the shell and its advanced features. As you continue to program, use this book as a reference to help you remember the intricacies of shell programming.
I hope that you learned not only to program efficiently using the shell but also to enjoy shell programming.
Previous | Table of Contents | Next |