Here is a more general approach. Get the output of du folder
and du -h folder
in two different files.
du folder > file1
du -h folder > file2
The key part is this: concatenate file1
and file2
line by line, with a suitable delimiter.
paste -d '#' file1 file2 > file3
(assuming #
does not appear in file1
and file2
)
Now sort file3
. Note that this will sort based on file1
contents and break ties by file2
contents. Extract the relevant result using cut
:
sort -n -k1,7 file3 | cut -d '#' -f 2
Also take a look at man sort
for other options.
You may also save this as an alias, for later re-use. To do so, add the following to the bottom of ~/.bashrc
:
sorted-du () {
paste -d '#' <( du "$1" ) <( du -h "$1" ) | sort -n -k1,7 | cut -d '#' -f 2
}
Then, open a new terminal session and execute your new alias:
sorted-du /home
Use the -o
option of sort
instead of using redirection operator, this will edit the file in place.
From man sort
:
-o, --output=FILE
write result to FILE instead of standard output
Here is a test:
$ cat test.txt
A
C
B
$ sort test.txt
A
B
C
$ sort test.txt -o test.txt
$ cat test.txt
A
B
C
Best Answer
If you have a file like above. You can use the command below to sort by GPA. (change the key to sort by any field : '-k value')
If you want the reverse order use -r option