Ls -al on filename with spaces

I am trying to extract the size and filename from a group of files. Normally I would just do an ls -alR combined with an awk ‘{print %5" "$9}’. However this doesn’t work with filename with spaces.

I found some things from a google search but they don’t work either. Here is other things I have tried.

ind /home/glrider/A53Music/Comedy -maxdepth 2 -type f -name “*.mp3” | while read name
do
echo $name
name=‘"’$name’"’
echo $name
ls -al $name
done

or

clear
cd ‘/run/user/1000/gvfs/mtp:host=SAMSUNG_SAMSUNG_Android_R5CT62NC5EN/Internal storage/Music/Comedy’
find . -name “Jerky Boys - Denta*.mp3” > /tmp/a53.lst1

cat /tmp/a53.lst1 |
while read line
do
newline=“'”$line"'"
echo “newline=”$newline
filesize=ls -al $newline | awk '{print $5}'
echo $line
done

exit

All situations end with ls dealing with the filename as separate files, even with single quotes or double quotes wrapped around it.

I should have added I am trying to do this in a shl script. It seems to work on the command line.

lrider@glrider-linux:/run/user/1000/gvfs/mtp:host=SAMSUNG_SAMSUNG_Android_R5CT62NC5EN/Internal storage/Music/Comedy$ ls -al “./Jerky Boys - Dental Malpractice.mp3”
-rw------- 1 glrider glrider 3069952 Jan 1 13:16 ‘./Jerky Boys - Dental Malpractice.mp3’
glrider@glrider-linux:/run/user/1000/gvfs/mtp:host=SAMSUNG_SAMSUNG_Android_R5CT62NC5EN/Internal storage/Music/Comedy$

I think you want to use the find -print0 option to separate the file names with nulls instead of spaces. You can then use xargs -0 to grab out the individual file names, which will need to be in quote if passed through a variable.

# touch "has spaces in it"
# find -maxdepth 1 -iname 'h*' -print0 | xargs -0 printf ">>%s<<\n"
>>./has spaces in it<<
>>./heap.py<<
>>./hosts.txt<<

hi,

Sorry for the delay. Things have been hectic. Thanks so much for the suggestion. This seems to give me exactly what I want for the filename, but if I wanted to also extract the size of the file?

find -maxdepth 2 -iname ‘*.mp3’ -print0 | xargs -0 printf “>>%s<<\n”

Easy! Just replace that printf with a stat call, like this:

find ... -print0 | xargs -0 -n1 stat --format='%s %n'

With %s being the file’s size, and %n its name. There are a bunch more options in stat’s format string, do stat --help and it will list them out.

Also note the -n1 on xargs, which says “only feed the command a single value at a time”, which can sometimes be necessary when dealing with various commands (printf will take a bunch of args and iterate internally over each one, while stat only takes a single file, hence the need to make xargs do the iteration).

Thanks, that gave me exactly what I needed. Cheers.