-3

I want to sort subdirectories inside a main directory according to the size,and also I want to sort files inside the subdirectories according to the file created date through terminal. Is that possible?

Edit: After I sorted the files inside the folder by following the below answers, it doesn't rearranges while I am viewing through nautilus. I want the files to be rearranged while viewing it through nautilus.

Seth
  • 57,282
  • 43
  • 144
  • 200
Avinash Raj
  • 77,204
  • 56
  • 214
  • 254
  • 2
    Sorry, but either, you make that nautilus arrange them or you do using the terminal, there isn't a middle point between them, – Braiam Dec 23 '13 at 18:39
  • Related: [Setting the view mode recursively for a directory](http://askubuntu.com/questions/335874/how-do-i-recursively-set-the-nautilus-view-mode-for-a-directory-tree) – Glutanimate Dec 24 '13 at 12:48
  • what i needed was posted by @souravc. – Avinash Raj Jan 07 '14 at 06:07
  • You cannot sort files in a terminal and expect them to remain sorted when viewing them in nautilus. – Tass Feb 03 '15 at 22:29

4 Answers4

1

To sort the files according to the size you can use the following command:

ls -Sl

To sort the results by date created is a little bit more complicated. See the following post in this sense:

Radu Rădeanu
  • 166,822
  • 48
  • 327
  • 400
1

Open your terminal and type as

du -sk * | sort -rn 
Raja G
  • 100,643
  • 105
  • 254
  • 328
1

If you want to sort sub-directories/files inside a directory according to the size, type in terminal,

gsettings set org.gnome.nautilus.preferences default-sort-order "size"

To sort sub-directories/files according to file modification time,

gsettings set org.gnome.nautilus.preferences default-sort-order "mtime"

You can also sort according to name and type. But you can not set two values simultaneously, like one for a directory and other for sub-directories. There is no problem to change the values any time. The change will take place immediately for all directories.

Also you can sort in reverse order for size, name, modification time or type. To do this type in terminal,

gsettings set org.gnome.nautilus.preferences default-sort-in-reverse-order true
sourav c.
  • 44,037
  • 20
  • 101
  • 128
0

I don't think it is possible without a loop – at least not in bash – but if you're willing to use loops, here's an example that will get you on the right track.

#!/bin/bash
for dir in $(du -sh */ | sort -rh | cut -f2); do
    echo "*** $(du -sh $dir) ***"
    ls -l --sort=time $dir
done

This script …

  • loops over all entries of du -sh, sorted by size – cut just formats the output
  • and, for each entry, prints the total size of that directory (du -sh) and lists its content sorted by size (ls -l --sort=time).
drc
  • 2,880
  • 13
  • 16
  • `ls -l --sort=time` will sort files according to the **modified** date, not **created** date. – Radu Rădeanu Dec 03 '13 at 16:01
  • You're right, but unfortunately most unix FS don't save creation time, so `--sort=time` is as good as it gets. For more detailed information, check http://unix.stackexchange.com/questions/20460/how-do-i-do-a-ls-and-then-sort-the-results-by-date-created – drc Dec 03 '13 at 21:33