Question

In: Computer Science

Consider that you want to rename all files of a given type (e.g. file extension) to...

Consider that you want to rename all files of a given type (e.g. file extension) to a different filetype. For example, you want to change the file extension of all txt files to text. Write a shell script rename.sh (3%) that:

  • takes two options as arguments. The first argument is the filetype you want to change. The second argument is the filetype you want to change files into.
  • check that the two arguments are actually given. If not, the script is supposed to return a error message and terminate (0.5%).
  • In the next step the script needs to find the corresponding filenames. Each file that is considered needs to be checked if it is a file (0.5%) and has read and write permissions (0.5%).
  • Once files are found their file extensions need to change: For example, file.txt needs to change to file.text. HINT: check the man page of the command sed (1%).
  • Finally, the script needs to output something to the effect of notifying the user that file.txt has been changed to file.text (0.5%).

Solutions

Expert Solution

Explanation :
1) Check for the no of arguments
if [ $# -ne 2 ];then                                                                                                                                   
echo "Provide two args [file 1: the original file] [file 2 : file name that the original file needs to be changed to]"                                                                                                                                 
exit 1                                                                                                                                                 
fi
checks for whether the no of args provided is atleast two or not
2) File Check : It checks whether the file to be renamed is present or not along with the extension check
Note : I have made check for only .txt
if [ -f "$file" ]                                                                                                                                      
then                                                                                                                                                   
if [ ${file: -4} == ".txt" ]                                                                                                                           
then                                                                                                                                                   
echo "file extension is .txt"                                                                                                                          
else                                                                                                                                                   
echo "file extension should be .txt only"                                                                                                              
fi                                                                                                                                                     
echo "$file found"                                                                                                                                     
else                                                                                                                                                   
echo "$file not found"                                                                                                                                 
exit 1                                                                                                                                                 
fi                                 
3) Check for Read and Write Permissions : The below code checks whether the file to be renamed has write and read permissions.
if [ -w $1 ] && W="Write"                                                                                                                              
then                                                                                                                                                   
echo "$file is having write permission"                                                                                                                
else                                                                                                                                                   
echo "$file is not having write permission"                                                                                                            
exit 1                                                                                                                                                 
fi                                                                                                                                                     
if [ -r $1 ] && R="Read"                                                                                                                               
then        
echo "$file is not having read permission"                                                                                                             
exit 1                                                                                                                                                 
fi                    
4) Renaming File using sed command
for filename in $1; do                                                                                                                             
newFilename=$(sed -E 's#img_([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})_(.*)$#newyears_20\3-\2-\1_\4#' <<< "$2")                                          
mv "$1" "$newFilename"                                                                                                                               
echo "File has been renamed successfully"                                                                                                            
done   
Steps to be followed :
Step 1 : Open vi editor or any other editor based on the linux system you are using and copy the following content into the file rename.sh
if [ $# -ne 2 ];then                                                                                                                                   
echo "Provide two args [file 1: the original file] [file 2 : file name that the original file needs to be changed to]"                                                                                                                                
exit 1                                                                                                                                                 
fi                                                                                                                                                     
file=$1                                                                                                                                                
if [ -f "$file" ]                                                                                                                                      
then                                                                                                                                                   
if [ ${file: -4} == ".txt" ]                                                                                                                           
then                                                                                                                                                   
echo "file extension is .txt"                                                                                                                          
else                                                                                                                                                   
echo "file extension should be .txt only"                                                                                                              
fi                                                                                                                                                     
echo "$file found"                                                                                                                                     
else                                                                                                                                                   
echo "$file not found"                                                                                                                                 
exit 1                                                                                                                                                 
fi                                                                                                                                                     
if [ -w $1 ] && W="Write"                                                                                                                              
then                                                                                                                                                   
echo "$file is having write permission"                                                                                                                
else                                                                                                                                                   
echo "$file is not having write permission"                                                                                                            
exit 1                                                                                                                                                 
fi                                                                                                                                                     
if [ -r $1 ] && R="Read"                                                                                                                               
then        
echo "$file is not having read permission"                                                                                                             
exit 1                                                                                                                                                 
fi                                                                                                                                                     
for filename in $1; do                                                                                                                             
newFilename=$(sed -E 's#img_([0-9]{1,2})-([0-9]{1,2})-([0-9]{1,2})_(.*)$#newyears_20\3-\2-\1_\4#' <<< "$2")                                          
mv "$1" "$newFilename"                                                                                                                              
echo "File has been renamed successfully"                                                                                                            
done                                                                                                                                                               
Step 2 : execute the command ./rename.sh file.txt file.text
Step 3 : Kindly execute the above command for various test cases that is mentioned in the problem description.
Output:
sh-4.4$ ./rename.sh file.txt file.text                                                                                                                 
file extension is .txt                                                                                                                                 
file.txt found                                                                                                                                         
file.txt is having write permission                                                                                                                    
file.txt is having read permission                                                                                                                     
File has been renamed successfully                                                                                                                     
sh-4.4$ vi rename.sh


Related Solutions

Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named...
Java Chapter 12.29 (Rename files) suppose you have a lot of files in a directory named Exercisei_j, where i and j are digits. Write a program that pads a 0 before j if j is a single digit. For example, a file named Exercise2_1 in a directory will be renamed to Exercise2_01. In Java, when you pass the symbol * from the command line, it refers to all files in the directory (see Supplement III.V). Use the following command to...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
repare a single compressed file in .zip format containing all the source files (.java files, NOT...
repare a single compressed file in .zip format containing all the source files (.java files, NOT .class files) and the output file generated by running your code (i.e., testsOutput.txt) and submit it through Moodle. To be more precise, your submitted .zip file should contain the following files: PhoneCard.java, SuperNA10Card, Global10Card, Global25Card, SuperPhoneCardInc.java, CardTable.java, CallZone.java, and testsOutput.txt. Note: (1) Your assignment will be given a zero mark if only the compiled files (.class files) are submitted. No exceptions. Please make sure...
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file....
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an array of objects and practice passing objects to and return objects from functions. String functions practice has also been included. Extend the BankAccount class. The member fields of the class are:...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
Consider a file system that uses inodes to represent files. Diskblocks are 8 kb in size,...
Consider a file system that uses inodes to represent files. Diskblocks are 8 kb in size, and a pointer to a disk block requires 4bytes. this file system has 12 direct disk blocks, as well assingle, double, and triple in direct disk blocks. What is themaximum size of a file that can be stored in this file system?Explain a little bit^^ Thank you
Consider the file “PatientTemperature_CSV.csv” given to you representing patient body temperatures at a hospital on a...
Consider the file “PatientTemperature_CSV.csv” given to you representing patient body temperatures at a hospital on a particular day. The data is organized into two columns: patient temperature and patient age. Using code snippets available from the class lecture material or otherwise, perform the following tasks with this data in python: [80 points] Read the data into Python Determine the mean, mode, median, variance, and standard deviation for the patient temperatures. Determine the mean, median, variance, and standard deviation for the...
C programming language only! a file is given that has comma-separated integers. the files contents are...
C programming language only! a file is given that has comma-separated integers. the files contents are -1,-9,1,45,3,2,1,-1... Create a function that takes as an input a filename and returns an array containing the list of integers in the file
I have multiple .txt files in a folder, and i want to combine all their contents...
I have multiple .txt files in a folder, and i want to combine all their contents into a single .txt file How do I go about such an operation in Python?
a) What types of files might you want to copy to the cloud? Why would you...
a) What types of files might you want to copy to the cloud? Why would you copy files to the cloud instead of copying them to an external storage device? b) How does the access time of storage compare with the access time of memory? c) End user programs are designed for specific functions such as word processing or a game. You have installed a new piece of applications software onto a stand-alone PC. You then find that the printer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT