Question

In: Computer Science

Create an executable bash script named sync. You may find file name pattern matching and the...

Create an executable bash script named sync. You may find file name pattern matching and the basename command useful. In fact, the sync-skeleton file contains part of the solution to this problem. You may choose to use it anyway you wish. You'll have to create your own test directories (using the mkdir command) and files (using the touch command) to test this script.

You should research the options to the cp command in its main page.

  1. The script takes TWO ARGUMENTS. Let's call them dir1 and dir2. If two arguments are not given display an error message to that effect and exit.

  2. Verify that both given arguments are paths to directories. If one or neither argument is a path to a directory, display an error message to that effect and exit.

  3. If a file exists in dir1 but not in dir2, copy the file from dir1 to dir2 preserving its timestamp. Display a message saying what's being done.

  4. If a file exists in dir2 but not in dir1, copy the file from dir2 to dir1 preserving its timestamp. Display a message saying what's being done.

  5. If a file is in both directories but is newer in dir1 than the file in dir2, copy the file from dir1 to dir2 preserving its timestamp. Display a message saying what's being done.

  6. If a file is in both directories but is newer in dir2 than the file in dir1, copy the file from dir2 to dir1 preserving its timestamp. Display a message saying what's being done.

Solutions

Expert Solution

#!/bin/bash


if [ $# -ne 2 ]
then
   echo "Please provide two arguments"
   exit
fi

if [ -e $1 ] && [ -e $2 ]
then
   if [ -e $1/* ]
   then
       cp -p $1/* $2/
       echo "file copied from $1 to $2"
   else  
       echo "There is no file in the $1"  
   fi

   if [ -e $2/* ]
   then
       cp -p $2/* $1/
       echo "file copied from $2 to $1"
   else  
       echo "There is no file in the $2"  
   fi
else
   echo "directory not exist"
fi

if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and we can edit and change the answers if you argue, thanks :)


Related Solutions

Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
create an executable bash runtests.sh as follows: The script must include the definition of a recursive...
create an executable bash runtests.sh as follows: The script must include the definition of a recursive function, explore The script prompts the user to enter a directory name and an executable name, and checks that they are both readable and executable (terminating with an error message if not). It then passes the two names to the explore function. The explore function carries out a recursive traversal of the directory and all its subdirectories, and runs the executable on each of...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Write a bash script that will allow you to: Read in your Your name Course name...
Write a bash script that will allow you to: Read in your Your name Course name and Instructor’s name Then prompt the user to enter two numbers and determine which number is greater. Ex: If 10 is entered for the first number and 3 for the second, you should output Your name Course name Instructor’s name 10 is greater than 3. If 33 is entered for the first number and 100 for the second, you shou output Your name Course...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Create a file and change the file permissions to (a) make it executable, (b) enable the...
Create a file and change the file permissions to (a) make it executable, (b) enable the SUID, and (c) use the ACL to add a permission for the root user. Do a long listing of this file. LINUX command line / virtual box.
Write a bash script file that tests each file entry in the current directory. It should...
Write a bash script file that tests each file entry in the current directory. It should determine if it is a file or directory. If it is a file, it will determine if it is readable or not. If it is readable, it will display only the first 4 lines to the terminal in sorted order (just sort the first 4 lines). If it is a directory, it should display the contents of that directory (the files and subdirectories). NOTE:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT