Skip to content

Small time saving grep trick for newbs like me who are reinstalling at least once a day on average...

The most tedious part of the Arch install process is cutting out all the superfluous servers out of /etc/pacman.d/mirrorlist using nano (for why no Vim on the live image?!). Of course, there's probably some trick to nano that I don't know, but whatever.

I thought to myself just now, as I reinstalled for the nth time, that there must be an easier way. So I did a little man grep and discovered that with the -A switch you can make grep return as many lines as you like that trail positive results.

What that means for our Mirrorlist is that you can search for your country's name, and bounce all results with the next line as well into a new file:

cat /etc/pacman.d/mirrorlist | grep -A 1 Canada > mirrorlist

You need the trailing line because the line that contains the country's name is a comment, whereas the actual server address is on the next line. If you were parsing a file which had three-like sections, you would then similarly use -A 2, and there is also a function to copy leading lines, in case the field you're searching for is nested within a function or section of regularly-formatted lines. After you've cut out the separators (or maybe you know the trick to do it automatically in the same command line - I was too lazy to look it up, but I'm sure it's pretty durn simple, actually, now that I think of it...), replace the old mirrorlist with the generated one:

mv mirrorlist /etc/pacman.d/mirrorlist

Only issue is that it adds a '--' in between the discontiguous results, but it's a lot easier to open up a file that contains only mirrors from your own country, and then cut out the separators and the slowest servers. In my case, Canada only has 6 mirrors, so I just had to delete the separator lines.

It's only about 30 seconds of work saved, but if you're doing this daily like I currently am, it's handy in that otherwise tedious moment. It will also make you more curious about what other handy functions are hidden in those commands you're using every day. and curiosity leads to greater bash-fu.

5 points25 comments
Open on Reddit
About this post
Posted
Archived
Score
5 · 100% up
Kind
Text
ID
2tw0wi

Author

24
posts
55
comments
1
media
View profile

26 comments

Just one question comes to mind: why are you reinstalling daily?

8 points

Cause I learn by breaking stuff. Sometimes (all the time) when I'm learning something new, I end up messing with this or that config file, and realize that I have foo'd the bar good and well. I imagine there is an easy command that will wipe all config files and reduce the system back to base group, but I dunno it yet.

Also, it is a minor goal to be able to install Arch without having to consult the Installation guide, which I currently still do. No reason, I just don't like that I need to rely on a paper to do something that basic.

Because it's fun, dammit!

5 points

Virtual Machines, yo.

I have like 6-7 linux distros in various stages of disrepair on virtualbox. Saves a lot of effort and moments of panic when you think you accidentally wiped your data drive with cfdisk

All the fun of distro hopping with none of the drawbacks. Also, when you learn a new neat trick, you can apply it to your working system right away.

1 points

In order to pare down the mirrorlist on the live image, I simply install reflector and use it to create a good, clean mirrorlist.

6 points

Can you install stuff in the live image? I think I tried once and couldn't. I'll have a look at that.

1 points

Newbs? I've been using Linux for 3 years and consider myself a low-level expert on it, but still reinstall every 3-7 days.

1 points

The comment was mostly to forestall anyone thinking that I was presenting myself as any kind of expert. I just discovered something cool.

2 points

Backup files before messing with them? That made it easy to configure booting directly with efibootmgr really easy, I just backedup my file and restored them from the livecd when they wouldn't work.

1 points

I haven't really understood your problem, but:

  • cat file | grep patter is equivalent to grep pattern file
  • to filter out the --, just do grep -A 6 Canada mirrorlist | grep -v '\-\-'
1 points

-A 6 would return each Canada server, plus the two servers after that, and an extra comment besides.

However, the -v, good to know, thank you. I also had no idea you could use grep that way. Like I said, newb... :>

1 points

(for why no Vim on the live image?!)

You could also install vim during the install, I suppose. :-) Both in the chroot and in the live environment.

1 points

I just cut the highest rated mirror from the list and pasted it at the top. Takes about 5 seconds.

1 points

Neat. I think the two things I would like on my install disk is a personal copy of my wireless settings and a pacman list that only has the https servers. I thought about just storing my prefered text files on the USB stick, but there are many ways to do things!

1 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.