Score:7

How to sort files with a certain permission by size?

cn flag

I need to sort all my files with 644 permissions by size.

So far I've tried using the ls command to find and sort but they don't seem right. I think it may need a grep command but I think it needs to show numbers like 644 not rwx. I don't know how to do that in a simple way since its my first time using Linux and the terminal in general.

in flag
This sounds like homework. Could you elaborate on what you mean with "they don't seem right"? Are you missing files? Is it an incomplete solution? Sharing what you've already done might allow someone to offer a suggestion on how to reach your goal
waltinator avatar
it flag
Read `man find stat`.
Score:11
cn flag

In your standard bash shell, you can do that with the find command:

find . -perm 0644 -printf '%s %p\n'| sort -nr

This finds all files and directories with permissions 0644 and prints the results formatted using the -printf action. %s represents the file size and %p the file path. \n represents a new line. The output is piped into sort, set to sort numerically (-n) and in reverse order (-r).

Eva  avatar
cn flag
thank you so much for the help
Score:7
hr flag

Using the Z shell (zsh) with its glob qualifiers:

print -rC1 *(.DNf:u=6,go=4:oL)

where the meanings are

  • . matches plain files only
  • D sets the GLOB_DOTS option for the current pattern
  • N sets the NULL_GLOB option for the current pattern
  • fspec matches files with access rights matching spec
  • oL orders the results by length (size in bytes); use OL to reverse the order
stumblebee avatar
mx flag
You gave them the answer to the bonus question! LOL
hr flag
@stumblebee irl I would probably have used `find` with `-perm` and `-printf` to output size and name, then sort and cut the result - I was just interested in exploring how to do it in zsh!
user535733 avatar
cn flag
This is a brilliant answer on several levels. Most entertaining answer I've seen today!
Eva  avatar
cn flag
thank you so much for the answer
Eva  avatar
cn flag
the deadline was like two days ago so no worries no bonus points for this, just wanted to learn how to do that :D im not taking credit for other peoples work
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.