Issue
cd subdir1/subdir2
ineedthis=$(find subdir3/ -name "*.csv" | tr ' ' '_') ## assigning name of the file into this variable
echo -n $ineedthis | wc -c
I wanted to see the length of all the filename by assigning a variable called ineedthis and making changes such that I don't have any spaces between the names. Then, I tried to use echo -n to read the name only and count the characters to find the length of the name. However, when I try to use wc -c on the echo statement, it would give me the number of characters of the chunk, instead of giving me the length of each filename.
What I was hoping was:
# numbers indicating the length of filename
9 subdir3/saying/hello.csv
6 subdir3/saying/hi.csv
9 subdir3/nay/noway.csv
12 subdir3/nay/nomethod.csv
16 subdir3/nay/you_dont_say.csv
Solution
find subdir3/ -name "*.csv" |\
while read path; do
$file=$(basename "$path")
$len=$(echo -n "$file" | wc -c)
echo $len "$path"
done
while
loops over each path found byfind
basename
strips off everything up to final/
(optionally a suffix can also be removed. bash provides bultins like${path##*/}
and${path%%.csv}
that are similar)
Answered By - jhnc