Sorting files is one of the most common tasks we do on a daily basis. You can use your own hands to sort, or you can use an app like Unclutter for iOS and Android. If you want to know how Bash Sort works and why it’s so useful in sorting large files quickly, read this blog from our friends at TechCrunch
and then check out the article!
The “bash sort -k” is a command-line tool that allows users to sort files like a boss. The “bash sort” can be used with either the key “-” or “-k”.
Are you seeking for a method to organize your files and run some commands on them? In programming, there are numerous times when you need to sort data, such as a list of files. Using the Bash sort and ls tools to sort files can help you stay organized.
You will learn the foundations of sorting files and file contents in this course.
Let’s get started sorting!
This article utilizes Ubuntu 20.04, but it will work with any Linux system.
How to Get Started with Git Bash on Windows (Related)
Sorting Files Alphabetically with Bash
There are other methods to sort files in Linux, but let’s start with the most frequent method: alphabetical sorting.
To acquire a list of files in a directory in ascending order, open your terminal and type the ls -l command below. The -l parameter instructs the ls command to display the result as a long list.
Sorting Files Alphabetically with Bash
The ls command lists files in ascending order by default. Pass the -r option to the ls -l command, as this: ls -lr, to reverse the sorting order. The -r parameter may be used with the ls -l command in other instances in this article.
Ordering Files by Size with Bash Sort
You may choose to order files by file size rather than alphabetically. When you want to view the smallest or biggest files in a list, File Sorting by Size comes in helpful.
Passing the -S parameter to the ls command causes the list of files to be sorted by file size.
To list files ordered by file size in a long list format, use the command below (-lS).
The command ordered the files by size in decreasing order as seen below (biggest to smallest in size).
Add the -r parameter to reverse the sorting order (smallest to largest), as in: ls -lSr
File Sorting by Size
Sorting Files by Modification Time Using Bash
Moving forward from File Sorting by Size, you might encounter a use case where you need to sort files by the time they were modified. For example, you forgot the name of the file you created, and you only remember the time you last modified the file.
Run the command below, where the -t argument instructs the ls program to sort the files according to when they were last modified.
Sorting a Directory’s Files by Modification Time
Filtering Files by Extension
When you’re looking for a file with a particular file type, Filtering Files by Extension is the ideal solution.
To list files in the working directory and arrange them by extension, use the command below (-lx).
The command ordered files with the same extension in ascending order based on their names, as seen below.
Filtering Files by Extension
Organizing Text File Contents
You’ve now learnt many ways for sorting files in a directory. Switching your attention away from the ls command, try the sort command. The sort command arranges the contents of a file based on the flag you provide. But first, you’ll need a file containing the information you want to change.
Create a text file called /data/fruits.txt with the names of fruits by running the command below. The -e switch causes backslash to be interpreted as a new line for each word (n).
echo -e “apple nmango nwatermelon ncherry norange nbanana” > fruits.txt
To sort each word in fruits.txt, use the command below.
The contents of the file are arranged in ascending order as shown below.
Sorting File Content in Ascending Order Alphabetically
The sort command organizes the file contents in ascending order by default if no flags are specified. Add the -r argument to the sort command to reverse the sorting order, as in sort -r fruits.txt. The -r parameter may be used to sort in reverse order in other instances in this tutorial.
In a text file, sorting a list of numbers
Numbers in a File Sorting is identical to sorting words in a file, only you’ll need the -n switch instead. To explain how to numerically sort a file’s content, first create a file.
Create a file called /data/scores.txt that includes random integers, each on a separate line, using the command below.
echo -e “45 n69 n52 n21 n3 n5 n78” > scores.txt
To sort the numbers (-n) in the /data/scores.txt file, use the program below.
The numbers are given in lines below, beginning with the lowest and increasing in size.
Numbers in a File Sorting
In a text file, sorting a list of version numbers
Perhaps you wish to sort a list of version numbers in a text file. If this is the case, adding the —version-sort option will solve the problem.
Create a text file first to illustrate how the —version-sort option works.
To produce a text file called /data/versions.txt with random version numbers listed on each line, use the command below (n).
echo -e “126.96.36.199 n 188.8.131.52 n184.108.40.206” > versions.txt
Run the command below to sort the version numbers in the versions.txt file (—version-sort). The —field-separator option instructs the sort command to use a dot to separate the numbers in each version (.). You may use any character to divide the numbers on the versions specified in your text file as the field separator.
versions.txt sort —version-sort —field-separator=
Version Numbers from a Text File Sorted
To find and sort files by file extension, use Bash Sort.
In the previous examples, you ran single commands (either ls or sort). However, in programming, you may often need to combine two or more instructions. How? By connecting one instruction to the next.
Use the following command to locate all markdown files (-iname “*.md”) in the current directory (.) and arrange them alphabetically descending (sort -r). Change “*.md” to another file extension to search and sort additional files.
How to Use the Bash Find Command to Find Files Using Dozens of Criteria
sort -r find. -iname “*.md”
Searching for and listing files in alphabetical order
If you prefer to save the sorted output to a text file instead of standard output on the console, add the –output option, like this: sort -r find. -iname “*.md” –output=sorted.txt. The –output option tells the sort command to create an output file for the sorted list of files.
The goal of this post was to show you how to sort data on a Linux computer using Bash commands. You’ve now learnt how to sort the contents of files as well as the file listing. You should also be able to pipe various commands together for more complicated file sorting.
Why not use this newfound knowledge to construct scripts that automate file listings and file content sorting?
The “bash sort descending” is a command-line tool that allows you to use bash to sort files like a boss. The command is used to sort the files in descending order.
- bash sort options
- bash sort string
- bash sort file in-place
- bash sort file by column
- bash sort man