I am trying to get rid of the first two lines of multiple text files. I know the command to do it for just one file using

sed -i '1,2d' filename.dat

But how can I run this command for multiple files? Can I do this using .sh file?


sed takes multiple file names as arguments. Just pass them along:

sed -i '1,2d' file1.txt file2.txt file3.txt

If you file names follow a certain pattern, you can take help from shell globbing e.g. for the above:

sed -i '1,2d' file{1..3}.txt

For all .dat files in the current directory:

sed -i '1,2d' *.dat
  • 2
    Note for those wishing to simulate this without the -i (--in-place) option, you will need to add the -s (--separate) flag, otherwise only the first two lines of the first input file will be affected (-i with multiple files implies -s since it wouldn't make sense otherwise). Aug 23 '16 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.