Create a script to update the Pacman mirror list

February 21, 2021

Overview

This will be a short post covering the Bash script I use to easily update my mirror list for Pacman on Arch. This script was created from the examples provided on the ArchWiki page for Reflector. I run this immediately before I install or upgrade any packages. I prefer more manual control in most cases, but this can automated in several ways (just read the wiki page). Note the sleep timers palced between each command. I know this is not required, but I like to slow things down a bit when I’m writing statuses to the terminal as the script runs. There are other (probably better) ways to do this, but this has served me well for many years. I just wanted to share this, and hopefully it will be of use to someone.

Deleting files and making backups

The first part of the script just does some housekeeping. The echo statements will just print to the terminal to explain/document what is happening. On line 3, the script is removing any pacnew or previous backup files that may exist in the pacman.d folder. With line 4, the script creates a backup of the existing mirror list (a backup is always a good idea).

echo "Creating mirrorlist backup..."
sleep 1
rm /etc/pacman.d/mirrorlist.*
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
echo "Creating new mirrorlist..."
sleep 1

Generating the list and refreshing Pacman

Now, the new mirror list will be generated with Reflector. The options I selected will pick the top 10 fastest mirrors in the United States. There are many options for Reflector, and many of them are shown in the wiki. By adding the --verbose option, Reflector will print the output to the terminal. Lastly, Pacman’s package list is updated.

reflector --verbose --country 'United States' --latest 10 --sort rate --save /etc/pacman.d/mirrorlist
sleep 1
echo "Refreshing package list...Syy"
sleep 1
pacman -Syy

All Together Now

I have this script saved in my home folder as update-mirrors.sh. Sudo is required when running the script as it modifies files in the /etc/pacman.d directory and runs Pacman. Whenever I get ready to use Pacman, I just run sudo ./update-mirrors.sh while in my home folder, and everything is ready to go.

#!/bin/bash
echo "Creating mirrorlist backup..."
sleep 1
rm /etc/pacman.d/mirrorlist.*
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
echo "Creating new mirrorlist..."
sleep 1
reflector --verbose --country 'United States' --latest 10 --sort rate --save /etc/pacman.d/mirrorlist
sleep 1
echo "Refreshing package list...Syy"
sleep 1
pacman -Syy