Tag: Linux shell

  • Linux Tutorial Series – 61 – Checkpoint

    Here is the video version, if you prefer it:

    In the following articles, we will talk about command types (not all commands are of the same type), some file-related commands and searching for files.

    Searching for files is the thing you will use most often, but it pays to know what command types are there and how to see the difference between two files, for example. Make sure to pay attention to searching for files and do read through other content, but again, it isn’t going to be of that much importance.

    Talk soon!

  • Linux Tutorial Series – 60 – Recap

    Here is the video version, if you prefer it:

    A flashback – we talked about:

    Redirecting input and output:

    • ls > output.txt is an example
    • Use >> to append
    • Use &> to redirect both standard output and standard error

    Other stuff we talked about:

    • Pipelines are used to redirect standard output of one command to the standard input of the other command; usage: command1 | command2
    • Shell globbing (wildcards) is used to match filenames before the command is executed
    • Brace expansions – something{else,else2,else3} (no spaces between the commas)
    • Regular expressions are used to match patterns in text; for example, a* means match the character a zero or more times

    Hope you refreshed your memory!

  • Linux Tutorial Series – 59 – More information about regular expressions

    Here is the video version, if you prefer it:

    What we covered in the previous post are so-called basic regular expressions. There are two things, for those of you who are interested, to look into (alongside references for each):

    • POSIX character classes (“POSIX Bracket Expressions,” n.d.)⁠
    • Extended regular expressions (“Understanding Regular Expressions,” n.d.)⁠ (this reference covers what is covered in this article, as well as extended regular expressions)

    I have personally never used regular expressions in grep because I usually want to find out if a file contains some particular word. However, I have used regular expressions in programming languages (like Python) to clean up my input, so I think you learned something useful because you will be able to use the basic regular expressions if the need arises and you have gotten an introduction to something you will probably use from time to time if you decide to write computer programs.

    Hope this was useful!

    References

    POSIX Bracket Expressions. (n.d.). Retrieved January 13, 2020, from https://www.regular-expressions.info/posixbrackets.html

    Understanding Regular Expressions. (n.d.). Retrieved January 13, 2020, from https://linuxconfig.org/understanding-regular-expressions

  • Linux Tutorial Series – 58 – Regular expressions construction in Linux

    Here is the video version, if you prefer it:

    Regular expressions are symbolic notations used to identify patterns in text. (Shotts, 2019)⁠

    The more complex explanation of what regular expressions are goes into theoretical computer science (more specifically, automata theory) and is way out of scope for this post (and I would have to review the stuff I learned in my Introduction to the theory of computation course). But, what I will say is just this – people have found some clever mathematical ways of describing patterns in text. In order to find out more about the development of the idea of regular expressions, have a look at the Wikipedia history entry here: (“Regular expression,” n.d.)⁠

    So, regular expressions help you find patterns in text. That’s their usage.

    The time has come to become acquainted with regular expressions. To repeat, regular expressions allow us to match patterns in text. (Shotts, 2019)⁠ It is important to note that regular expressions differ from shell globbing (wildcards), since shell globbing is related to the shell and regular expressions are used to match patterns in text on a much broader level (regular expressions are used in programming languages, for example, while wildcards are only used in the shell). More explanations can be found in (“Regular expressions VS Filename globbing,” n.d.)⁠ and (“Globbing and Regex: So Similar, So Different,” n.d.)⁠

    Let’s first list the special characters in regular expressions, then show them applied to a couple of examples:

    • Any character is denoted by .
    • * denotes zero or more characters; a* means zero or more characters a (“Basic Regular Expressions: Kleene Star,” n.d.)⁠
    • Anchors are denoted by ^ and $; they denote beginning and the end of the string pattern we are matching, respectively
    • Bracket expressions are denoted with [] – if ^ is the first character in the bracket expression, we treat it as a negation (meaning match everything except the thing in the bracket expression); if ^ is not the 1st character in the bracket expansion, it is matched literally
    • - denotes a range in a bracket expression; if it is the first character, it is matched literally, if not, then it denotes a range; you can have multiple ranges (as in [A-Za-z] if you wanted to capture all the letters)

    Let’s look at a couple of examples. We will use grep, because grep’s name is actually globally search a regular expression and print (“grep,” n.d.)⁠. So grep was actually about regular expressions all along! If this was a mafia movie, grep would now get shot and thrown in the sea by the docks. Anyway, let’s get back to our examples:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘a.*’ aba.txt

    Abba Money

    In the Rich Man's World

    What I said here is match every line “that has the small letter a followed by any character zero or more times”. It did so.

    Another example:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘^Ab’ aba.txt

    Abba Money

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘.ld$’ aba.txt

    In the Rich Man's World

    Here I matched every line that “begins with Ab” and every line that “ends with any character, then ld”.

    Another example:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘[A-Z]’ aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘[AI]’ aba.txt

    Abba Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘[^A-Z]’ aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    mislav@mislavovo-racunalo:~/Linux_folder$ grep ‘^[^A-Z]’ aba.txt

    [A-Z] says find any line that “has letters A to Z in it”. The second grep call (with the regular expression [AI])says “find any line that has letters A or I in it”. [^A-Z] says find any line that “has a character which is not A to Z in it”. This prints every line, since every line contains lowercase letters. However, the regular expression ^[^A-Z] says “anything that does not begin with A to Z”. Since every line begins with an uppercase letter, I get no output.

    I just wanted to note that it is vital to enclose regular expressions in quotes. Prefer single quotes over double quotes; take my word for it now, you will see the difference between those types of quotes later on. (“What’s the Difference Between Single and Double Quotes in the Bash Shell?,” n.d.)⁠). Otherwise, expansion can occur in a place where you meant to pass a regular expression, because expansion occurs before the command is executed. Just remember this, but for an example refer to (“Globbing and Regex: So Similar, So Different,” n.d.)⁠.

    Hope you learned something useful!

    References

    Basic Regular Expressions: Kleene Star. (n.d.). Retrieved February 19, 2020, from https://chortle.ccsu.edu/FiniteAutomata/Section07/sect07_16.html

    Globbing and Regex: So Similar, So Different. (n.d.). Retrieved January 13, 2020, from https://www.linuxjournal.com/content/globbing-and-regex-so-similar-so-different

    grep. (n.d.). Retrieved January 13, 2020, from https://en.wikipedia.org/wiki/Grep

    Regular expression. (n.d.). Retrieved January 11, 2020, from https://en.wikipedia.org/wiki/Regular_expression#History

    Regular expressions VS Filename globbing. (n.d.). Retrieved January 13, 2020, from https://askubuntu.com/questions/714503/regular-expressions-vs-filename-globbing

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

    What’s the Difference Between Single and Double Quotes in the Bash Shell? (n.d.). Retrieved January 13, 2020, from https://www.howtogeek.com/howto/29980/whats-the-difference-between-single-and-double-quotes-in-the-bash-shell/

  • Linux Tutorial Series – 57 – Brace expansion

    Here is the video version, if you prefer it:

    Brace expansion is used to pass multiple arguments to a command. (Barrett, 2016)⁠ An example:

    mislav@mislavovo-racunalo:~/Linux_folder$ echo my{File1,File2,File3}

    myFile1 myFile2 myFile3

    So the shell performed its expansion and I have 3 filenames because of that. Note that there are no spaces between the commas and the next letter. If I put a space, I would get:

    mislav@mislavovo-racunalo:~/Linux_folder$ echo my{File1, File2, File3}

    my{File1, File2, File3}

    Useful to know.

    Hope you learned something new!

    References

    Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Page 30

  • Linux Tutorial Series – 56 – Shell globbing (aka wildcards)

    Here is the video version, if you prefer it:

    Today we are going to talk about shell globbing (sometimes referred to as wildcards). They both refer to the same thing (“Globbing vs wildcards,” n.d.)⁠, so I will use the names interchangeably, or just stick to wildcards since it is shorter.

    Wildcards enable us to specify a set of file names using a shorthand. (Barrett, 2016)⁠ Let’s look at an example. Say I had these files:

    mislav@mislavovo-racunalo:~/Linux_folder$ ls

    aba.txt ab.txt a.txt cb.txt file.txt

    Good. And let’s say I wanted to print out the contents of all the files whose filenames start with a. I could do so the tedious way as follows:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat a.txt

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

    mislav@mislavovo-racunalo:~/Linux_folder$ cat ab.txt

    The more efficient way of doing this is to write the equivalent of the 3 above statements, which is:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat a*.txt

    What did I just do here? Am I a magician? Well, not really, so let’s look at what happened.

    As I stated above, wildcards enable us to specify a set of file names using a shorthand. With this particular wildcard (a*.txt), I am saying: “Give me all the filenames that start with a, have zero or more consecutive characters afterwards, and end with a .txt”. So in some intermediary step, my command looks like:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt ab.txt a.txt

    Now here is something important – the shell does all of this expansion (this is how it is called – turning a*.txt to all of the filenames) before it executes the cat command. So, the expansion of the wildcard is done before the command runs. (Ward, 2014)⁠

    Here is a list of wildcards and their meanings; the wildcard and its meaning is delimited with a dash (Barrett, 2016)⁠:

    • * – zero or more consecutive characters
    • ? – any single character
    • [set] – any single character in the given set; [abcde] matches characters a, b, c, d and e, while [a-z] matches all lowercase characters from a to z
    • [^set] or [!set] – anything not in the set (both [^set] and [!set] have equivalent meaning); i.e. [^1] is anything but the digit 1

    There are also some specifics:

    • If you want to include a literal dash in the set, put it first or last
    • To include a literal closing square bracket in the set, put it first
    • To include the ^ or the ! symbol literally, don’t put it first

    Thank you for reading and hope you learned something useful!

    References

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

    Globbing vs wildcards. (n.d.). Retrieved January 11, 2020, from https://unix.stackexchange.com/questions/413357/globbing-vs-wildcards

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

  • Linux Tutorial Series – 14 – Starting a shell session

    Here is the video version, if you prefer it:

    To start a shell session, find Terminal on your Linux distribution (via the graphical user interface) and click on it. That should start a shell session, which means you can type in commands!

    We will be writing some commands in the very next article, so stay tuned!

  • Linux Tutorial Series – 12 – What is a shell?

    Here is the video version, if you prefer it:

    The shell, the shell… We all heard that. We know you can type some command in the shell in a Linux environment and get some output. But what does the shell actually mean?

    “A shell is a program that runs commands”, says (Ward, 2014)⁠. That is basically it. You type some commands in, the shell executes them, then you get some output. Shell scripts, another popular term, are essentially just commands typed in a file called a shell script; so the shell has the same job – execute commands.

    There are multiple kinds of shells. You can find more information here: (“5 Most Frequently Used Open Source Shells for Linux,” n.d.)

    Before I end, it is important to note that programs like Terminal are not actually shells – they are graphical user interfaces running shell on your behalf. (Barrett, 2016)⁠ Figure 1 depicts this. This program with a graphical user interface, called a terminal emulator, interacts with the shell. (Shotts, 2019)⁠

    Figure 1 – Relationship between graphical user interfaces to the shell and the shell itself (modeled after figure on page 15 of (Barrett, 2016)⁠)

    Hope you learned something new!

    References

    5 Most Frequently Used Open Source Shells for Linux. (n.d.). Retrieved December 22, 2019, from https://www.tecmint.com/different-types-of-linux-shells/

    Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Page 15

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

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