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 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.
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?
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).