---
title: "How to Sort Text Files: A Linux Shell Script"
description: "Check out this clever shell script for quick and efficient sorting of text files. It's a handy tool to have in your coding arsenal!"
url: https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/
date: 2023-09-24
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/09/pexels-photo-18289659.jpeg?fit=1880%2C1253&ssl=1
categories: ["Linux"]
tags: ["Check", "Sort", "Sort Shell Script", "Store"]
type: post
lang: en
---

# How to Sort Text Files: A Linux Shell Script

Here’s a simple shell script that sorts the lines in a text file alphabetically. You can use the sort command to accomplish this.

![Shell script to sort text file](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/09/pexels-photo-2008270.jpeg?resize=987%2C1300&ssl=1)*Photo by Dominika Roseclay on Pexels.com*

## Linux Shell Script [to Sort Text](https://srinimf.com/2023/08/26/how-to-sort-text-files-linux-shell-script-ideas/) File

Here the script is explained [step by step](https://srinimf.com/2021/09/03/scala-shell-how-to-run-commands-useful-video/).

```
!/bin/bash
#Check if a file name is provided as an argument

if [ $# -ne 1 ]; then
echo "Usage: $0 "
exit 1
fi

#Store the filename provided as an argument

filename="$1"

#Check if the file exists

if [ ! -e "$filename" ]; then
echo "File '$filename' not found."
exit 1
fi

#Check if the file is a regular file

if [ ! -f "$filename" ]; then
echo "'$filename' is not a regular file."
exit 1
fi

#Sort the file and create a sorted version

sort "$filename" > "${filename}.sorted"

#Check if the sorting was successful

if [ $? -eq 0 ]; then
echo "File '$filename' has been sorted and saved as '${filename}.sorted'."
else
echo "Sorting failed for '$filename'."
exit 1
fi
```

Save this script to a file, for example, **sort_file.sh**, and make it executable using the chmod command:

```
chmod +x sort_file.sh
```

You can then use it to sort a text file by running:

```
./sort_file.sh your_text_file.txt
```

Replace **your_text_file.txt **with the actual name of the file you want to sort. The sorted version of the file will be saved as **your_text_file.txt.sorted**. You can adjust the output file name and location as needed in the script.

## Travel Neck Pillow

Trajectory Travel Neck Pillow Rest Cushion for Travel in Flight car Train Airplane with 2 Years Warranty for Sleeping for Men and Women (Grey, Polyester)

- [Travel Neck Pillow](https://www.amazon.in/Trajectory-Economy-Pillow-Everyday-Travel/dp/B07S21FK7C?tag=srinimf-21)

Related

- [How to Sort Text Files: Linux Shell Script Ideas](https://srinimf.com/2023/08/26/how-to-sort-text-files-linux-shell-script-ideas/)DateAugust 26, 2023
- [How to Write Echo Command Correctly in Linux](https://srinimf.com/2022/12/03/how-to-write-echo-command-correctly-in-linux/)DateDecember 3, 2022
- [How to Write a Script in K Shell](https://srinimf.com/2017/11/01/ksh-script-steps-to-create-awesome-way/)DateNovember 1, 2017

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F09%2F24%2Fhow-to-sort-text-files-a-linux-shell-script%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F09%2Fpexels-photo-18289659.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=How%20to%20Sort%20Text%20Files%3A%20A%20Linux%20Shell%20Script)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/09/24/how-to-sort-text-files-a-linux-shell-script/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Sort%20Text%20Files%3A%20A%20Linux%20Shell%20Script&body=https%3A%2F%2Fsrinimf.com%2F2023%2F09%2F24%2Fhow-to-sort-text-files-a-linux-shell-script%2F&share=email)
-

### Like this:

Like Loading…
