Buy some stoppers. Most superstores carry small rubber stoppers you can use to stabilize the furniture. They are often found in the do-it-yourself or homemaking section. The stoppers are flat and will sit under the corners of the furniture to grip the carpet and prevent sliding. A rug with rubber on the bottom works in place of the stoppers.
Have a friend lift one side of the bookshelf. Slide the stoppers under the corners, as he provides some space. Make sure he lifts a side and not the front or back of the bookshelf. Lifting up the back will cause the books to fall off the shelves.
Make sure the rubber-gripping side is face-down and on the carpet, if there are two sides to the stopper and only one is rubber. If the other side is face down, the stopper is useless and will slide.
Wedge the bookshelf between other pieces of furniture if you do not want to buy the stoppers. Sandwiching the sliding bookshelf will stop it from moving so much as the other pieces of furniture will block it from sliding.
Move the bookshelf to a low-traffic area of your home if you do not wish to buy the stoppers or move your other furniture around. This will decrease the likelihood that anyone will bump it and cause it to slide.