I am working with a large text file on my Ubuntu server that contains a list of usernames in random order. I need to sort this list alphabetically from A to Z and save the output to a new file. What is the basic command to achieve this in the terminal? Additionally, are there specific flags I should use if the file contains duplicate entries I want to remove, or if I need to sort in reverse order (Z to A)? I want to make sure I understand the syntax before running it on my production data.
2 answers
The primary tool for this task is the sort command. To simply display a file's contents in alphabetical order, you use sort filename.txt. If you want to save that sorted list to a new file, you can use the redirection operator: sort filename.txt > sorted_list.txt. For common variations, use the -r flag for a reverse alphabetical sort and the -u (unique) flag to remove any duplicate lines automatically. This utility is extremely powerful because it can handle files much larger than your system's memory by using temporary disk space, making it a staple for anyone working in Data Science or System Administration.
The alphabetical sort works great, but what if my file has numbers at the beginning of the lines? When I use the standard sort, it treats "10" as coming before "2" because it's looking at the first character alphabetically. How do I tell Linux to treat those leading characters as actual numbers for a proper numerical sequence?
Elena, you need to use the -n (numerical) flag. Running sort -n filename.txt tells Linux to evaluate the numeric value of the strings. This is a classic "gotcha" in Software Development. If you have a mix of text and numbers, you can even specify which "column" or field to sort by using the -k flag, which is incredibly useful for processing CSV-style logs.