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:
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:
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:
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
This post is about becoming a plumber. Wait, what?! I thought this was a Linux related post. Oh sorry, my mistake. It is. Let’s learn about pipelines (no plumbing needed).
Pipelines are a way to redirect one command’s standard output to the other command’s standard input. (Shotts, 2019) Essentially, you are “chaining commands” here. Here is an example I found myself using most often:
mislav@mislavovo-racunalo:~/Documents$ ls | grep test
test.txt
This is something I most often use. I list out the contents of a directory using ls (or the contents of a file using cat) and then I use grep to find a file or a line that I am interested in.
In the following posts, when I talk about a command, sometimes I may fail to mention that you may need to have superuser privileges for that command. I tried to mention that in every post, but if it happens that you get a Permission denied type of error, you most likely need to prefix the command with sudo.
Just wanted to write a short piece on how to handle errors related to commands on the command line. Here’s how:
When you get an error, Google the relevant keywords and look at the answers. This may seem obvious, but there is probably someone that already had the same (or similar) problem and you will take care of it by Googling.
Let’s say you get a “Permission denied” error when trying to run the rm command. Then you can Google:
“permission denied rm”
or something similar.
In my perspective, the goal is to understand Linux good enough to know what is going on so that you place the things you read on the Internet in their proper context (and not just blindly copy/paste commands and hope it works). When you have the idea how the puzzle pieces fit together, then you can Google for the specific things when the need arises.
Because the computer deals with rather abstract notions of input and output streams (where the streams can be the keyboard (for input), a file (for both input and output), a computer screen (for output)), you can actually redirect the output of some of the commands you run.
Say you run cat on a file and the file is large. Your Terminal window will get very messy with the cat output. Instead, you can redirect the cat’s output like so:
cat largeFile > output.txt
Voila. Now you can look at the cat output at your convenience, as it is stored in output.txt.
I will now cover some stuff in regard to input and output redirection. Let’s dive in:
cat largeFile > output.txt would override a file named output.txt if it already existed. (Shotts, 2019) To append to a file that exists (and not override it), use cat largeFile >> output.txt. By the way, appending means adding at the end of an existing file.
To redirect standard error in the same file as the standard output (standard error and standard output are separated by default), use the &>, like so: cat largeFile &> output.txt; to append, use &>>
You can just redirect standard error somewhere using 2>, as in cat largeFile 2> errors.txt, but I have pretty much never used this.
To make a command read input from a file (and not from a keyboard), you use <; this doesn’t make much sense with cat, but it does with some other commands. Just remember that it is possible to redirect standard input as well.
By the way, if you are wondering why is redirecting standard error equal to writing 2>, that’s because standard input, standard output and standard error have numbers associated with them; for standard error it is 2.
And there you have it. To be completely honest with you, I haven’t used input/output redirection much on the command line, other than logging the output of some program to a file. I have used pipelines much more and you will learn about it in some other post.
Signing off till then, hope you learned something useful!
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
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!
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).