Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete first line of all files in a folder (on ubuntu)

I have a folder that has 2800 .txt files in it and I need to delete the first line of every files. The names of the files are all different except for that fact that they end with .txt .

would it be possible to do that while keeping the same file name (instead of sending the output (file without the first line) to another file)...

like image 452
SparksHallow Avatar asked Jun 11 '15 14:06

SparksHallow


2 Answers

something like this does the trick

sed -i '1d' *.txt

where -i is inplace editing

edit: adition

pls try also this

time sed -i '1d' *.txt

and compare with other solutions (just add time before)[trying certainly with some backup files]

like image 69
josifoski Avatar answered Sep 23 '22 16:09

josifoski


You could do a bash script. Something like this:

#!/bin/bash
for filename in *; 
do 
    tail -n +2 "${filename}"
done

The run it from command line: $ <script_file.sh>

Take this with a grain of salt. I'm not actually running on a *nix machine. See here for a variety of ways of deleting the first line of a file. Also note that tail is supposed to be much faster than sed, if performance is important to you.

like image 45
Paul Sasik Avatar answered Sep 20 '22 16:09

Paul Sasik