Tag: Linux Tutorials

  • Linux Tutorial Series – 52 – Standard input and output streams

    Here is the video version, if you prefer it:

    We are going to go a bit conceptual. We are going to talk about so-called input and output streams.

    See, a computer program is pretty much indifferent to where it gets its data from (input) and where it displays the data it has calculated (output). The input can be either the keyboard, a file on your computer, some sensor, a sample of your voice, some other thing I haven’t thought of – it doesn’t matter; the computer program gets some input in whatever way, does something with it, and then outputs it. Outputs it where? On the computer screen, by default. But it can also write to a file, send the output over the computer network, or something else – there’s a lot of options.

    So remember – input and output streams, plainly said, are an abstract term used to answer the questions “Where does my computer program get data from?” and “Where does my computer program output the thing it computed?” This is going to be important when learning about some Linux things.

    There is also the standard error stream – if something goes wrong, it is passed to the standard error stream. (Ward, 2014)⁠

    Hope you learned something useful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 13-14

  • Linux Tutorial Series – 51 – Checkpoint

    Here is the video version, if you prefer it:

    Next up we have command pipelines, input/output redirection and regular expressions.

    All of these are used quite frequently and are thus important to understand. A note on regular expressions: regular expressions might not seem very useful at the time, but if you invest the time to learn them it will pay off, as you can’t resolve regular expression mistakes using Google as easily as some other types of mistakes. The same holds for when you will be learning about input/output redirection and pipelines. You will have to use them at some point and if you don’t know how, it will be to your detriment.

    So grit your teeth, read the words of Linux wisdom and go forth!

  • Linux Tutorial Series – 50 – Recap

    Here is the video version, if you prefer it:

    Here are all of the commands we have learned: 

    • The general command format is: command -options arguments
    • Commands are case sensitive
    • cd directory to change the directory you are located in (current directory)
    • pwd to print the absolute path to the directory you are in
    • man command gives you the man pages of the command
    • Options can be both short (-) and long (--)
    • Use -h or --help to see a quick description of command usage
    • ls command lists directory contents
    • cp src dest copies from source (src) to destination (dest)
    • mv old new renames file with filename old to filename new
    • rm item is used to delete the item; rm -r folder to delete the folder
    • touch filename to create a new file with name filename
    • mkdir dirname to create a new directory with name dirname
    • rmdir dirname to remove a directory with the name dirname (the directory has to be empty)
    • echo message to print out the message
    • cat filename to print out the contents of the filename
    • less filename to page through the contents of the filename
    • head and tail for viewing only the first or the last portion of a file
    • sudo command to execute the command as the superuser
    • su to switch user (without an argument switches to the superuser)
    • grep pattern file for pattern matching in files
    • wc file to count the number of words (and other stuff) in a file – haven’t used it as much

    Hope this refreshed your memory!

  • Linux Tutorial Series – 49 – The wc command

    Here is the video version, if you prefer it:

    The wc command is used to display the number of lines, words and bytes contained in files. (Shotts, 2019)⁠ It is used as follows:

    wc fileName

    It outputs three numbers – the first number is the number of lines in a file, the second number is the number of words contained in a file and a third number is the number of bytes contained in a file.

    An example:

    mislav@mislavovo-racunalo:~$ cat file.txt

    Some line

    Some other line

    mislav@mislavovo-racunalo:~$ wc file.txt

    2 5 26 file.txt

    I have personally never used this command (never had the need).

    Hope you learned something useful today!

    References

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Page 87

  • Linux Tutorial Series – 48 – The grep command

    Here is the video version, if you prefer it:

    The grep command is used to find text patterns within files. (Ward, 2014)⁠ Its usage is as follows:

    grep pattern filename

    grep outputs the lines which match the pattern. What is the pattern? The pattern is anything you want to match. You can use regular expressions here, but we are going to talk about that later. pattern, for our purposes right now, is a word or multiple words you want to find in a file. Say I had this file:

    mislav@mislavovo-racunalo:~$ cat file.txt

    Some line

    Some other line

    Here is what happens when I want to print out every line which contains the word (pattern) line:

    mislav@mislavovo-racunalo:~$ grep line file.txt

    Some line

    Some other line

    Here is the result if I want to print every line which contains the pattern other:

    mislav@mislavovo-racunalo:~$ grep other file.txt

    Some other line

    If I wanted my pattern to be composed of multiple words, I would have to put them under single quotations, like this:

    mislav@mislavovo-racunalo:~$ grep ‘other line’ file.txt

    Some other line

    If I wouldn’t I’d get an error. But you do know how to use Google for errors, do you?

    Useful options are -i for case insensitive matches (no difference between capital letters and “small” letters) and -v for inverting matches (printing every line that does not contain a match).

    Hope you found this helpful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 18-19

  • Linux Tutorial Series – 47 – The su command

    Here is the video version, if you prefer it:

    The su command is used to start a shell as another user (most often the superuser). (Shotts, 2019)⁠ If you want to use it, this is what you’d write:

    su [-[l]] [user]

    This is some new notation? What does this mean? Everything enclosed in square braces is optional, meaning you can execute the command without writing any of it. (Barrett, 2016)⁠ Couple of examples of valid commands:

    su

    su -

    su -l

    su -l mislav

    su - mislav

    su mislav

    All are valid. If you don’t specify the user it is assumed you want to start the shell as the superuser. The -l option means you want to change to that user’s home directory. To enter the previous shell, type exit.

    The -c option is used to execute a command as another user like so:

    su -c 'command'

    but I have never used it. In general, I have never found myself using the su command – whenever I want to do something as the superuser, I use sudo.

    A caveat: If you switch to the superuser, your prompt will change from $ to #, like so:

    mislav@mislavovo-racunalo:~$ su

    Password:

    root@mislavovo-racunalo:/home/mislav#

    Hope you found this useful!

    References

    Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Pages 171-172

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 126-127

  • Linux Tutorial Series – 46 – The sudo command

    To refresh your memory, let me remind you that in Linux there are two kinds of users – regular users and superusers.

    What is the difference? The superuser is able to do more than the regular user, basically. If you attempt to do some things as a regular user, which you don’t have permissions for, you will not be allowed to do that. Permissions are a topic for a different post, but you can think of the difference between the regular user and the superuser as follows – both regular users and superusers are in a club. Superuser is in the VIP section and regular users are in the “normal” section. Superusers can walk from VIP to the “normal” section and reverse, but regular users can’t get to VIP, because bouncers would ask them for the “special bracelet” and they wouldn’t have it, so they would not be allowed in the VIP section.

    The sudo command allows regular users to execute commands which require superuser permissions. More technically, the sudo command allows you to execute commands as some other user, but in most cases that user is the superuser. (Shotts, 2019)⁠ The syntax is as follows:

    sudo someCommand

    Basically, you prefix whatever you want to run with sudo. Then you will be prompted for your password and once you enter it, voila – you have executed the command as the superuser.

    Why isn’t everyone the superuser? Fair question. There are two answers which make sense:

    • If multiple people are using the computer, then one person (the most responsible person) should be the superuser (the administrator) and others should just be regular users in order not to mess something up
    • Ever heard that Linux is pretty secure (as in, not a lot of viruses)? Well, that is because of the separation of superusers and regular users. Even if you download a certain malware (malware is a malicious program that tries to do harm to your computer), if you are a regular user, the malware won’t be able to execute all of the commands and access all files on your computer and therefore the damage will not be as devastating as if you were the superuser. Think of it like this – if a fight breaks out in our imaginary club (call it “The Penguin’s Hideout”), if it breaks out on the “normal” section there will be some damage, but not much. However, if there is a fight in the VIP section, oh boy! All of those expensive bottles of champagne and the crystal vases and the wonderful decoration are destroyed. Same as what happens in the computer.

    Hope you found this useful!

    References

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 127-128

  • Linux Tutorial Series – 45 – head and tail commands

    Here is the video version, if you prefer it:

    The head command is used to display the first 10 lines of a file. The tail command is used to display the last 10 lines of the file. (Barrett, 2016)

    Both of the commands have an option -n which control how many lines you want to output. By default it is 10, but you could write

    head -n 12

    and head would output 12 lines at the beginning of the file.

    A really cool thing about tail is that it has the -f option, which enables you to watch a file actively; you get new lines which are written to a file displayed. Why is this useful? Well, if you are watching some system file (a file which contains information about devices connected to your computer, for example), you could find out some information about the newly connected device.

    Hope you learned something new!

    References

    Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Pages 60-61

  • Linux Tutorial Series – 44 – The less command

    Here is the video version, if you prefer it:

    The less command is used to paginate through contents of a text file. (Ward, 2014)⁠ You navigate it by using the following keys (these are the most important):

    • Space for page down
    • B for page up (no need to keep Caps Lock on; just press B on your keyboard)
    • Q for quitting less

    As you can probably suppose, less is better to use when reading text files than cat, because it outputs them more conveniently; it doesn’t just dump the contents of the file to your screen like cat does.

    Fun fact: man uses less (“Does man command invoke less command to display manual contents?,” n.d.)⁠

    Hope you learned something new!

    References

    Does man command invoke less command to display manual contents? (n.d.). Retrieved January 8, 2020, from https://askubuntu.com/questions/322851/does-man-command-invoke-less-command-to-display-manual-contents

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 19

  • Linux Tutorial Series – 43 – Did you see it yet?

    Here is the video version, if you prefer it:

    Heya,

    did you notice the general command structure? To remind you, it looks like:

    command -options arguments

    Let’s take a look at the commands we learned so far, applying the abstract command structure over them:

    • cd arguments
    • pwd
    • man arguments
    • ls options

    I hope this helped shed light on the underlying general command structure.

    Talk soon!