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...
Linux Sign into your lab account Write a bash shell script that does the following: Print...
Linux Sign into your lab account Write a bash shell script that does the following: Print out a friendly welcome message Ask the user for the name of their favorite animal Grep that animal from a text file noises.txt Tell the user what that animal says (i.e. what noise does it make) If the animal does not exist ask the user what noise the animal makes Store that new information in noises.txt
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within...
Part 2 – Scripting Goals:  Write a bash script  Use linux shell commands within your script  Provide user feedback  Use loops  Use conditionals Remember to use chmod +x to make your file executable! Each script is 5 points in the Specifications portion of the rubric. Don’t forget to maintain good standards and comments. Script 1 – Echo-back some information Write a script name hello.sh that will take the user’s first name as a command line...
Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
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...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed to achieve a certain distance. The program should take in the amount of fencing needed from the user, then ask them the size of the smaller piece. The fencing is to be made up of two sizes of fencing, the smaller being two feet less than the larger. Then, the program will tell the user how many of each piece is needed. However, I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT