Use the same strategy to secure a bookcase to a masonry wall whether the bookcase sits directly on the floor or hangs on the wall. It is important to use anchors and screws manufactured specifically for brick, or masonry, for a strong, safe installation.
Set the bookcase on the floor near the wall where it will be attached or hung. If the bookcase will sit directly on the floor, it can be secured to keep it from falling over. One inch above the top shelf, drill a 3/8-inch pilot hole with a wood bit every 2 feet. There should be at least one pilot hole near each end of the bookcase. By drilling just above the top shelf, the mounting screws will not be visible after the bookcase is secured.
Move the bookcase into place. With a 1/4-inch masonry drill bit, drill through the pre-drilled holes into the brick wall. Drill just enough to make an identifiable mark on the brick. This is for position only. Move the bookcase away from the wall. With a masonry drill bit, drill holes into the brick as marked. Insert the masonry anchors into the holes.
Move the bookcase back into place. Install the masonry anchor screws through the pre-drilled holes in the bookcase into the anchors you have inserted into the brick wall. If the head of the screw is smaller than the 3/8-inch pilot hole, add a washer to the screw before screwing into the anchor. Do not over tighten. The bookcase is now secure and will not tip over.
The exact same process applies for a wall-mounted bookcase. Level the bookcase at the height you want. You may need some help to hold the shelf or bookcase in place while drilling the marking holes and installing the final screws.
For a wall-mounted application, the anchors should be rated for the weight of the free-hanging bookcase, including the items that will be placed on it. Books are heavy. Masonry anchors and screws might have to be larger than 3/8 inch.