I think I mentioned this before when I covered the ls command, but it bears repeating:
Dot files are the files whose name begin with a dot. They are called hidden files. They can be displayed in a directory listing using ls, but only by providing the -a option. I never had to meddle with dot files in my Linux dealings so far. I did have to look at some dot files in my programming projects, but that is a different story.
Let’s say you want to search for a particular command in your command line history. You could write history | grep pattern, but there is a better way. Reverse incremental search to the rescue!
Here is how to use reverse incremental search: (Shotts, 2019)
When in a Terminal window, press ^ + R (CTRL + R) to initiate it. Start typing in the command you are looking for in history. As you type in more characters of the command, the search will refine itself. If you want to execute the command currently displayed, press ENTER. If you want to copy the command to your current terminal session, press ^ + J (CTRL + J).
It should look something like this:
(reverse-i-search)`sudo': history | grep sudo
This is something I rarely use (I use the Up/Down arrows), but something that I have in the back of my head should I need it. I have also seen this being used by other Linux users in their day-to-day, so I thought that this was important to cover.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 110-112
Let’s talk about history. No, not about a high school history class or about your browsing history (that could spark interesting conversations for sure). I’m talking about the history command.
The history command is used to display your command execution history. (Shotts, 2019) Example usage:
mislav@mislavovo-racunalo:~/Linux_folder$ history | grep sudo
1075 sudo apt install nodejs npm
1081 sudo npm install npm@latest -g
...
Here, I look for all of the sudo prefixed commands that I have executed. It is important to note that history doesn’t go on till the beginning of your bash usage – it is limited, although you can change how much history entries you keep. Keep that in mind.
Your history is stored in a file called .bash_history in your home directory (the first dot in the filename indicates it is a hidden file).
I didn’t use history as much, but I was aware of its existence. I have found myself looking for the commands I ran by using the Up/Down arrows on the keyboard. history seems to be a better choice for when the command was used in the not-so-recent past.
Carrying on, we will talk about history and about variables. Understanding what variables are and how to manipulate them is essential. Understanding what important variables entail is also essential. Do make sure to pay attention.
We talked about command types, which there are 4 – executables, shell built-ins, shell functions and aliases, as well as the commands used to determine the type of a particular command. We also talked about finding files – both locate and find – and (hopefully) memorized the syntax find dir -name file -print.
We also covered some other stuff, but the above ones are the most important.
There are some commands I never used in my day-to-day Linux experience – such as cut, sed, awk, sort and uniq – among other things. I did use them once, when I was taking a class which had some Linux command line laboratory exercises, but that was it. Since I haven’t used these commands at all, I shouldn’t talk about them. If you ever have the need to learn what these commands do, I think that a Google search with the command and the keyword “tutorial” will do the trick.
As I am writing this, I feel bad for leaving out a bunch of commands, but the point of this Linux post series is to trim the fat – to give you the things you need for everyday use, not cover everything possible.
I may me mistaken, but I think that it is a valid strategy to learn to use these tools as the need arises. I found myself manipulating text in programming languages like Python and C++ and not as much sed and awk. Those are my 2 cents.
Let’s talk about the xargs command. The xargs command is used to accept input from the standard input and then converting it into an argument list for a specified command. (Shotts, 2019)
-rw-r--r-- 1 mislav mislav 12 Feb 5 22:48 /home/mislav/Linux_folder/ab2.txt
-rw-r--r-- 1 mislav mislav 63 Jan 13 05:17 /home/mislav/Linux_folder/aba.txt
-rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 /home/mislav/Linux_folder/aba.txt~
-rw-r--r-- 1 mislav mislav 12 Jan 13 05:17 /home/mislav/Linux_folder/ab.txt
-rw-r--r-- 1 mislav mislav 0 Jan 11 23:00 /home/mislav/Linux_folder/ab.txt~
Here I use find and grep to find files starting with ab in my Linux_folder. Then I use xargs to use those results and pass it to ls -l so that I get the long listing of each of the resulting files.
I have almost never had the need to use xargs. I only used it once or twice as a part of the “use Google to find a solution to your problem”. In that way, it is immensely useful to know what xargs does.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 252-253
The find command is used to find files. (Ward, 2014) There are a lot of options to find, but I remember it simply as this:
find dir -name file -print
where dir is a directory from where to start the search and file is the file name I’m looking for. To elaborate more on the dir part – find will find files in the directory dir or a subdirectory of dir, but it will not go upwards of dir. For example, if my find starts like this:
find /home/mislav …
it will not look outside the mislav folder, just “further down”. That means it is impossible for me to get a result such as /home/jellyfish/… .
-name is followed by the name of the file you are looking for. -print is used to instruct find to print the full file name on the standard output, followed by a newline. (“FIND(1),” n.d.)
Some examples of find:
mislav@mislavovo-racunalo:~/Linux_folder$ find /home -name ab -print
I always used find to find a particular file (such as some files I needed which relate to software development), so I never needed to use other find functionalities (and there are many). I found a good guide for functionalities of find I didn’t cover here: (“A find Tutorial and Primer,” n.d.), although again, other than memorizing this short line at the beginning of this post and using it, I never used anything else besides that.
“What’s the difference between locate and find?” you may ask. Well, the astute reader (and I hate when someone writes this as I always feel bad) will have noticed that locate operates on a database. So, if some file entry wasn’t added to the database locate operates on, locate won’t find it, but find will. So find is more powerful than locate in the sense that it doesn’t rely on a database which can be incomplete at a given moment. I have used find much more often than locate.
Today we are going to talk about locating files. This is very useful and I use it relatively frequently myself.
The locate command is used to locate files. (Shotts, 2019) It is used as such:
locate string
where string is a part of the pathname you want to locate. To remind ourselves, a pathname is a location on your disk, such as /home/mislav/Linux_folder, so you are asking your computer “give me all the pathnames that contain string within themselves” (string being an array of characters).
Here, all paths that contain Linux_folder within them are located.
Note: You may not have locate installed as a command (I know I haven’t). You can install the locate command by following an answer provided here: (“How to install the locate command?,” n.d.)
locate uses a database to find files. What that means is that pathnames are stored in a database and then that database is searched for pathnames matching string.
There also exists a command called find, which you can also use to find files, but we will talk about find in another article.
The diff command is used to see the difference between two files. (Shotts, 2019) I don’t think you will be using this command much if you are not a software developer, but if you are a software developer, you could use it to look at the difference of two program source files, or to compare two files which were outputted by different programs.
The output seems a bit confusing. What is this 1,5c1,4? In my opinion, you don’t have to know what that means. What you need to know is that when you encounter a <, that means that the line that follows is missing from the second file (in this case ab.txt) and when you encounter a >, that means that that line is missing from the first file (in this case aba.txt). (“Understanding of diff output,” n.d.)
Again, as I stated, when you encounter a < that means that the line that follows is missing from the second file (in this case ab.txt) and when you encounter a > that means that that line is missing from the first file (in this case ab2.txt). They only differ in the second line, and the second file is missing Ab, while the first file is missing aB.
If you really want to know what 2c2 means, I refer you to the first answer in (“Understanding of diff output,” n.d.). I agree with the second answer in the reference, which is that you don’t need to know what this means. You will most likely be using diff rarely and when you do, you will be able to see the conflicting lines and that’s all you need to know. That is, in my experience, enough for practical purposes.
There are 2 options for diff that are useful: the -c option (also known as the context format) and the -u option (aka the unified format). Both alter the output of diff. Here is an example of diff -c:
Here is the meaning of the output: First of all, the *** denote the first file, while --- denote the second file. That means that *** 1,4 **** means lines 1 through 4 of the first file, while --- 1,4 ---- means lines 1 through 4 of the second file. Now for the actual file contents: a – in front of a line means that a line appears in the first file, but not the second file. A + means that a line appears in the second file, but not in the first file. An ! means that a line (or lines) changed between the first and the second file. (Shotts, 2019)
A – indicates that a line was removed from the first file and a + indicates that a line was added to the first file. This means that if we were to remove the lines with the minuses and add the lines with the pluses, we would get the contents of the second file.
That’s pretty much it. I think I have covered all you need. You won’t be using diff as much if you’re not a software developer anyway, and if you are a software developer, this is all you need to know in my opinion.
In case you are really curious: the numbers 1,5c1,4 in the first example are instructions for the patch command. Since I never used the patch command, I won’t cover it. If you ever need to use it, you know it exists and Google is your friend.
Hope you learned something new!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 319-321