Search code examples
shellscriptingfindfilenamesspaces

Print full filenames with spaces in shell with find command


I have a find command that will print size and filename(along with full path) of the 20 largest files on the given directory and write it into a file:

find ${DIR}  -type f -size +1M -exec du -h '{}' + | sort -hr|awk '{print $1","$2}'|head -20 >>files.csv

The problem is, there exist some files with spaces in their file names. These file names are only printed till the first space.

Ex: A file named 'hello there.txt' is printed as 'hello'

I tried setting the IFS to '\n',

i.e. IFS=$'\n',

but the problem still persists.

Any help will be much appreciated.

Thanks!


Solution

  • In awk, print $1","$2 prints the first two fields separated by a comma. The problem is that there.txt in hello there.txt is in the third column.

    It seems that the only thing that awk is doing here is replacing the first tab with a comma. That can be easily done with sed:

    $ find "${DIR}" -type f -size +1M -exec du -h '{}' + | sort -hr| sed 's/\t/,/' |head -20
    4.0M,./path/hello there.txt