5 Things That Blew My Mind When Learning The Basics of Command Line

Abi Travers
7 min readJul 26, 2017

--

So here goes, my first week doing the pre-course at the Makers Academy coding bootcamp. It all started with a set of tutorials on the basics of command line.

After teaching myself online marketing & Growth Hacking it doesn’t half feel good having a bit of guidance and structure to my learning this time.

I get very excited about all things so naturally learning command line was one of them… So here is a post on the top 5 things I learnt doing this tutorial including when I went a bit off piste …. WARNING: Only very beginners will also find these things interesting …

1. The Spaces Are Important.

Put them in the wrong place or leave them out and you fill face the wrath of command line…(that is if you also feel like an error message can be considered as wrath).

I first came across this simple finding when executing the tail command:

tail -3 longtext.txt

I had foolishly written as:

tail-3 longtext.txt

In this example I am trying to pass the parameter ‘longtext.txt’ file to the command tail — 3 which prints that last 3 lines of a file. However, without the space in-between ‘tail’ and ‘-3’ all I was getting was an error message.

I am glad I learnt about observing the spaces in commands as this came in useful later on when I was running more ‘complicated’ commands.

2. ‘Read’, ‘Write’ & ‘Execute’ Permission.. and some …

Running the command:

ls -l

allows you to see which user classes have which type of access to a given file within a directory.

For those of you who are not aware the user classes are;

userYour accountgroupAny permissions group that your account belongs tootherAny account that is not yours and that does not belong to a permissions group that your account belongs to

When doing this I found more information against some of the files than the tutorial suggested.

The tutorial states that you can see the different permissions of a files in a directory by listing them in ‘long’ format with:

ls -l

This is what I saw when I did that:

Following the tutorial I learnt how to read the meaning of:

-rw-r — r —

The first — represents nil or not. Here is where a d appears if the file is a directory as opposed to a file. Because it is not a directory the — appears instead of the d.

rw- represents ‘read’, ‘write’ but the ‘-’ where the ‘x’ should be means no execute permission. This first sequence of three is the permission for the ‘user’ class (this is you on your computer). So the ‘user’ class has read and write but not execute permission on this file.

r — represents ‘read’, no write no execute permission. This second sequence of 3 is the permission for the ‘group’ class. The ‘group’ class in this subDirectory example is “staff”. A group class simply means a group of users. The best example of this is when there are several users accessing a computer or system remotely.

The last r — represents ‘read’, no write no execute permission for the last class, the “other” class of users. Other users are simply users that don’t fall into the other two classes.

So that is what I learnt from the tutorial … but what is that last @ at the end of some of this data?e.g.

-rw-r — r — @

So I had a google…

Turns out the @ means a file had extended attributes and that you can use the

xattr

command to view and modify them like so:

Wait… this raised some more questions …. what is an extended attribute?

Extended File Attribute

Regular attributes such as ‘permission’ have a purpose which is strictly defined by the filesystem they are part of (aka the directory etc).

Extended file attributes enable uses to associate files with metadata not interpreted by the file system.

The typical use of these are;

  • -storing the author of a document,
  • -the character encoding of a plain-text document,
  • -a checksum (a code which detects errors, for example in a digital network to detect accidental changes to raw data.),
  • -a digital certificate (a digital certificate used to prove the ownership of a public key),
  • -or ‘discretionary’ access control information (another way of restricting access permissions to groups or users but done by a ‘trusted computer system evaluation criteria’).

Ok well great but still what is com.apple.metadata:_KMDItemUserTags doing on my file??

com.apple.metadata:_KMDItemUserTags

My first bit of googling led me to find out that this MAY (yes i can’t even be sure of this right now) be associated with something called:

Mavericks

and that it is something to do with this Mavericks file tagging system.

Further googling has lead me to see that this is something to do with ISO’s, so my macs, file labeling/ tagging system. Ok this doesn’t 100% make sense still but I trust you apple, I feel this tagging system is helping me out in some way …

3. YOU can become a SUPERUSER / the “root”.

If you become a superuser you automatically get super powers

Some actions on a computer require administrative POWER to do. This power is only bestowed upon a SUPERUSER.

Turning yourself into a SUPERUSER means (as long as you know the superuser password) you can delete a file you haven’t got permission to delete…

I am pretty sure this isn’t the coolest thing it can do, especially with super in it’s name, but still, I am impressed.

You become a SUPERUSER (it sounds so powerful that it just needs capitals) by prefixing (starting) a command with the word sudo.

eg.

sudo rm inaccessiblefilesname

Allows you to delete that pesky inaccessible file.

4. You can see all the processes your computer is running.

The ‘ps’ command allows you to see what processes you have launched within a directory. You can use the ‘x’ flag to see ALL processes running on your computer:

ps x

You can filter for the processes you want to see by redirecting the output to grep, then use the wc to count the number of them instead of listing:

ps x | grep bash | wc -l

Will count all the bash processes you have running.

5. You can hide secrete data in your code!

You do this by modifying environmental variables!

Modifying environmental variables in command line allows you to hide sensitive data like passwords or secret keys which allow you access to information you want to keep private.

Say you are making a Ruby programme which calls on the users facebook photos. You want to make this programme open source and you need to use your facebook photos as a sample. You can set an environmental variable with a secrete key, e.g.

export SECRET_KEY=1453637600kk

in your command line. Then in your Ruby code read this variable with:

secret_key = ENV['SECRET_KEY']

At the moment this environmental variable will be deleted when we exit the current terminal session. This would be an issue if we wanted to call on these variables in our Ruby code on a more permanent basis.

To solve this we can create permanent env vars (environmental variables) by putting them in our bash profile:

echo "export SECRET_KEY=1453637600kk" >> ~/.bash_profile

The double >> means append, whereas single means overwrite, so be careful to use two here!

This nifty command line control also allows you to modify paths which I am sure will also come in handy for security reasons in the future. But for now, I am the most impressed by it’s ability to hide data from those you don’t want accessing it.

So there you have it, my MINDBLOWING 5 things I got most excited by when learning the basics of command line. Yes I know what you are thinking .. ‘this girl is very easily amused’…

This post was written on my blog first before I ‘migrated’ it over here … If you want to check out the original you can do that HERE.

--

--

Responses (2)