Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to loop over all files in the current directory.
Bash Scripting
for file in [1]; do echo "$file"; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' which refers to the current directory itself, not the files inside.
Using '/' which refers to the root directory.
Using '~' which refers to the home directory.
✗ Incorrect
The asterisk (*) matches all files and directories in the current directory.
2fill in blank
mediumComplete the code to loop over all '.txt' files in the 'docs' directory.
Bash Scripting
for file in docs/[1]; do echo "$file"; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*.doc' which matches files with '.doc' extension, not '.txt'.
Using 'txt.*' which is not a valid pattern for '.txt' files.
Using 'docs.txt' which is a filename, not a pattern.
✗ Incorrect
The pattern '*.txt' matches all files ending with '.txt' in the 'docs' directory.
3fill in blank
hardFix the error in the code to loop over directories only in the current folder.
Bash Scripting
for dir in [1]; do echo "$dir"; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which matches files and directories.
Using '.*' which matches hidden files and directories.
Using '*/**' which is not a valid pattern in basic bash globbing.
✗ Incorrect
The pattern '*/' matches only directories in the current directory because directories end with a slash.
4fill in blank
hardFill both blanks to loop over all '.log' files larger than 1MB in the 'logs' directory.
Bash Scripting
for file in logs/[1]; do if [[ $(stat -c%s "$file") [2] 1048576 ]]; then echo "$file"; fi; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*.txt' which matches wrong file types.
Using '<' which checks for smaller files, not larger.
Forgetting to quote "$file" in stat command.
✗ Incorrect
Use '*.log' to match log files and '>' to check if file size is greater than 1MB (1048576 bytes).
5fill in blank
hardFill all three blanks to create a dictionary-like output of filenames and their sizes for '.csv' files in 'data' directory larger than 500 bytes.
Bash Scripting
declare -A sizes; for file in data/[1]; do size=$(stat -c%s "$file"); if [[ $size [2] 500 ]]; then sizes[[3]]=$size; fi; done; for f in "${!sizes[@]}"; do echo "$f: ${sizes[$f]} bytes"; done
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*.txt' which selects wrong files.
Using '<' which checks for smaller files.
Not quoting the filename as array key causing errors with spaces.
✗ Incorrect
Use '*.csv' to select CSV files, '>' to check size greater than 500 bytes, and '"$file"' as the key in the associative array.