1

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!

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for the reply. And awk is actually selecting required 2 columns and adding a comma in between them, since ls lists many more values and I have no need for all that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.