CHC4010 DevOps Week 6 Practical: Shell Scripting

Part I. Display content list

  1. Choose a directory on your computer that contains a lot of files and child directory. Get the absolute path of this directory. In your shell, display the content list of this directory using its absolute path.

Note: if you computer is running MacOS or Linux, you need to use the command ls -l to get the detailed information of each item in the directory.

  1. Create a shell script and put the command you just used into this script. Run the script and see that it generates the same output. Note: refer to page 10 of the lecture slide for how to execute shell scripts in Unix shell and in Windows Powershell.

  2. Instead of directly writing the path of the target directory in the script, make the script accept a parameter for the path of the directory whose content list will be displayed.

Part II. Filter files by size

  1. Look at the content list output from our script. How are files and subfolders listed differently?

  2. Create a Java class FileFilter.java and save it in the same location as your shell script. In the main method of this class, receive the content list of a directory as standard input (System.in) and print out only the files (not the folders). Modify your shell script and use piping to make the output of ls become the input of java FileFilter.java.

Example target directory:

Name
Date Modified
Size
Kind

pycache_

2023/9/15

Folder

blackbox

2023/9/15

Folder

coffee_can.pdf

2023/9/15

223 KB

PDF Document

dice_game

12:35

Folder

errors

2024/3/4

Folder

mark.py

2023/9/15

518...ytes

Python Scripte

mountain_array.py.

12:35

442...ytes

Python Script

notes.md

2023/9/15

3 KB

Document

Print.java

2023/9/15

119...ytes

Java so...e code

solution

2024/3/8

Folder

test.py

2024/3/4

622...ytes

Python Script

valid_mountain_array.py

2024/3/8

2 KB

Python Script

Example output:

  1. Modify FileFilter.java so that it prints out only the files whose size is bigger than 1000 bytes (or choose an appropriate number).

  2. Instead of hardcoding the maximum size 1000 in the java code, pass it as a second parameter into the shell script, which in turn should use it as an argument when running the Java file. We know that the main method of a Java class is defined like this: public static void main(String[] args) If you run the java file with arguments: java <file_path> a b c then inside the main method, the parameter args with be an array of Strings containing 3 elements: ${ ^ { \prime \prime } \mathsf { a } ^ { \prime \prime } , ^ { \prime \prime } \mathsf { b } ^ { \prime \prime } , ^ { \prime \prime } \mathsf { c } ^ { \prime \prime } }$ Run the shell script with different values for the second parameter and observe the different output.

  3. Modify the FileFilter.java to output only the name of files and modify the shell script to redirect the output into a file big_files.txt.

Part III. Back up big files

  1. Create a new folder called “big_files_backup”, and set the environment variable big_files_back_up_path to the absolute path of this folder. Read the environment variable from your shell to check that it’s actually saved.

  2. In your shell script, read from big_files.txt and copy each file listed here into the folder we just created, using the environment variable. Note: big_files.txt contains only the file name, so you need the path of the containing directory in order to locate these files.

[!TIP] đź”— Solution codes

Last updated