#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Finding size of files with spaces in their file names


    I am running a UNIX script to get unused files and their sizes from the server. The issue is arising due to the spaces present in the filename/folder names.Due to this the du –k command doesn’t work properly.But I need to calculate the size of all files including the ones which have spaces in them.



    I have a problem with this part:



    for file in `find . -type f -print | cut -c 3- | sed 's/ /#}/g'` //my file name here would be UK#}dump/a.jpg
    PHP Code:

    do

      
    file2=`echo $file | sed 's/#}/ /g'`                                          //my file name here would be UK dump/a.jpg . If I run a du –k command for this filename I will get an error

      
    file3=`echo $file | sed 's/#}/\\\ /g'`                                   // this line is my attempt to change the filename to UK\ dump/a.jpg but it does not happen

      
    size=`du -k $file3 |awk '{print $1}'`

      
    result=`grep -c "$file2$dbdump`

    …………………….

    done 
    I have tried the following ways:


    PHP Code:

    1
    sed 's/#}/\ /g'`                                   

    2) sed 's/#}/\\ /g'
    `                                   

    3sed 's/#}/\\\ /g'`      

    4) sed 's/#}/\\\\ /g'

    Some of it works on the command line but doesn't work when I run them as a script.

    I cannot use ls -ltr because I dont want the sizes of all files in a folder. I need to get each file in a set of folders, loop through each of them, do some comparison and then write them to a file along with their size.So Could you'll please help me with the same.

    Thanks and Regards,
    Neil
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    One of these perhaps?
    Code:
    $ find test/ -type f | xargs -d'\n' du -k
    8	test/file with spaces.txt
    12	test/foo.c
    12	test/foo.o
    $ 
    $ find test/ -type f -print0 | xargs --null du -k
    8	test/file with spaces.txt
    12	test/foo.c
    12	test/foo.o
    So long as your filenames do not have a newline in them, the first one suffices.

    If you have really weird filenames, then you can make find and xargs use \0 as the delimiter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo