+4 votes
in Operating Systems by (37.5k points)
Which Linux command should I use to delete the last row of a file?

1 Answer

+2 votes
by (43.2k points)
selected by
Best answer

To delete the last row from a file, you can use the 'sed' command with 'd' and '$' options.

'$' - Match the last line

'd' -  Delete pattern space 

Use the following command:

sed -i '$d' file_name

$d should be inside the single quote ('). Without a single quote, the command will give an error. If you use a double quote ("), the command will not delete the last record.