File Statistics in Shell
Level 3120 keystrokes
Loops over the .txt files in the current directory, prints each name and counts them. The output depends on the files present (here data.txt and notes.txt).
Shell code
# Start the counter at 01count=0# The glob *.txt expands to the .txt files in the current directory2for f in *.txt; do # If nothing matches, f is the literal "*.txt"; -f skips that case3 if [ -f "$f" ]; then # Add 1 with arithmetic expansion4 count=$((count + 1)) # Print the file name5 echo "Found: $f"6 fi7done# Print how many .txt files were found8echo "Total: $count files"Output
Found: data.txt Found: notes.txt Total: 2 files
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Shell code and how often they appear.
"× 6$× 4;× 2:× 2(× 2)× 2=× 2-× 1.× 1[× 1