Tag: Linux terminal

  • Linux Tutorial Series – 67 – apropos, whatis and info – commands I never used

    Here is the video version, if you prefer it:

    Hello there,

    I wanted to introduce you to 3 commands I never used, but may come in handy in some cases (although so far I have never encountered them).

    The first is apropos. It is used to find commands appropriate for something. I use Google for this task.

    The second is whatis. It is used for short, one-line command descriptions. I use --help or (much less frequently) man. Much more frequently, I use Google to find out what the command does.

    The last command on the list is info. I use man over info and Google over man. To add something here – I have seen some instances where people recommend checking out info pages (accessed through the info command), but that is recommended in an answer on StackOverflow which gives the answer and then suggests looking at the info pages.

    There you have it – the commands and the alternatives I use. One of them is Google, but hey, if you are on a desert island and you simply must figure out what command is appropriate for something, use apropos.

    Thank you for reading! Hope you learned something useful.

  • Linux Tutorial Series – 66 – The alias command

    Here is the video version, if you prefer it:

    The alias command is used to construct your own commands which are made out of existing commands. (Shotts, 2019)⁠ Here is an example of how it is used:

    mislav@mislavovo-racunalo:~$ alias ll='ls -l'

    mislav@mislavovo-racunalo:~$ ll

    total 76

    drwxr-xr-x 27 mislav mislav 4096 Sep 19 14:00 anaconda3

    drwxr-xr-x 26 mislav mislav 4096 Dec 30 00:40 'Calibre Library'

    drwxr-xr-x 2 mislav mislav 4096 Aug 25 12:21 Desktop

    drwxr-xr-x 26 mislav mislav 4096 Jan 16 09:38 Documents

    Its syntax is as follows:

    alias name='string'

    Be wary of the spaces – there are no spaces between name, = and ‘ ‘!

    As you can see, you can create your own commands with alias. More specifically, you are creating an alias for an existing (or a sequence of existing) commands – hence the name.

    Some notes: make sure not to alias an existing command name (check if the command name is used already with the type command). Also, the alias you create will vanish when you exit your Terminal session. That is a topic for another post.

    Hope you learned something useful!

    References

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

  • Linux Tutorial Series – 65 – Executing multiple commands on the same line

    Here is the video version, if you prefer it:

    You can execute multiple commands on the same line. To do that, you need to add a semicolon, like so: (Shotts, 2019)⁠

    command1; command2; command3

    An example:

    cd /; ls; cd

    This lists the contents of the root directory and then switches your working directory back to your home directory.

    Hope you learned something new!

    References

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

  • Linux Tutorial Series – 64 – The which command

    Here is the video version, if you prefer it:

    The which command shows you the location of a given executable. (Shotts, 2019)⁠ For example:

    mislav@mislavovo-racunalo:~/Linux_folder$ which cp

    /usr/bin/cp

    It only works for executables, not for other command types.

    Hope you learned something useful!

    References

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

  • Linux Tutorial Series – 63 – The type command

    Here is the video version, if you prefer it:

    The type command displays the command’s type. (Shotts, 2019) Here is how it is used:

    mislav@mislavovo-racunalo:~/Linux_folder$ type ls

    ls is aliased to `ls --color=auto'

    mislav@mislavovo-racunalo:~/Linux_folder$ type mv

    mv is /usr/bin/mv

    mislav@mislavovo-racunalo:~/Linux_folder$ type cp

    cp is /usr/bin/cp

    mislav@mislavovo-racunalo:~/Linux_folder$ type echo

    echo is a shell builtin

    mislav@mislavovo-racunalo:~/Linux_folder$ type cd

    cd is a shell builtin

    So ls is an alias, mv and cp are executables and echo and cd are shell builtins.

    Hope you learned something new!

    References

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

  • Linux Tutorial Series – 62 – Command types

    Here is the video version, if you prefer it:

    There are 4 types of Linux commands (Shotts, 2019)⁠:

    • executable programs (called executables for short) – programs that are written in a programming language (either a so-called compiled programming language or an interpreted programming language)
    • a command built into the shell itself (shell builtin) (such as echo)
    • shell functions (miniature shell scripts)
    • aliases (commands which we define ourselves which are composed of other commands)

    Why is this useful to know? Well, let’s say that you type in ll into your Terminal and get the following error:

    mislav@mislavovo-racunalo:~/Linux_folder$ ll

    bash: ll: command not found

    Ooops! What just happened? bash tells me it didn’t find the command ll. How could this be? Well, it is because ll is an alias for ls -l, which I can execute:

    mislav@mislavovo-racunalo:~/Linux_folder$ ls -l

    total 20

    -rw-r--r-- 1 mislav mislav 63 Jan 13 05:17 aba.txt

    -rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 aba.txt~

    -rw-r--r-- 1 mislav mislav 12 Jan 13 05:17 ab.txt

    -rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 ab.txt~

    -rw-r--r-- 1 mislav mislav 26 Jan 13 05:18 a.txt

    -rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 a.txt~

    -rw-r--r-- 1 mislav mislav 40 Jan 13 05:18 cb.txt

    -rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 cb.txt~

    -rw-r--r-- 1 mislav mislav 26 Jan 11 22:18 file.txt

    If I hadn’t known ll is an alias, I might have thought I am lacking some executable. Moreover, I can define my own ll alias which shortens my typing session.

    Hope you learned something useful!

    References

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

  • 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/