Question

Write a script using simple shell commands. The script takes a command line argument that specifies...

Write a script using simple shell commands. The script takes a command line argument that specifies a directory dir. The script first changes directory to dir, then prints the following in sequence:

(a) A line starting “Current date and time: ”. Then on the same line, the current time and date.

(b) A line starting “Current directory is : ”. Then, on the same line, the absolute pathname of the current working directory.

(c) An empty line

(d) The line “--- 10 most recently modified directories ---”

(e) the 10 most recently modified subdirectories in the current directory in long listing format (most recent first)

(f) An empty line

(g) The line “--- 6 largest files ---”

(h) The 6 largest files in the current directory in long listing format (largest file last)

(i) An empty line, followed by a line of 70 equal signs

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I got your question. I have created a script using simple shell commands to complete the task.

Task 1: Change directory to the given command line argument. To do so, I have used if statement

if [ -d "$1" ]; then

cd $1

This checks where the given argument is a directory and changes to the given directory else print "Directory does not exist!!!"

Task 2: Current date and time. This can be done easily by date command but I have used date +"%D %T" to print current date and time.

Task 3: Current directory. This can be easily done by using pwd command. It returns the complete address to the current directory.

Task 4: 10 Most recently modified files. I have done this with the help of ls -ltp | grep "/" | head -n 10 command

ls: to list

-ltp: long list format with time-based output

grep "/" : to print only directories

head -n 10: to print top 10 of the results.

Task 5: 6 largest files. I have done this with the help of find . |ls -l | grep -Ev "total" |sort -nr|head -n 6 command

find . : to find in the current directory

ls -l: to get result in long list format

grep -Ev "total": to remove the line have "total" keyword in them

sort -nr: to sort the result on the basis of size.

head -n 6: to print top 6 of the results.

If to find the largest file and print in the long list is not required then:

find . -printf '%s %p\n'|sort -nr|head is command will give the best result.

find . -maxdepth 1 -printf '%s %p\n'|sort -nr|head this command will restrict find to max-depth of 1 sub-directory only

The final script is:

#!/bin/bash
if [ -d "$1" ]; then
cd $1
   echo "Current date and time: $(date +"%D %T")"
   echo "Current directory is: $(pwd)\n"
   echo "--- 10 most recently modified subdirectories ---"
   ls -ltp | grep "/" | head -n 10
   echo "\n"
   echo "---6 largest files ---"
   find . |ls -l | grep -Ev "total" |sort -nr|head -n 6
   echo "\n"
   for i in `seq 1 70`;
   do
   echo "*\c"
   done
   echo "\n"
else
echo "Directory not exist!!!"
fi

Below are the images of the script in vi editor and leafpad.

Output of the scripts:

Hope this would help you. If you have any further query feel free to ask.

Add a comment
Know the answer?
Add Answer to:
Write a script using simple shell commands. The script takes a command line argument that specifies...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programs/scripts 13. Write a bash shell script that displays the date by using the shell command...

    Programs/scripts 13. Write a bash shell script that displays the date by using the shell command date. 14. Write a command line program that takes a number and a command as arguments. Call that command that number of times. For example, if your program name is "test" test 3 dir <shows directory> dir <shows directory> dir <shows directory>

  • Write a bash shell script called direct.sh. This script will take an arbitrary number of command...

    Write a bash shell script called direct.sh. This script will take an arbitrary number of command line arguments. Your script should create directories starting with the first argument, then the second directory inside the first one, and then the next one inside the second one, and so on.

  • Write a shell script called GetMyStuff.sh that will provide the above information (the script must be...

    Write a shell script called GetMyStuff.sh that will provide the above information (the script must be a executable, and must work regardless of which directory it is in). Also, just saying cat MyStuff.txt is not sufficient. (1.0%) A pipeline is the combination of at least 2 commands, where the output of command1 is the input for command2. The syntax is command1 | command2 where | is the pipeline operator. Read the man pages about the ls and wc commands. Devise...

  • Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many...

    Homework No.2 CSC 222 Write a shell script program called "countf.sh" that will count how many files or directories recursively. beside the file counts, also report how many files for each types (directory or regular files). It will either take arguments for specific directories and no argument. If no argument, it will read the current working directory as the starting point. Please also provide -h option to print out how to use your countf.sh program % ./countf.sh % ./countf.sh file1...

  • SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5...

    SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5 input arguments in a for loop, but only add the even i values to sum and display the result. Problem 6: Write a shell script that accepts path of the directory and returns a count of all the files within the specified directory. (Hint: use alias to perform cd operation) Problem 7: Write a shell script that takes an ‘count’ value as an input...

  • UNIX Write and run a simple shell script name mymenu that creates a simple command menu....

    UNIX Write and run a simple shell script name mymenu that creates a simple command menu. The script should present menu items (see below), prompts the user for a selection, and then executes the appropriate command selected. [Submit your script and the results of a test case for a, b, c, and d selections]     COMMAND MENU a. Current date and time b. Name of the working directory c. Contents of the working directory Enter a, b, or c:

  • Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell...

    Unix questions, i need help on Hint: Commands to study to answer this question: predefined shell variables, and .profile script file, echo SHELL, HOME, PATH, MAIL and TERM are predefined shell variables. You can use the value of a shell variable in a shell command putting $ in front of it. For example, to display the value of the HOME directory of the user, specify $HOME in the echo command like echo $HOME. Do not give just the value of...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • QUESTION 1 What will be the output of following Unix command: find / -name ‘*’ A....

    QUESTION 1 What will be the output of following Unix command: find / -name ‘*’ A. List all files and directories recursively starting from / B. List a file names * in / C. List all files in / directory D. List all files and directories in / directory QUESTION 2 Which command is used to extract a column/field from a text file / input. A. paste B. get C. cut D. tar QUESTION 3 Which command creates an empty...

  • Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”....

    Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”. The script reports and prints (to stdout) selected storage management information. Think of it as “RePort Storage Management”. The easy way to describe what the script needs to do is to look at what it should display in the case where you provide incorrect parameters, or in the case where you provide “-h” for help: Usage: ./rpsm.sh <options>< directory> Reports selected information about specified directory...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT