How can I reverse the order of lines in a file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Reversing the lines of a file means preserving each line exactly while changing only their order. The best solution depends on your environment and file size: command-line tools are fastest for quick shell work, while a small program is better when the logic belongs inside an application.
Use a Native Command When One Exists
On many Unix-like systems, the simplest tool is tac, which prints a file from the last line to the first.
That command is ideal for logs, exports, and one-off data inspection. It preserves line content and reverses by original position, not by alphabetical sort order.
If tac is unavailable, you can still solve the problem in a portable scripting language. The important idea is to reverse the sequence of lines, not to sort them. Sorting would change the data order based on text comparison, which is a different operation entirely.
Reverse the Lines in Python
For application code or cross-platform scripting, Python is straightforward:
splitlines(keepends=True) is doing an important job here. It preserves newline characters, so the output keeps the same line endings instead of collapsing everything into one long string.
If you want to overwrite the original file in place, write to a temporary file first and replace the original only after the write succeeds. That avoids corrupting the source file if the process fails halfway through.
A Java Version for Existing JVM Code
If the task belongs inside a Java application, you can read the lines into a list, reverse it, and write them back out.
This approach is readable and perfectly reasonable for small or medium files. The tradeoff is memory use: it reads the entire file into memory at once.
Consider File Size Before Choosing an Approach
Many examples online assume the file is small enough to fit comfortably in memory. That is fine for configuration files or ordinary reports, but it becomes risky for very large logs or dumps.
For big files, a purpose-built tool is often preferable because it can use optimized buffering. If you do need to implement your own large-file reversal, the problem becomes more involved because you have to scan the file in chunks from the end while reconstructing lines correctly.
In practice, that complexity is usually unnecessary unless reversing huge files is part of a production workflow. For day-to-day work, choose the simplest correct option first.
Common Pitfalls
- Sorting lines instead of reversing their original order.
- Forgetting to preserve newline characters when rebuilding the output.
- Overwriting the source file directly without a safe temporary file.
- Reading a very large file fully into memory without checking size.
- Assuming every platform has
tacinstalled.
Summary
- Use
tacfor the fastest shell-based solution when it is available. - In Python or Java, reverse the line sequence rather than sorting it.
- Preserve newline characters so the output structure stays intact.
- Prefer a temporary output file when replacing the original.
- Check file size before using an in-memory approach.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.