Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 29, 2025
When using Linux and during general data science and manipulation, we might often encounter situations where we need to work with large files that may contain duplicate data. Identifying duplicates by line can be essential for understanding the activities and tracking recurring issues to make better decisions.
For example, duplicate lines may indicate repeated requests, errors, or security threats. By identifying these duplicate lines, we can refine the data and optimize system performance.
In this tutorial, we’ll explore various commands like sort, uniq, and awk, as well as a Bash script to identify duplicate lines in a file without deleting them.
Let’s consider a sample file, duplicate_check.txt, and display its content:
$ cat duplicate_check.txt
apples
oranges
bananas
apples
Watermelon
oranges
mangoes
apples
After reviewing the content, we can observe that some lines are repeating, e.g., apples and oranges.
However, as discussed earlier, if a file contains hundreds or thousands of lines, it may be difficult to track duplicate lines manually.
So, the methods we discuss can mostly be applied to files of any size with minor caveats.
We can use the sort command to arrange the content of a file or stream in alphabetical order, making it easier to spot duplicate lines:
$ sort duplicate_check.txt
apples
apples
apples
bananas
mangoes
oranges
oranges
watermelon
In the output, we can see that the duplicate lines are now grouped.
However, it’s worth noting that the sort command also shows those lines which aren’t duplicates. So, we still manually examine the output and look for repeated lines to identify duplicates.
The primary purpose of the uniq command is to identify and remove duplicate lines from a file. However, the uniq command only works with consecutive duplicate lines. Therefore, we should combine it with the sort command for effective use.
For example, we can use the -d option to display only the duplicate lines by piping the input from sort:
$ sort duplicate_check.txt | uniq -d
apples
oranges
Here, the pipe operator (|) redirects the output of the sort command to the uniq command.
In the output, only the duplicate lines are displayed, but we don’t see how many times each line appears.
For this purpose, we can combine the -c and -d options to display the count of duplicate lines:
$ sort duplicate_check.txt | uniq -cd
3 apples
2 oranges
Now we can see the exact count of each duplicate line. In this case, these are 3 for apples and 2 for oranges.
The awk command is a powerful tool that we can use to perform complex text-processing tasks such as identifying duplicate lines:
$ awk '{ count[$0]++ } END { for (line in count) if (count[line] > 1) print line }' duplicate_check.txt
apples
oranges
Now let’s understand the command:
To get the exact count of each duplicate line, we can make minor changes to the above awk command:
$ awk '{ count[$0]++ } END { for (line in count) if (count[line] > 1) print count[line] " " line }' duplicate_check.txt
3 apples
2 oranges
In this command, we added print count[line] ” ” line which prints the number of the line, followed by space and then the line itself, if the condition is true. Effectively, we code the behavior of the uniq command we previously demonstrated.
In addition to the above commands, we can also create a Bash script to identify duplicate lines in a file:
$ nano find_duplicate.sh
#!/bin/bash
read -p "Enter the file name: " file_name
declare -A lines
while IFS= read -r line; do
(( lines[$line]++ ))
done < "$file_name"
for line in "${!lines[@]}"; do
if [ ${lines[$line]} -gt 1 ]; then
echo "${lines[$line]} $line"
fi
done
Now let’s understand the script:
Moreover, we also assume that the file is in the same directory as the Bash script. Otherwise, we need to provide the complete path to the file.
In this article, we discussed several commands like sort, uniq, awk, and Bash scripting to identify duplicate lines in a file.
The sort command is simple but may not be effective for larger files. The uniq command efficiently identifies duplicates but requires a combination with the sort command.
Finally, both awk and Bash scripting provide flexible solutions but require a good understanding of their syntax.