w3resource

Bash File and Directory Operations: Exercises, Solutions, and Explanations

1.

Create a Directory:

Write a Bash script that creates a directory named "workarea" in the current directory.

Code:

#!/bin/bash

# Create a directory named "workarea" in the current directory
mkdir workarea

# Check if the directory was created successfully
if [ $? -eq 0 ]; then
    echo "Directory 'workarea' created successfully."
else
    echo "Failed to create directory 'workarea'."
fi

Output:

Directory 'workarea' created successfully.

Explanation:

In the exercise above,

The script uses the "mkdir" command to create a directory named "workarea" in the current directory. Afterward, it checks the exit status of the "mkdir" command to determine if the directory creation was successful or not and prints an appropriate message accordingly.

2.

Copy a File:

Write a Bash script that copies a file named "error.log" to a new file named "old_error.log".

Code:

#!/bin/bash

# Check if the source file exists
if [ -f "error.log" ]; then
    # Copy the file to a new file named "old_error.log"
    cp error.log old_error.log

    # Check if the copy operation was successful
    if [ $? -eq 0 ]; then
        echo "File 'error.log' copied to 'old_error.log' successfully."
    else
        echo "Failed to copy file 'error.log' to 'old_error.log'."
    fi
else
    echo "Source file 'error.log' does not exist."
fi

Output:

File 'error.log' copied to 'old_error.log' successfully.

Explanation:

In the exercise above,

This script first checks if the source file "error.log" exists using the -f option with the "if" statement. If the source file exists, it uses the "cp" command to copy "error.log" to a new file named "old_error.log". Afterward, it checks the exit status of the "cp" command to determine if the copy operation was successful or not and prints an appropriate message accordingly. If the source file doesn't exist, it prints a message indicating that the source file doesn't exist.

3.

Move a Directory:

Write a Bash script that moves a directory named "workarea" to a new location named "new_workarea".

Code:

#!/bin/bash

# Check if the directory "workarea" exists
if [ -d "workarea" ]; then
    # Move the directory to a new location named "new_workarea"
    mv workarea new_workarea

    # Check if the move operation was successful
    if [ $? -eq 0 ]; then
        echo "Directory 'workarea' moved to 'new_workarea' successfully."
    else
        echo "Failed to move directory 'workarea' to 'new_workarea'."
    fi
else
    echo "Directory 'workarea' does not exist."
fi

Output:

Directory 'workarea' moved to 'new_workarea' successfully.

Explanation:

In the exercise above,

This script first checks if the directory "workarea" exists using the -d option with the "if " statement. If the directory exists, it uses the "mv" command to move "workarea" to a new location named "new_workarea". Afterward, it checks the exit status of the "mv" command to determine if the move operation was successful or not and prints an appropriate message accordingly. If the directory doesn't exist, it prints a message indicating that the directory doesn't exist.

4.

Delete a File:

Write a Bash script that deletes a file named "file1.txt".

Code:

#!/bin/bash

# Check if the file "file1.txt" exists
if [ -f "file1.txt" ]; then
    # Delete the file
    rm file1.txt

    # Check if the deletion was successful
    if [ $? -eq 0 ]; then
        echo "File 'file1.txt' deleted successfully."
    else
        echo "Failed to delete file 'file1.txt'."
    fi
else
    echo "File 'file1.txt' does not exist."
fi

Output:

File 'file1.txt' deleted successfully.

Explanation:

In the exercise above,

The script first checks if the file "file1.txt" exists using the -f option with the "if" statement. If the file exists, it uses the "rm" command to delete "file1.txt". Afterward, it checks the exit status of the "rm" command to determine if the deletion was successful or not and prints an appropriate message accordingly. If the file doesn't exist, it prints a message indicating that the file doesn't exist.

5.

Create Multiple Directories:

Write a Bash script that creates three directories named "dir_1", "dir_2", and "dir_3" in the current directory.

Code:

#!/bin/bash

# Create directories
mkdir dir_1 dir_2 dir_3

# Check if directories were created successfully
if [ $? -eq 0 ]; then
    echo "Directories created successfully."
else
    echo "Failed to create directories."
fi

Output:

Directories created successfully.

Explanation:

In the exercise above,

This script uses the "mkdir" command to create the directories "dir_1", "dir_2", and "dir_3" in the current directory. It then checks the exit status of the "mkdir" command to determine if the directories were created successfully or not and prints an appropriate message accordingly.

6.

Copy Multiple Files:

Write a Bash script that copies all files with the ".txt" extension from one directory to another.

Code:

#!/bin/bash

# Source directory
source_dir="source_directory"

# Destination directory
destination_dir="destination_dir"

# Create destination directory if it doesn't exist
mkdir -p "$destination_dir"

# Copy .txt files from source to destination directory
cp "$source_dir"/*.txt "$destination_dir"/

# Check if files were copied successfully
if [ $? -eq 0 ]; then
    echo "Files copied successfully."
else
    echo "Failed to copy files."
fi

Output:

Files copied successfully.

Explanation:

In the exercise above,

Replace "source_directory" and "destination_dir" with the appropriate paths to your source and destination directories. This script first creates the destination directory if it doesn't exist using mkdir -p. Then, it uses "cp" to copy all files with the ".txt" extension from the source directory to the destination directory. Finally, it checks the exit status of the "cp" command to determine if the files were copied successfully and prints an appropriate message.

7.

Move Multiple Directories:

Write a Bash script that moves multiple directories named "dir1", "dir2", and "dir3" to a new location named "new_dir".

Code:

#!/bin/bash

# Move directories to a new location
mv dir_1 dir_2 dir_3 new_dir/

# Check if directories were moved successfully
if [ $? -eq 0 ]; then
    echo "Directories moved successfully."
else
    echo "Failed to move directories."
fi

Output:

Directories moved successfully.

Explanation:

In the exercise above,

The script assumes that "dir_1", "dir_2", and "dir_3" are located in the current directory and moves them to the "new_dir" directory. If any of the directories do not exist or if there are any other issues during the move operation, an appropriate message is displayed.

8.

Delete Multiple Files:

Write a Bash script that deletes all files with names starting with "error" in the current directory.

Code:

#!/bin/bash

# Delete files starting with "error_"
rm error*

Explanation:

In the exercise above,

The script uses the "rm" command with the wildcard * to match and delete all files in the current directory that start with "error". Be cautious when running this script, as it will permanently delete the matched files without confirmation.

9.

Write a Directory Hierarchy:

Write a Bash script that creates a directory hierarchy with the following structure:

parent_directory

|- workarea
|   |- subworkarea1
|   |- subworkarea2
|- workarea2

Code:

#!/bin/bash

# Create parent directory
mkdir parent_directory

# Create subdirectories within parent_directory
mkdir parent_directory/workarea
mkdir parent_directory/workarea/subworkarea1
mkdir parent_directory/workarea/subworkarea2
mkdir parent_directory/workarea2

The script first creates the parent directory parent_directory, then creates the necessary subdirectories within it to match the desired structure.

10.

Copy Directory Contents:

Write a Bash script that copies all files and subdirectories from one directory to another, preserving the directory structure.

Code:

#!/bin/bash

# Source directory
source_dir="/path/to/source_directory"

# Destination directory
destination_dir="/path/to/destination_directory"

# Copy files and subdirectories from source to destination preserving structure
cp -R "$source_dir"/* "$destination_dir"

Explanation:

In the above script replace /path/to/source_directory with the path to the source directory containing the files and subdirectories you want to copy, and /path/to/destination_directory with the path to the destination directory where you want to preserve the directory structure and copy the files and subdirectories.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/bash-script-exercises/creating-copying-moving-and-deleting-files-and-directories.php