0
0
Bash Scriptingscripting~10 mins

File existence checks in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the file 'data.txt' exists.

Bash Scripting
if [ [1] "data.txt" ]; then
  echo "File exists"
fi
Drag options to blanks, or click blank then click option'
A-f
B-d
C-e
D-s
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks only for directories.
Using -f which checks only for regular files.
Using -s which checks if file exists and is not empty.
2fill in blank
medium

Complete the code to check if 'log.txt' is a regular file.

Bash Scripting
if [ [1] "log.txt" ]; then
  echo "It's a regular file"
fi
Drag options to blanks, or click blank then click option'
A-f
B-e
C-d
D-r
Attempts:
3 left
💡 Hint
Common Mistakes
Using -d which checks for directories.
Using -e which checks for any file or directory.
Using -r which checks if file is readable.
3fill in blank
hard

Fix the error in the code to check if 'folder' is a directory.

Bash Scripting
if [ [1] "folder" ]; then
  echo "It's a directory"
fi
Drag options to blanks, or click blank then click option'
A-f
B-d
C-e
D-s
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f which checks for regular files.
Using -e which checks for any file or directory.
Using -s which checks if file is not empty.
4fill in blank
hard

Fill both blanks to check if 'script.sh' exists and is executable.

Bash Scripting
if [ [1] "script.sh" ] && [ [2] "script.sh" ]; then
  echo "Script is ready to run"
fi
Drag options to blanks, or click blank then click option'
A-e
B-x
C-f
D-r
Attempts:
3 left
💡 Hint
Common Mistakes
Using -e instead of -f to check for regular file.
Using -r instead of -x to check for executable permission.
5fill in blank
hard

Fill all three blanks to create a dictionary of files and their sizes if size is greater than 0.

Bash Scripting
declare -A file_sizes
for file in *; do
  if [ [1] "$file" ] && [ $( [2] "$file" ) -gt 0 ]; then
    file_sizes[[3]]=$(stat -c%s "$file")
  fi
done
Drag options to blanks, or click blank then click option'
A-f
Bstat -c%s
C$file
D-s
Attempts:
3 left
💡 Hint
Common Mistakes
Using -f instead of -s to check file size.
Using filename incorrectly as dictionary key.
Not using stat command to get file size.