[Request] Copying a file with cp except for the last one or two lines.

Hi folks! I'm new to the unix-side of the world! :wink: and I'm trying to learn as much as possible the good stuff that's in it!
So.. here comes the point.. as you can see in the title, I need to copy a file but truncating it so that last 1-2 lines are not copied... any suggests from the mastercoders? :slight_smile:

i dont think cp has any option to copy like that directly. As a workaround you could do something like below

to copy except last line :

cat souce_file | sed '$d' > dest_file

to copy except last two lines :

cat souce_file | sed '$d' |sed '$d'  > dest_file
1 Like

Any reason for skipping the last 1-2 lines? Are these "special" in some way from the other lines?

With GNU-head the syntax for copying and cutting the last 2 lines:

head -n-2 source_file >dest_file
1 Like

Nice workaround.. what does sed $d do? I can't get it.. (my bad)

Sure :wink:

Ohhh yeah, that's just what I was looking for! :slight_smile:

$ is the address of the last line of input and d is for deleting the pattern (which in this case is none..so delete the entire line)

1 Like

You get a Useless Use of Cat Award. :wink: