0% found this document useful (0 votes)
11 views

Linux LAB

Uploaded by

vansh patade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Linux LAB

Uploaded by

vansh patade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB PROGRAMS

PROGRAM 3:
Create a script that displays the disk space usage of each directory in the
current location

 display_disk_usage is a function that takes two parameters:

 path: the directory path to calculate disk usage.


 level: the depth level of the directory (used for indentation).

 indent is a variable that generates spaces for indentation. The deeper the directory level,
the more spaces are added.
 du -sh "$path" calculates the size of the directory in a human-readable format (-sh).
 2>/dev/null suppresses any error messages.
 cut -f1 extracts the size from the du output.
 echo prints the directory path and its size with the appropriate indentation.
 The for loop iterates over all items in the directory ("$path"/*/), matching only
directories ([ -d "$dir" ]).
 For each subdirectory, the function calls itself recursively, increasing the indentation level.
 main is the main function that initializes the script.
 current_directory=$(pwd) gets the current working directory.
 echo prints a header indicating the start of disk usage reporting.
 display_disk_usage "$current_directory" 0 starts the recursive process from the
current directory with an initial indentation level of 0.

 main function to start the script.

PROGRAM 4:

Write a script that compresses a given file using both gzip and bzip2, creating two
compressed versions.
Running the Script:

1. Save the script to a file, e.g., compress_file.sh.


2. Open a terminal.
3. Navigate to the directory containing compress_file.sh.
4. Make the script executable: chmod +x compress_file.sh.
5. Run the script with the file you want to compress as an argument:
./compress_file.sh yourfile.txt.

WORKING
 "$#": The special variable that holds the number of arguments passed to the script.
 [ "$#" -ne 1 ]: Checks if the number of arguments is not equal to 1.
 If the condition is true (i.e., the script does not receive exactly one argument), it prints the
usage message and exits with a status code of 1.

 file_to_compress=$1: Assigns the first argument to the variable file_to_compress.

 [ ! -f "$file_to_compress" ]: Checks if the file does not exist.


 If the file does not exist, it prints an error message and exits with a status code of 1.
 gzip -c "$file_to_compress": Compresses the file using gzip, with -c option to write
the output to standard output.
 > "${file_to_compress}.gz": Redirects the output to a new file with a .gz extension.
 $?: Special variable that holds the exit status of the last executed command.
 [ $? -eq 0 ]: Checks if the previous command was successful (exit status 0).
 If the compression is successful, it prints a success message. If not, it prints an error
message and exits with a status code of 1.
 bzip2 -k "$file_to_compress": Compresses the file using bzip2, with -k option to
keep the original file.
 $?: Special variable that holds the exit status of the last executed command.
 [ $? -eq 0 ]: Checks if the previous command was successful (exit status 0).
 If the compression is successful, it prints a success message. If not, it prints an error
message and exits with a status code of 1.

You might also like