Have you heard about Envirotechnical? Let's fight climate change one byte at a time :) Learn More →

logo
Published on

An adventure to get started with the Unix Shell, part 1

Authors

The why

I remember very well how it was the first time I tried to access the screen of power.

It was quite a few years ago now, but the shivers that were, I still feel today. That is the beautiful power of command line interfaces, or CLI, for short.

Remember The Matrix? All that green jibber jabber that made you believe coding was an absolute bummer if you were not effin Leonardo Da Vinci?

 

Smart guy uses CLI

Yeah, sorry to break it for you, that was a lie. A big fat lie.

Also, bonus points, you should probably install cmatrix if you are on a unix environment (that would be Ubuntu, MacOS or the likes).

This installation will make you look cool for the 80s and 90s kids. Other people just won't give a dime.

The how

So, you want to be cool like Oprah gifting cars to random strangers on TV but you don't know where to start.

Look no further my friend because I've got you covered.

Table of Contents

 

Oprah gifting CLIs

cd as in "change directory"

Every good story begins with a hero wanting to go somewhere, but where does our hero go without the legs? That's all cd is, hero's legs.

With cd you can move around the filesystem, as long as you have the permission to do so. Think it like this: You are a young wanna be hero, permissionless and clueless, so your cd command will move you only to some secured areas that your father, king Linux, wants you to see, like, say, your \home\hero\ folder.

Maybe one day you will grow to be a fine king, but for now let us dare no more and venture forth with our unique campaign.

ls as in "list"

Our adventure is near and our power is growing tenfold a day! But with great power comes great thirst, so we should make sure we are bringing all the juice we need for the journey, along other less important things, like swords and armors.

How do you know you are bringing the right stuff? Simple as listing the content of your backpack, or room, or folder, if you will.

ls /home/hero/backpack

# a magical list of resources appear!
-> coke, sprite, fanta, another coke, yet another coke, a knife with butter on it

All is well, a young hero is being born from knowledge and sugar fascination! But peril is near and he shall soon find out!

mv as in "move"

A double edged weapon in your hand, you start playing with mv.

Little did you know that this command would let you double up on your new juicy powers. You learnt to cd around the castle and now look at you, you are also moving things around with the mv command! Bravo!

But who is that? A strage figure approaches you. It is Master Kernel the Great (boring dude, if you ask me).

"Welcome young hero! I see you are becoming versed in the misteries of mv! What a delight!"

He is a boring master, true, but a knowledgeable one aswell. He teaches you that you can also rename your belongings with the very same command.

mv coke /home/hero/trashbin # will let you move the coke to the path of your choice
mv trashed coke # will rename the trashed coke to a new coke! How fancy!

# Now cleaner

mv [item name] [path] # to move the item to the path you want
mv [item name] [new name] # to rename the item

Boasting your new learnings and being kind of a prick, you cd to your sibling's room, located in another wing of the castle. Your plan is simple and ideal: steal the new bought games of your older sibling by moving them secretly to your room and deleting your sibling's name from them.

Any ideas how you would do that?

Oprah gifting CLIs
cd /home/sibling/bedroom
ls
# ps5 game, new ball, new toy
mv "new toy" /home/hero/bedroom
cd /home/hero/bedroom
mv "new toy" "my toy"

Didn't go quite right, tho, did it?

Permission denied, who would have thought? I guess we'll have to ask dad the King to buy us the same toy.

cp as in "copy"

And so you do just that, you ask audience with our father, King Linux, and pretend that the new toy is also gifted to you, the prodigy of the family.

Your father is aware of the situation but will not surrender to your subtle and gentle demands, so there's only one thing, as a brave young hero, that you can do. Enforce yourself.

sudo crybaby # a command to command, with sudo the king you might become

The power of your warcry is so disruptive that you now command the King, in a way. Of course, little do you know that another Permission denied lurks behind the throne, as your father was strict in the permissions he gave to you, being young and all that.

Lucky you, your older sibling has sudo access and was able to recognize your prowess, hence you were entitled to receive your toy.

cp /home/sibling/bedroom/toy home/hero/bedroom/games/toycopy

All is well and good once again, so you return happily in your room to finally entertain with the new gift.

rm as in "remove"

But your mother was right! You should have listened to her!

ls
# ...
# and infinite list of toys appear!

You manage to find the new toy with a trick taught to you by your older sibling, but you do care about what your mother, Queen Cli, tells you, so you also use another command to remove the unused toys from the drawer.

ls -la | grep "toycopy"
# finally, your toy!

rm -rf *
ls

Ah, the drawer is now completely empty. EMPTY?!

You start crying again, remembering what your mother, Queen Cli, told you about recursive and forced removal of items from the folders you own. Luckily your sibling Esteban is nearby and hates your guts when you cry, so:

cp /home/sibling/bedroom/toy home/hero/bedroom/games/toycopy

Better take care of this one.

rm [item], as seen, deletes the item of your choice, but not folders, unless you specify. Those -rf flags stand for recursively, forcefully, so you might want to take care and use them wisely.

mkdir as in "make a directory"

You are a fast learner! A fast learning hero! You may have encountered a few stones and bricks along your journey but nothing that could stop you.

Wiser from what you learnt, you decide to create a more meaningful place for your new toys and store them there.

cd /home/hero/bedroom
mkdir newtoysdrawer # creates a new folder, or drawer if you are into the story!
mv ./games/* ./newtoysdrawer # the ./ stands for "where we are right now!", aka relative path

Fantastic, you moved all your games to the new drawer in a single move! You also create random drawers for your younger siblings to get lost into.

mkdir fakedrawer1
mkdir fakedrawer2
mkdir fakedrawer3
# ... 2 days later
mkdir fakedrawer9000

You are having fun with the mkdir command but your dumb older sibling, Esteban, knows better.

"You should just change the modifiers, permissions to your game drawer, you know?".

Damn you and your silicon wavy hairs, Esteban!

But he is right and you know it.

chmod as in "change modifiers, permissions"

"All you want to do is make sure that the drawer is only usable by you and no one else. Of course, anyone with sudo powers would be able to open it, but that would just be dad, mom and, well, me." - says Esteban

chmod 700 ./newtoysdrawer
ls -la # list with -la flags shows a long list with attributes
# drwx------   4 hero  hero   128B  7 Feb 10:00 newtoysdrawer

The permissions have been changed and so you are now the only holder of power! But behold, a brothers' fight!

sudo chown esteban:esteban ./newtoysdrawer # change ownership of the folders or files
chmod 700 ./newtoysdrawer # change permissions of folders or files
ls -la # list with -la flags shows a long list with attributes
# drwx------   4 esteban  esteban   128B  7 Feb 10:00 newtoysdrawer

The three numbers in the chmod [xyz] command are used to issue permissions of usage (read, write, execute) to the user that owns the file (x), the group that owns the file (y) and all the others (z).

7 means that you have all the rights! There's a nice table to explain that!

7 4(r) + 2(w) + 1(x)
6 4(r) + 2(w)
5 4(r) + 1(x)
4 4(r)
3 2(w) + 1(x)
2 2(w)
1 1(x)
0 0 --- none

Also, your sibling is a prick. Did I mention that?

Esteban is right about chmod

chown as in "change ownership"

You go and cry to your mother, Queen Cli, whom was bestowed with the powers of sudo, also known as the power of royals!

sudo chown hero:siblings /home/hero/bedroom/newtoysdrawer # change ownership of the folders or files
chmod 750 /home/hero/bedroom/newtoysdrawer # change permissions of folders or files
ls -la # list with -la flags shows a long list with attributes
# drwxr-x---   4 hero  siblings   128B  7 Feb 10:00 newtoysdrawer

Your mom helped you out but also warned you that you ought to share your games with your siblings if you want to keep them. She made sure you will by changing the permissions again and the group ownership. Also, she scolded you for not studying.

cat as in "defaq"

"But I did study!", you tell her. And you did, I remember you did, but there's a point in what your mom said. You didn't really study, did you? You just opened the entire book altogether and tried to buffer everything into your brain. Fancy, but not ideal.

cat /home/hero/bedroom/desk/history is useful to show everything about a text file (and other things) that might interest you, but if your point is actually reading and understanding what you are doing, I'd say you're better off with the next command!

less as in "show me less stuff on the screen"

less /home/hero/bedroom/desk/history takes a little bit more time and effort on your side, as you have to read it page by page, in a sense, but it displays everything nicely and you can scroll through the pages instead of just eating the entire book like you would with cat

Bonus Points: man

Another day in the castle of the hero to be is over again but rest assured, the young hero will be back with more commands and powers to show! And you, being the hero and the reader altogether, make haste and learn them!

Hopefully you learnt a lot and remember a lot, but if you are willing to study a bit more, you should probably make treasure of this last, oh so magical command.

man [command]
man less

This way you will be presented with the manual concerning the command you just searched in all of its glory and flags! Now, don't fret about being unable to quit the man, you just need to press q, as in quit.

The goodies

There's a CLI (command line interface) game that I used with my Junior Developers to teach them about command line tools and to get them used to using the command line.

If you want to try, just head over to bashcrawl and git clone the repo.

The goodbye

I hope you found this article useful and to your liking and if you have any requests, drop a message on one of my social media accounts or open an issue/start a discussion on github, on this repository!

Wake up hero