Linux Commands Examples

A great documentation place for Linux commands


sort lines of text files

see also : uniq


sort [OPTION]... [FILE]...
[OPTION]... --files0-from=F

add an example, a script, a trick and tips

: email address (won't be displayed)
: name

Step 2

Thanks for this example ! - It will be moderated and published shortly.

Feel free to post other examples
Oops ! There is a tiny cockup. A damn 404 cockup. Please contact the loosy team who maintains and develops this wonderful site by clicking in the mighty feedback button on the side of the page. Say what happened. Thanks!



Unix/Linux find and sort by date modified

Try this very code find '$dir' -name '$str'\* -print | xargs ls -tl | head -10 but it's useful to filter data by -mmin/-mtime and -type


In bash, how to sort strings with numbers in them?

Something like this might do what you want, though it takes a slightly different approach:

pdftk $(for n in {1..18}; do echo cwcch$n.pdf; done) cat output output.pdf


Sorting human readable file sizes

Afaik, there's no standard command to do this.

There are various workarounds, which were discussed when the same question was asked over at Stack Overflow: How can I sort du -h output by size


List of files under a directory in name order

If by "name order" you mean a simple alphabetical sorting, you could just sort the find results, which works fine as long as the files do not contain a newline in their name:

find "$PHOTODIR" -iname "*.jpg" | sort

If you want to pass these as arguments to an image viewer—or any other command—you need to pipe them to xargs. This works out of the box as long as there's no whitespace in the file names or paths.

If you want this to work with any file name or path, use the -print0 argument to make find's output delimited by NUL-characters, sort with -z, and use xargs -0:

find "$PHOTODIR" -iname "*.jpg" -print0 | sort -z | xargs -0 <command>


GNU sort by case-sensitive

Override the collation order.

echo -e "c\nb\nB\na" | LC_COLLATE=C sort


Merge sorted lists

Where is the line between "shell commands" and "code"? This example is perhaps beyond that line, but it is just a quick aggregation function in awk that I already had written for another (more elaborate) purpose, so I might as well post it.

Assuming the files are located in the directory dir/, awk could sum them as such:

awk '
    END { for (fruit in fruits) {printf "%6s %s\n", fruits[fruit], fruit} }
' dir/* | sort -rn


  • Check every line in all files and add the value of the first field to the existing value of the index of that fruit in the array fruits (or create that index if it does not exist).
  • When the files have been traversed, loop over the indices in fruits and print their value and their name in your specified format.
  • Array sorting in awk is possible, but it's a lot easier on the eye to just use sort. -rn sorts numerically in descending order.

Linux command line / Move files filtered by lexicographic order

With brace expansion, which is available in Bash 3 and above, as well as Zsh and several other shells:*

mv IMG_{9431..9999}.jpg some_other_dir

The braces will be expanded to all the numbers between 9431 and 9999, so it is equivalent to writing out this:

mv IMG_9431.jpg IMG_9432.jpg … IMG_9999.jpg some_other_dir

This will fail if there are too many files (see this post about ARG_MAX for background info.)

If your shell lacks brace expansion features, or you have too many files, you can do this – which could be a little slower:

for n in $(seq 9431 9999); do mv "IMG_$n.jpg" some_other_dir; done

If you really want to sort lexicographically, have a look at ??????? ???????????'s answer. Much nicer than what follows here.

You'll have to sort your file names somehow. This is what I came up real quick, and it's not pretty and far from ideal. It works with GNU/Linux (grep, sort, xargs) and deals with any file names, including quotes and whitespace.

tmp="$(mktemp /tmp/files.XXX)"
find . -type f -name 'IMG*'  -maxdepth 1 -print0 | sort -z > "$tmp"
line=$(grep -nz IMG_9984.jpg "$tmp" | cut -d: -f1)
tr '\0\n' '\n\0' < "$tmp" | tail -n "+$line" | tr '\0\n' '\n\0' |
xargs -0 -I{} echo mv {} some_other_dir
rm "$tmp"

Remove the echo when you're sure this does what you want. What we do here:

  • Create a temporary file to hold the file names.

  • Find all files matching the pattern, and sort them into a temporary file. The records are delimited by NUL characters (-print0, -z) so we can deal with any file name.

  • Find the "line" number of the file name, e.g. IMG_9984.jpg

  • Swap NUL and newline in the temporary file so tail can deal with it.

  • Swap them back so xargs can deal with it (-0) and mv the files to the other directory.

This would be easier if we didn't have to deal with files that contain quotes or whitespace, but… that's just me. Better be safe than sorry.


Equivalent of gnu `sort -R` on OSX?

If you want, you can install GNU sort through GNU's coreutils package over Homebrew, which is a package manager for OS X.

Running this would install Homebrew.

ruby <(curl -fsSkL

Then just follow the installation instructions. When Homebrew is installed, run

brew install coreutils

This will install GNU sort as gsort, so you can use it like sort on any GNU Linux.

Alternatively, have a look at these Stack Overflow questions, which mention a couple of methods:

How can I randomize the lines in a file using a standard tools on Redhat Linux
How can I shuffle the lines of a text file in Unix command line?

Or take a look at this page:

Randomize lines (opposite of | sort)


How to reorder folders? (as displayed in `ls -U`)

You should take a look at this link:

According to what I am reading there is a utility called FATSort which can re-arrange the files for you.

It is actually also available from the Ubuntu repositories so:

sudo apt-get install fatsort


Sorting files based on a timestamp within the file

$ grep ^date_time *.dat | sort -t: -k3

or to just output filenames

$ grep ^date_time *.dat | sort -t: -k3 | cut -d: -f1

Where the data files are

$ head *dat
==> t8a.dat <==

==> t8b.dat <==

You can use grep's -m 1 option to stop it searching through the subsequent (binary) data.


Write sorted concatenation of all FILE(s) to standard output.

Mandatory arguments to long options are mandatory for short options too. Ordering options:
, --ignore-leading-blanks

ignore leading blanks

-d, --dictionary-order

consider only blanks and alphanumeric characters

-f, --ignore-case

fold lower case to upper case characters

-g, --general-numeric-sort

compare according to general numerical value

-i, --ignore-nonprinting

consider only printable characters

-M, --month-sort

compare (unknown) < ’JAN’ < ... < ’DEC’

-h, --human-numeric-sort

compare human readable numbers (e.g., 2K 1G)

-n, --numeric-sort

compare according to string numerical value

-R, --random-sort

sort by random hash of keys


get random bytes from FILE

-r, --reverse

reverse the result of comparisons


sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

-V, --version-sort

natural sort of (version) numbers within text

Other options:

merge at most NMERGE inputs at once; for more use temp files

-c, --check, --check=diagnose-first

check for sorted input; do not sort

-C, --check=quiet, --check=silent

like -c, but do not report first bad line


compress temporaries with PROG; decompress them with PROG -d


annotate the part of the line used to sort, and warn about questionable usage to stderr


read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input

-k, --key=KEYDEF

sort via a key; KEYDEF gives location and type

-m, --merge

merge already sorted files; do not sort

-o, --output=FILE

write result to FILE instead of standard output

-s, --stable

stabilize sort by disabling last-resort comparison

-S, --buffer-size=SIZE

use SIZE for main memory buffer

-t, --field-separator=SEP

use SEP instead of non-blank to blank transition

-T, --temporary-directory=DIR

use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories


change the number of sorts run concurrently to N

-u, --unique

with -c, check for strict ordering; without -c, output only the first of an equal run

-z, --zero-terminated

end lines with 0 byte, not newline


display this help and exit


output version information and exit

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line’s end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given, use the entire line as the key.

SIZE may be followed by the following multiplicative suffixes: % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.


Copyright © 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

reporting bugs

Report sort bugs to bug-coreutils[:at:]gnu[:dot:]org
GNU coreutils home page: <>
General help using GNU software: <>
Report sort translation bugs to <>

see also


The full documentation for sort is maintained as a Texinfo manual. If the info and sort programs are properly installed at your site, the command

info coreutils 'sort invocation'

should give you access to the complete manual.


Written by Mike Haertel and Paul Eggert.

How can this site be more helpful to YOU ?

give  feedback