Remove first line of file

How can I use bash to remove the first line of a file?

Try:

sed 1d file > newfile
tail -n+2 file > newfile
awk NR-1 file > newfile

With bash:

{
  read
  while IFS= read -r line
  do
    printf "%s\n" "$line"
  done
} < file > newfile
1 Like

One extra point-- Scrutinizer did a great job of showing possibilities.

Some UNIXES like Linux sed allow for "edit in place"

So for sed - the -i option does that:

# no option
sed 1d file >newfile
mv newfile file  # back to the starting name

# -i option
sed -i 1d file
# you now have file with the first line removed, no redirection or renzming

As mentioned in another thread, if you tell us what operating system you're using, what shell you're using, what you're really trying to do (instead of how you want it done), and show us what you have tried and explained what about it is not working; we would have a MUCH better chance of helping you reach your goal.

If you goal is just to skip processing the first line from a file you're reading from standard input in a script, the simple answer is to change:

while read line
do	whatever
done

to:

read line
while read line
do	whatever
done

Please help us help you by telling us what operating system you're using, what shell you're using, giving us a complete description of what you're trying to do, giving us a short sample input file and showing the exact output you hope to produce from that input, and showing us what you have tried to do to solve the problem on your own.

2 Likes