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
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.
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:
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.
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.