SAO PAULO (Reuters) - Brazilian security officials worked to complete an improvised wall of metal shipping containers on Sunday inside a prison where rival gangs have clashed in the past week, resulting in the brutal killing of 26 inmates.

Slideshow ( 5 images )

The rusty red containers were hauled into the Alcacuz prison yard in northeastern Brazil, with officials aiming to finish the temporary wall, stacking one container atop another, by sundown Sunday.

It is a desperate move for security officials struggling to keep control of Alcacuz, where the latest in a string of brutal prison massacres in Brazil’s north and northeast took place Jan. 14. Members of the nation’s most powerful gang attacked rivals with machetes and knives, beheading and quartering many of the 26 killed.

The outbreak of violence was the latest in Brazil’s beleaguered penitentiary system, where about 140 people have died in clashes since Jan. 1.

The overcrowded prisons are now the battleground in a quickly escalating war between the nation’s two biggest drug gangs, the Sao Paulo-based First Capital Command (PCC) and the Red Command based in Rio de Janeiro.

For two decades, the two factions have maintained a working relationship, ensuring a steady flow of drugs and arms over Brazil’s porous border. But about six months ago, the PCC began trying to muscle the Red Command out of key drug routes.

The PCC has aggressively moved into new areas in the north and northeast of Brazil, where the deadly prison riots have taken place in recent weeks. In response, the Red Command allied itself with local gangs, enlisting them to take on the PCC.

The killings began on Jan. 1, when the powerful North Family gang, an ally of the Red Command, killed 56 inmates at a prison in Amazonas state, mostly PCC members.

The North Family controls a lucrative cocaine route along the Solimoes, a branch of the Amazon that flows from Colombia and Peru, the world’s top two cocaine-producing nations.

The PCC retaliated on Jan. 6 by killing 33 inmates at the Monte Cristo prison in the neighboring state of Roraima and then carrying out the killings at Alcacuz this weekend.

Then at Alcacuz in Rio Grande do Norte state, PCC members slaughtered rivals belonging to the “Crime Union of RN” - a gang carrying the state’s initials - whose members were killed last weekend by PCC inmates.