Introduction
In this lab, you will learn how to perform various file tests in the shell. File tests are essential tools for checking the properties of files and directories in the file system. By the end of this lab, you will be familiar with common file test commands and their usage, which are fundamental skills for working with files in Linux environments.
Creating a Test File
Before we begin with file operations, it's important to understand our working environment. In Linux, you're always working within a specific directory, and it's crucial to know where you are in the file system.
Open a terminal in the WebIDE. This is where you'll type your commands.
Create a new file named
test_file.txt:touch test_file.txtThe
touchcommand is used to create an empty file. If the file already exists, it updates the file's timestamp without changing its content.Add some content to the file:
echo "This is a test file for our lab." > test_file.txtThis command uses
echoto output the text, and>to redirect that output into the file. Be careful with>, as it will overwrite any existing content in the file.Verify the file contents:
cat test_file.txtcatis short for "concatenate", but it's often used to display the contents of a file. You should see the message "This is a test file for our lab."
If you make a mistake or want to start over, you can always remove the file with rm test_file.txt and start again from step 1.
Testing File Existence
Now that we have created a file, let's learn how to check if a file exists. This is a common task in shell scripts, especially when you need to perform operations on files.
Create a new script file named
file_exists.sh:touch file_exists.shAdd the following content to the file:
#!/bin/bash filename="test_file.txt" if [ -e "$filename" ]; then echo "$filename exists" else echo "$filename does not exist" fiLet's break this down:
#!/bin/bashis called a shebang. It tells the system this is a bash script.- We set a variable
filenameto "test_file.txt". - The
ifstatement checks if the file exists.-eis a test that returns true if the file exists. - We use
echoto print a message based on whether the file exists or not.
Save the file and exit the editor.
Make the script executable:
chmod +x file_exists.shRun the script:
./file_exists.shYou should see the output: "test_file.txt exists"
Now, let's test with a non-existent file. First, we'll rename our test file:
mv test_file.txt non_existent.txtThis command renames
test_file.txttonon_existent.txt.Modify the script to check for the original file name "test_file.txt":
nano file_exists.shChange the
filenamevariable to "test_file.txt" if it's not already set to that.Run the script again:
./file_exists.shYou should see the output: "test_file.txt does not exist"
This script demonstrates how to check for file existence, which is crucial when your script needs to work with files that may or may not be present.
Testing Directory Existence
Similar to testing file existence, we can also check if a directory exists. This is useful when your script needs to work with directories that may or may not be present.
Create a new script file named
dir_exists.sh:touch dir_exists.shAdd the following content to the file:
#!/bin/bash dirname="test_directory" if [ -d "$dirname" ]; then echo "$dirname exists" else echo "$dirname does not exist" fiThis script is very similar to our file existence script, but it uses
-dinstead of-e. The-dtest checks specifically for directory existence.Save the file and exit the editor.
Make the script executable:
chmod +x dir_exists.shRun the script:
./dir_exists.shYou should see the output: "test_directory does not exist"
Now, let's create the directory and run the script again:
mkdir test_directory ./dir_exists.shYou should now see the output: "test_directory exists"
mkdiris the command to create a new directory.
This script demonstrates how to check for directory existence. This can be particularly useful in scripts that need to create, modify, or delete directories.
Testing File Permissions
In Linux, every file and directory has associated permissions that determine who can read, write, or execute them. In this step, we'll learn how to check file permissions, specifically if a file is readable.
First, let's rename our file back to its original name:
mv non_existent.txt test_file.txtCreate a new script file named
file_readable.sh:touch file_readable.shAdd the following content to the file:
#!/bin/bash filename="test_file.txt" if [ -r "$filename" ]; then echo "You have read permission for $filename" else echo "You do not have read permission for $filename" fiThis script uses the
-rtest, which checks if the file is readable by the current user.Save the file and exit the editor.
Make the script executable:
chmod +x file_readable.shRun the script:
./file_readable.shYou should see the output: "You have read permission for test_file.txt"
Now, let's remove the read permission and run the script again:
chmod -r test_file.txt ./file_readable.shYou should now see the output: "You do not have read permission for test_file.txt"
chmod -rremoves read permissions from the file.Restore the read permission:
chmod +r test_file.txtIt's important to restore the permissions so we don't accidentally leave our file unreadable.
This script demonstrates how to check file permissions. Understanding and managing file permissions is crucial for system security and proper functioning of scripts.
Summary
In this lab, you learned how to perform essential file system operations in the shell. You created scripts to test for file and directory existence, as well as file permissions. These skills are fundamental for working with files and directories in Linux environments and form the basis for more complex shell scripting tasks.
You practiced:
- Understanding your working environment
- Creating and manipulating files
- Writing and executing shell scripts
- Testing for file and directory existence
- Checking file permissions
These skills will be valuable as you continue to work with Linux systems and develop more advanced shell scripts. Remember to always check file and directory existence before performing operations on them to avoid errors in your scripts. Also, be mindful of file permissions when working with sensitive data or system files.
As you progress, you might want to explore more advanced file tests, learn about other shell scripting constructs like loops and functions, and practice combining these concepts to create more complex and powerful scripts.



