Question

In: Computer Science

I am attempting to write a script using bash on Linux. My program must save the...

I am attempting to write a script using bash on Linux. My program must save the long list format of the parent directory to a text file. Then, it must print how many items are in this parent directory. Then, it must sort the file in reverse order and save it to a different text file.

I understand that the parent directory is accessed using cd .., and I understand that a file can be written to by echoing and using >> fileName, and that |wc -l can be used to count the lines, but I am otherwise lost.

Any help would be greatly appreciated, thank you.

Solutions

Expert Solution

Unix/Linux programs are like Lego blocks. They do one thing, and they do it well. Using these blocks we can build a desired script. So lets see, what kind of programs / Lego blocks we have.

ls -l

Long list files and folders in the current working directory.

ls -l ..

Long list files and folders in the parent directory of the current working directory.

ls -l .. > filelist,txt

Save the long list files and folders in the parent directory to filelist.txt.

ls -1

List files in current directory (one per line) (output to standard output ./ terminal ).

ls -1 | wc -l

Pipe the output of one program ( ls -1 ) to another program ( wc -l ), this will count the number of lines in the output from previous program (list of files in current working directory) and thus we will get the number of files in current working directory. The program on the left executes first, then second and so on.

ls -1 .. | wc -l

Count the number of items in the parent directory.

sort names.txt

Sorts a file names.txt and displays on terminal.

sort -r names.txt

Reverse sorts a file names.txt and displays on terminal.

sort -r filelist.txt > sorted_filelist.txt

Reverse sorts the filelist that we created earlier and save it as a new file sorted filelist.txt

Putting together all the above examples we write the following script:

#!/bin/bash
ls -l .. > filelist.txt
ls -1 .. | wc -l
sort filelist.txt > sorted_filelist.txt

Once you run this script, you'll see the count of items on your screen and two files 'filelist.txt' and 'sorted_filelist.txt' will appear in the current working directory.

Feel free the comment any queries regarding this solution or somehow if it doesn't work for you. :)


Related Solutions

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.
i want to write bash script that generates syslog if my machine has been pinged this...
i want to write bash script that generates syslog if my machine has been pinged this is my code, but it is not full and not work as i want #!/bin/bash status=`echo "$?"` monitor=`sudo tcpdump -i eth0 icmp and icmp[icmptype]=icmp-echo -n` if [ "$status" -eq 0 ]; then sleep 5s pkill -f "$0" `echo "$monitor" | awk '{print $3}' fi
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask...
Bash Script Write a script using while-do-done loop to convert the kilometers to miles. - Ask the user to input the number of kilometers they need to travel. - Display the number of equivalent miles for the number. Use formula, Kilometers = miles/0.62137 - Every time the loop runs, it should ask the user if they want to continue, if user enters “Y” or “y”, then the loop runs again, if user enters “N” or “n”, then stop the loop...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user...
Cannot figure out why my c++ program keeps crashing, I am attempting to have the user decide the size of a 2d array then fill said array. #include <iostream> using namespace std; typedef int* IntArrayPtr; int main(void) { int column, row, i, j; IntArrayPtr *arr_num = new IntArrayPtr[row]; cout << " " << endl << " This program creates a 2D array of dynamic memory, fills it with user entered numbers, " << endl << " Then prints the array...
Fully Functional Script written in BASH In this section, you will write a fully functional script...
Fully Functional Script written in BASH In this section, you will write a fully functional script to help your manager with an important project that will build upon what you learned from your script work in Milestone Four. After you have written the script, you will execute it to generate a report for all three users (once for Bob, once for Henry, and once for Frank). You should have three unique generated reports in your ~/scripts directory (or more if...
The name of your bash script must be script1 and below is a description of what...
The name of your bash script must be script1 and below is a description of what it should do when executed. The script displays a message Guess, what is zip program's location Then it displays a message Enter the full path e.g. /etc/zip or /proc/bus/zip Then it reads the users response If the user's response is correct, it displays a message Great guess, zip's location is XXX, where XXX is the correct location of zip , and the script terminates....
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...
A C PROGRAM *Edit/Update I do not need the file loading script, but I am not against it being included in the answer* I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT