Bash Listing Files and Directories: Exercises, Solutions, and Explanations
1.
Write a Bash script that lists all files and directories in the current directory.
Code:
#!/bin/bash
# List all files and directories in the current directory
ls -al
Output:
total 28 drwxr-xr-x 1 rg rg 512 Apr 17 18:23 . drwxr-xr-x 1 root root 512 Jan 17 2022 .. -rw-r--r-- 1 rg rg 2 Apr 15 22:40 abc.sh -rw------- 1 rg rg 2068 Apr 16 16:13 .bash_history -rw-r--r-- 1 rg rg 220 Jan 17 2022 .bash_logout -rw-r--r-- 1 rg rg 3771 Jan 17 2022 .bashrc drwxr-xr-x 1 rg rg 512 Apr 17 12:29 destination_dir -rw-r--r-- 1 rg rg 284 Apr 13 18:09 input.txt drwxr-xr-x 1 rg rg 512 Apr 17 14:04 new_dir drwxr-xr-x 1 rg rg 512 Apr 17 11:18 new_workarea -rw-r--r-- 1 rg rg 2 Apr 13 18:15 nums.txt -rw-r--r-- 1 rg rg 67 Apr 17 11:42 old_error.log -rw-r--r-- 1 rg rg 416 Apr 13 18:17 output.txt drwxr-xr-x 1 rg rg 512 Apr 17 16:04 parent_directory -rw-r--r-- 1 rg rg 655 Jan 17 2022 .profile drwxr-xr-x 1 rg rg 512 Jun 10 2022 .vim -rw------- 1 rg rg 6695 Apr 17 18:23 .viminfo
Explanation:
In the exercise above,
The script utilizes the "ls" command with the -al options to list all files and directories, including hidden ones, with detailed information.
2.
Write a Bash script that lists only the files in the current directory.
Code:
#!/bin/bash
# Bash script to list only files in the current directory
for file in *; do
if [ -f "$file" ]; then
echo "$file"
fi
done
Output:
abc.sh document.txt erorrrr.log file2.txt input.txt nums.txt old_error.log output.txt sample.txt test1.sh test.sh
Explanation:
In the exercise above,
- for file in ; do: This line starts a loop that iterates over each item in the current directory (). The * wildcard matches all files and directories in the current directory.
- if [ -f "$file" ]; then: This line checks if the current item ('$file') is a regular file using the -f test operator. If it is a regular file, the code inside the "if" block will be executed.
- echo "$file": This line prints the name of the file ('$file') to the standard output.
- done: This line marks the end of the loop.
3.
Write a Bash script that lists only the directories in the current directory.
Code:
# Bash script to list only directories in the current directory
for dir in */; do
if [ -d "$dir" ]; then
echo "$dir"
fi
done
Output:
destination_dir/ new_dir/ new_workarea/ parent_directory/ source_directory/ test/
Explanation:
In the exercise above,
- for dir in /; do: This line starts a loop that iterates over each item in the current directory (/). The */ pattern matches all directories in the current directory.
- if [ -d "$dir" ]; then: This line checks if the current item ('$dir') is a directory using the -d test operator. If it is a directory, the code inside the "if" block will be executed.
- echo "$dir": This line prints the name of the directory ('$dir') to the standard output.
- done: This line marks the end of the loop.
4.
Write a Bash script that lists all files and directories in a specific directory provided as an argument to the script.
Code:
#!/bin/bash
# Check if an argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 directory_path"
exit 1
fi
# Check if the provided argument is a directory
if [ ! -d "$1" ]; then
echo "$1 is not a directory."
exit 1
fi
# List all files and directories in the provided directory
echo "Files and directories in $1:"
ls -al "$1"
Output:
ad@DESKTOP-3KE0KU4:~$ ./test1.sh test.txt test.txt is not a directory.
ad@DESKTOP-3KE0KU4:~$ ./test1.sh parent_directory Files and directories in parent_directory: total 0 drwxr-xr-x 1 rg rg 512 Apr 17 16:04 . drwxr-xr-x 1 rg rg 512 Apr 17 20:15 .. drwxr-xr-x 1 rg rg 512 Apr 17 16:04 workarea drwxr-xr-x 1 rg rg 512 Apr 17 16:04 workarea2
Explanation:
In the exercise above,
- if [ $# -ne 1 ]; then: This line checks if the number of arguments provided to the script is not equal to 1.
- echo "Usage: $0 directory_path": If the number of arguments is incorrect, this line prints the correct usage of the script.
- exit 1: This line exits the script with a non-zero status, indicating an error.
- if [ ! -d "$1" ]; then: This line checks if the provided argument (directory path) is not a directory.
- echo "$1 is not a directory.": If the provided argument is not a directory, this line prints an error message.
- echo "Files and directories in $1:": This line prints a message indicating the directory whose contents are going to be listed.
- ls -al "$1": This line lists all files and directories in the provided directory ('$1') along with detailed information (-al options for ls command).
5.
Write a Bash script that lists all hidden files (files starting with a dot) in the current directory.
Code:
#!/bin/bash
# List all hidden files in the current directory
for file in .*; do
if [ -f "$file" ]; then
echo "$file"
fi
done
Output:
.bash_history .bash_logout .bashrc .profile .viminfo
Explanation:
In the exercise above,
- for file in .*; do: This line starts a loop that iterates over each entry in the current directory starting with a dot.
- if [ -f "$file" ]; then: This line checks if the current entry is a regular file.
- echo "$file": If the current entry is a regular file, this line prints its name.
- done: This line marks the end of the loop.
6.
Write a Bash script that lists all files modified within the last 24 hours in the current directory.
Code:
#!/bin/bash
# List all files modified within the last 24 hours in the current directory
find . -type f -mtime -1 -print
Output:
./.viminfo ./destination_dir/abc.txt ./destination_dir/pqr.txt ./erorrrr.log ./old_error.log ./source_directory/abc.txt ./source_directory/pqr.txt ./test.sh ./test1.sh
Explanation:
In the exercise above,
find . -type f -mtime -1 -print: This command searches for files (-type f) in the current directory (.) that have been modified within the last 24 hours (-mtime -1) and prints their paths (-print).
7.
Write a Bash script that lists all files larger than 1MB in the current directory.
Code:
#!/bin/bash
# List all files larger than 1MB in the current directory
find . -type f -size +1M -exec ls -lh {} \;
Explanation:
In the exercise above,
find . -type f -size +1M -exec ls -lh {} \;: This command searches for files (-type f) in the current directory (.) that are larger than 1MB (-size +1M) and executes the ls -lh command on each of them to list their details in a human-readable format.
8.
Write a Bash script that lists all empty files in the current directory.
Code:
#!/bin/bash
# List all empty files in the current directory
find . -maxdepth 1 -type f -empty -exec ls -lh {} \;
Output:
-rw-r--r-- 1 rg rg 0 Apr 17 22:33 ./input.txt -rw-r--r-- 1 rg rg 0 Apr 18 07:44 ./input1.txt
Explanation:
In the exercise above,
find . -maxdepth 1 -type f -empty -exec ls -lh {} \;: This command searches for files ('-type f') in the current directory (.) that are empty ('-empty') and executes the "ls -lh" command on each of them to list their details in a human-readable format. The "-maxdepth 1" option ensures that only the files in the current directory are considered, not in its subdirectories.
9.
Write a Bash script that lists all files with a specific extension (e.g., ".txt") in the current directory.
Code:
#!/bin/bash
# List all files with a specific extension in the current directory
extension=".txt"
for file in *"$extension"; do
if [ -f "$file" ]; then
echo "$file"
fi
done
Output:
document.txt file2.txt input1.txt input.txt nums.txt output.txt sample.txt
Explanation:
In the exercise above,
- extension=".txt": This variable stores the specific extension you want to search for. You can change it to any desired extension.
- for file in *"$extension"; do: This loop iterates over all files in the current directory with the specified extension.
- if [ -f "$file" ]; then: This condition checks if the current item in the loop is a regular file.
- echo "$file": If the file is a regular file, its name is echoed, indicating that it matches the specified extension.
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.
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://www.w3resource.com/bash-script-exercises/listing-files-and-directories.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics