0
0
Bash Scriptingscripting~10 mins

Looping over files and directories 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 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'
A~
B.
C/
D*
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.
2fill in blank
medium

Complete 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'
A*.txt
B*.doc
Ctxt.*
Ddocs.txt
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.
3fill in blank
hard

Fix 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'
A*/**
B*/
C.*
D*
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.
4fill in blank
hard

Fill 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'
A*.log
B>
C<
D*.txt
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.
5fill in blank
hard

Fill 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'
A*.csv
B>
C"$file"
D*.txt
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.