w3resource

Bash Working with Symbolic Links exercises

1.

Create a Symbolic Link:

Write a Bash script that creates a symbolic link named "file_link" pointing to a file named "input.txt".

Code:

#!/bin/bash

# Create a symbolic link named "file_link" pointing to "input.txt"
ln -s input.txt file_link

# Check if the symbolic link was created successfully
if [ -L "file_link" ]; then
    echo "Symbolic link 'file_link' created successfully."
else
    echo "Failed to create symbolic link 'file_link'."
fi 

Output:

file_link: command not found

Explanation:

In the exercise above,

  • ln -s input.txt file_link: This command creates a symbolic link named "file_link" pointing to the file "input.txt". The -s option creates a symbolic link, and the first argument is the target file (input.txt), and the second argument is the name of the symbolic link (file_link).
  • if [ -L "file_link" ]; then ... else ... fi: This construct checks if the file "file_link" is a symbolic link using the -L option in the [ ] test condition. If it is a symbolic link, it prints a success message; otherwise, it prints a failure message.

2.

Check if a Symbolic Link Exists:

Write a Bash script that checks if a symbolic link named "file_link" exists in the current directory.

Code:

#!/bin/bash

# Check if symbolic link "file_link" exists
if [ -L "file_link" ]; then
    echo "Symbolic link 'file_link' exists in the current directory."
else
    echo "Symbolic link 'file_link' does not exist in the current directory."
fi

Output:

Symbolic link 'file_link' exists in the current directory.

Explanation:

In the exercise above,

  • [ -L "file_link" ]: This checks if there is a symbolic link named "file_link" in the current directory. The -L flag checks if the given file is a symbolic link.
  • echo "Symbolic link 'file_link' exists in the current directory.": If the symbolic link exists, this line is executed, indicating that the symbolic link exists.
  • echo "Symbolic link 'file_link' does not exist in the current directory.": If the symbolic link does not exist, this line is executed, indicating that the symbolic link does not exist.

3.

Remove a Symbolic Link:

Write a Bash script that removes a symbolic link named "file_link".

Code:

#!/bin/bash

# Remove the symbolic link "file_link"
rm file_link

# Check if the symbolic link was removed successfully
if [ ! -L "file_link" ]; then
    echo "Symbolic link 'file_link' removed successfully."
else
    echo "Failed to remove symbolic link 'file_link'."
fi

Output:

Symbolic link 'file_link' removed successfully.

Explanation:

In the exercise above,

  • rm file_link: This command removes the symbolic link named "file_link".
  • [ ! -L "file_link" ]: This condition checks if "file_link" is not a symbolic link anymore. If the symbolic link was successfully removed, this condition becomes true.
  • echo "Symbolic link 'file_link' removed successfully.": If the symbolic link was successfully removed, this message is displayed.
  • echo "Failed to remove symbolic link 'file_link'.": If the symbolic link couldn't be removed for some reason, this message is displayed.

4.

Create a Symbolic Link for a Directory:

Write a Bash script that creates a symbolic link named "dir_link" pointing to a directory named "workarea".

Code:

#!/bin/bash

# Create a symbolic link named "dir_link" pointing to "workarea"
ln -s workarea dir_link

# Check if the symbolic link was created successfully
if [ -L "dir_link" ]; then
    echo "Symbolic link 'dir_link' created successfully."
else
    echo "Failed to create symbolic link 'dir_link'."
fi

Output:

Symbolic link 'dir_link' created successfully.

Explanation:

In the exercise above,

  • ln -s workarea dir_link: This command creates a symbolic link named "dir_link" pointing to the directory "workarea".
  • [ -L "dir_link" ]: This condition checks if "dir_link" is a symbolic link. If the symbolic link was successfully created, this condition becomes true.
  • echo "Symbolic link 'dir_link' created successfully.": If the symbolic link was successfully created, this message is displayed.
  • echo "Failed to create symbolic link 'dir_link'.": If the symbolic link couldn't be created for some reason, this message is displayed.

5.

Check if a Symbolic Link Points to a Directory:

Write a Bash script that checks if a symbolic link named "dir_link" points to a directory.

Code:

#!/bin/bash

# Check if "dir_link" exists and is a symbolic link
if [ -L "dir_link" ]; then
    # Check if the target of the symbolic link is a directory
    if [ -d "dir_link" ]; then
        echo "Symbolic link 'dir_link' points to a directory."
    else
        echo "Symbolic link 'dir_link' does not point to a directory."
    fi
else
    echo "Symbolic link 'dir_link' does not exist."
fi

Output:

Symbolic link 'dir_link' points to a directory.

Explanation:

In the exercise above,

  • The first condition [ -L "dir_link" ] checks if "dir_link" exists and is a symbolic link.
  • Inside this condition, [ -d "dir_link" ] checks if the target of the symbolic link is a directory.
  • If both conditions are true, it prints "Symbolic link 'dir_link' points to a directory."
  • If the first condition is true but the second one is false, it prints "Symbolic link 'dir_link' does not point to a directory."
  • If the first condition is false, it prints "Symbolic link 'dir_link' does not exist."

6.

List Symbolic Links in the Current Directory:

Write a Bash script that lists all symbolic links in the current directory.

Code:

#!/bin/bash

# List all symbolic links in the current directory
for file in *; do
    if [ -L "$file" ]; then
        echo "$file"
    fi
done

Output:

dir_link
my_link

Explanation:

In the exercise above,

  • This script iterates over each file in the current directory.
  • For each file, it checks if it is a symbolic link using the -L flag in the if condition.
  • If the file is a symbolic link, it prints the file name.

7.

Determine the Target of a Symbolic Link:

Write a Bash script that prints the target of a symbolic link named "file_link".

Code:

#!/bin/bash

# Check if the symbolic link "dir_link" exists
if [ -L "dir_link" ]; then
    # Print the target of the symbolic link
    target=$(readlink -f "dir_link")
    echo "Target of 'dir_link': $target"
else
    echo "Symbolic link 'dir_link' does not exist."
fi 

Output:

Target of 'dir_link': /home/ad/workarea

Explanation:

In the exercise above,

  • The script first checks if the symbolic link "dir_link" exists using the -L option.
  • If the symbolic link exists, it uses the readlink -f command to get the absolute path of the target file.
  • Finally, it prints out the target of the symbolic link. If the symbolic link doesn't exist, it prints a message indicating that.

8.

Create a Symbolic Link with Absolute Path:

Write a Bash script that creates a symbolic link named "link_to_file" with an absolute path pointing to a file named "input.txt".

Code:

#!/bin/bash

# Absolute path to the file
file_path="$(pwd)/input.txt"

# Create a symbolic link named "link_to_file" with an absolute path
ln -s "$file_path" link_to_file

# Check if the symbolic link was created successfully
if [ -L "link_to_file" ]; then
    echo "Symbolic link 'link_to_file' created successfully."
else
    echo "Failed to create symbolic link 'link_to_file'."
fi

Output:

Symbolic link 'link_to_file' created successfully.

Explanation:

In the exercise above,

  • The pwd command is used to get the absolute path of the current directory.
  • The absolute path to the file "input.txt" is obtained by concatenating the current directory path with the filename.
  • The ln -s command is used to create a symbolic link named "link_to_file" with the absolute path of "input.txt".
  • The script then checks if the symbolic link was created successfully using the -L option with an if condition.

9.

Create a Symbolic Link with Relative Path:

Write a Bash script that creates a symbolic link named "link_to_file1" with a relative path pointing to a file named "output.txt".

Code:

#!/bin/bash

# Create a symbolic link named "link_to_file1" with a relative path
ln -s output.txt link_to_file1

# Check if the symbolic link was created successfully
if [ -L "link_to_file1" ]; then
    echo "Symbolic link 'link_to_file1' created successfully."
else
    echo "Failed to create symbolic link 'link_to_file1'."
fi

Output:

Symbolic link 'link_to_file1' created successfully.

Explanation:

In the exercise above,

  • ln -s: Creates a symbolic link. -s flag specifies that it creates a symbolic link.
  • output.txt: The target file that the symbolic link will point to.
  • link_to_file1: The name of the symbolic link being created.
  • [ -L "link_to_file1" ]: Tests if "link_to_file1" exists and is a symbolic link.
  • If the symbolic link exists, it prints "Symbolic link 'link_to_file1' created successfully."
  • If the symbolic link doesn't exist, it prints "Failed to create symbolic link 'link_to_file1'."

10.

Create a Symbolic Link to a Common System Directory:

Write a Bash script that creates a symbolic link named "link_to_bin1" pointing to the "/bin" directory.

Code:

#!/bin/bash

# Create a symbolic link named "link_to_bin" pointing to the "/bin" directory
ln -s /bin link_to_bin1

# Check if the symbolic link was created successfully
if [ -L "link_to_bin1" ]; then
    echo "Symbolic link 'link_to_bin1' created successfully."
else
    echo "Failed to create symbolic link 'link_to_bin1'."
fi

Output:

Symbolic link 'link_to_bin1' created successfully.

Explanation:

In the exercise above,

  • The ln -s command is used to create a symbolic link named "link_to_bin" with a relative path pointing to the "/bin" directory.
  • The script then checks if the symbolic link was created successfully using the -L option with an if condition.

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/working-with-symbolic-links.php