//Tips tagged sort
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
This piece of code lists the size of every file and subdirectory of the current directory, much like du -sch ./* except the output is sorted by size, with larger files and directories at the end of the list. Useful to find where all that space goes.
du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'
To find out the number of files of each type in your current directory try the following:
(You may want to add this as an alias rather than type it in each time!)
find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr
5 PHP script text
2 data
2 Zip archive data
2 GIF image data
1 PNG image data
(You may want to add this as an alias rather than type it in each time!)
Sort a file by line length:
cat test.txt | awk '{ printf "%06d|%s\n",length($0),$0}' | sort | cut -b'8-'
Use the following to see the commands you use most often based on your shell history:
history | awk '{print $2}' | sort | uniq -c | sort -rn | headsort a file by the first column in dictionary order, then by the second column numerically.
sort +0d -1 +1n -2 file.txt
I use the following to list non-system users. It should be portable though won't work on systems without the getent command.
alias lsusers='getent passwd | tr ":" " " | awk "\$3 >= $(grep UID_MIN /etc/login.defs | cut -d " " -f 2) { print \$1 }" | sort'The command below can be used to sort the lines of a file by ascending order (shortest first). Change 'sort -n' to 'sort -nr' to sort in descending order (longest first).
cat /etc/passwd | awk '{print length, $0}' | sort -n | awk '{$1=""; print $0 }'The commmands below show the ten largest files/dirs in the working directory. Both commands give similar results, though handle things slightly differently. The 'du' option is good if you also need sizes of subdirectories, but the 'ls' option gives more detail.
I find both to be useful in situations where I need to make more free space.
ls -laSh | head -10 du -s * | sort -nr | head -10
I find both to be useful in situations where I need to make more free space.
Display the top ten running processes - sorted by memory usage
ps aux | sort -nk +4 | tail
ps returns all running processes which are then sorted by the 4th field in numerical order and the top 10 are sent to STDOUT.
ps aux | sort -nk +4 | tail
ps returns all running processes which are then sorted by the 4th field in numerical order and the top 10 are sent to STDOUT.
The command below will give a list of the packages installed on a debian system sorted from smallest to largest (the order can be reveresed by adding an 'r' option to sort - 'sort -k2 -nr').
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k2 -n
The bash shell has a variable called $RANDOM, which outputs a pseudo-random number every time you call it. This allows you to randomize the lines in a file for example:
In other words, put a random number on every line, sort the file, then take off the random numbers.
for i in `cat unusual.txt`; do echo "$RANDOM $i"; done | sort | sed -r 's/^[0-9]+ //' > randorder.txt
In other words, put a random number on every line, sort the file, then take off the random numbers.
You might want to get rid of the awk, sort, uniq and grep depending on how much info you need.
for dir in $(find /proc -maxdepth 1 -type d -name "*[0-9]");do ls -l $dir/fd/ | awk '{print $11}';done | sort | uniq | grep "^/var/"

