CHC4010 DevOps Week 6 Practical: Shell Scripting
Part I. Display content list
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.
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.
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
Look at the content list output from our script. How are files and subfolders listed differently?
Create a Java class
FileFilter.javaand 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 oflsbecome the input ofjava FileFilter.java.
Example target directory:
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:
Modify
FileFilter.javaso that it prints out only the files whose size is bigger than 1000 bytes (or choose an appropriate number).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 cthen 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.Modify the
FileFilter.javato output only the name of files and modify the shell script to redirect the output into a filebig_files.txt.
Part III. Back up big files
Create a new folder called “big_files_backup”, and set the environment variable
big_files_back_up_pathto the absolute path of this folder. Read the environment variable from your shell to check that it’s actually saved.In your shell script, read from
big_files.txtand copy each file listed here into the folder we just created, using the environment variable. Note:big_files.txtcontains only the file name, so you need the path of the containing directory in order to locate these files.
[!TIP] đź”— Solution codes
Last updated