To list files based on size in unix
GOAL : To fetch list of files occupying more space in unix
using the below command
ssh serverName du /folderName/* | grep -v 'cannot' | sort -nr | head -10
Using sort -nr to consider as numeric and sort in reverse (To开发者_如何学编程 get files occupying more space)
Using the grep -v 'cannot' because there is no access to few folders and these lines must be ignored before sorting
Below is the sample output
624 /folder1/folder2/conf
16 /folder1/folder2/error/include
192 /folder1/folder2/error
284 /folder1/folder2/htdocs
264 /folder1/folder2/icons/small
du: cannot read directory `/folder1/folder2/file1': Permission denied
du: cannot read directory `/folder1/folder2/file3': Permission denied
Facing issues with grep and sort commands, as the error messages are not getting filtered
You need to redirect stderr to stdout using 2>&1
so that you can grep out the error messages. You should also escape the wildcard so that it gets expanded on the remote machine, not on the local one.
ssh serverName du /folderName/\* 2>&1 | grep -v 'cannot' | sort -nr | head -10
You don't need the grep
if you close stderr.
ssh serverName du /folderName/\* 2>&- | sort -nr | head -10
Note that the wildcard is escaped.
精彩评论