-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
  • 58,122
Avinash Raj
  • 78,556

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
  • 169,590
1

Open your terminal and type as

du -sk * | sort -rn 
Raja G
  • 102,391
  • 106
  • 255
  • 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,715
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
  • 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