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....
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...
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?
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that...
you need to submit the following files: Main.java Additionally, you need to download file ‘letter_count.csv’, that contains counts for characters in a text (see submission folder on iCollege) and put it into the root of related Eclipse project folder. To view your project folder through system file explorer, right-click on ‘src’ folder in the Eclipse project explorer and choose ‘Show In->System Explorer’. Consider the following Java code, that reads a .csv file: BufferedReader csvReader = new BufferedReader(new FileReader("letter_count.csv")); String currentRow...
Design an algorithmic flowchart for the given FASTA dataset, assuming all the files are of correct...
Design an algorithmic flowchart for the given FASTA dataset, assuming all the files are of correct format (no need to check for bad FASTA data). Given the two sequences, (A). ATCGC (B). AATTGCGCAA The outputs should be 5 and 10 respectively. Without using any code or any Python module, using a flowchart, draw and explain the above.
Consider a small business you want to create. In your post, address the following: What type...
Consider a small business you want to create. In your post, address the following: What type of business would you create? Conduct some research on companies offering a similar product. What distinguishes you from your competitors? Name 2-3 risks you need to consider in your new venture? Consider the risk factors similar companies may have encountered. How could you leverage e-commerce and social media to make your business profitable?
C++ question. I want to write all output to the file "output.txt", but it will write...
C++ question. I want to write all output to the file "output.txt", but it will write just first character. Can you fix it? #include #include #include #include #include using namespace std; using std::cin; using std::cout; using std::string; // remove dashes convert letters to upper case string normalize(const string &isbn) { string ch; for (char i : isbn) { if (i == '-') { continue; // if "-" then skip it }    if (isalpha(i)) { i = toupper(i); // Check...
Download the following zip file Bag and complete the program. You will need to complete files...
Download the following zip file Bag and complete the program. You will need to complete files MyBag and . MyBag uses the java API class ArrayList as the underline data structure. Using the class variables given, complete the methods of the MyBag class. In your BagHand class you should set appropriate default values for select class variables in the constructor in the add method, use the MyBag object to add a PlayingCard type to the bag and find and set...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT