Issue
I am looking to write a for loop which prints the names of all files and directories inside my current working directory along with the number of characters each file and directory name possess. I currently have the below, it doesn't work as I intend, any suggestions to the below?
#!/bin/bash
FILES=/c/Users/johndoe/unix/*
for i in $FILES
do
echo "$FILES has" | wc -c
done;
Solution
You could use an array
Files=(/c/Users/johndoe/unix/*)
for i in "${Files[@]}"
do
wc -c "$i"
done
Also you should quote your variables, and it's advised not to use UPPERCASE variable names as they can conflict with environment variables.
If you aren't reusing the filename list you could also just simply do
wc -c /c/Users/johndoe/unix/*
And if you wanted to recursively check dirs you could use find
find /c/Users/johndoe/unix/ -type f -exec wc -c {} \;
Answered By - 123