Skip to playerSkip to main contentSkip to footer
  • 5 days ago
In this video we are going to have a lookg at 5 different linux commands. Among them one is meant to analyze processes running in a machine and the other are text processors that can help us issuing commands, transforming text, making changes etc. You may already know these commands, and even in that case, perhaps you don't use them as often it may be useful to you. So come along and learn in this video how to use them or perhaps refresh your knowledge on how to use these particular commmands that are mostly found in Linux Debian versions like the one I use, which is Ubuntu. I hope you enjoy the video and until the next one, be sure to stay tech, keep programming, be kind and have a good one everyone! Cheers!

---

Chapters:

00:00:00 Start
00:00:28 Introduction
00:01:14 fuser - file user
00:07:56 comm - compare
00:14:13 nl - number lines
00:15:49 shuf - shuffle
00:20:09 tac - reverse of cat, which means concatenation
00:21:55 bat - concatenation made better - bat analogous to what bat species can do
00:28:21 End notes
00:29:06 See you in the next video!
00:29:45 End credits
00:30:23 Disclaimer

---

Source code:

- https://github.com/jesperancinha/jeorg-cloud-test-drives

---

Soundtrack:

- https://soundcloud.com/joaoesperancinha/66b2ac4c-76ea-4a15-8409-26098819eadf

---

Related videos:

- https://youtu.be/VbTLF5sgTqs
- https://youtu.be/98ncCDpj1ho
- https://youtu.be/qFdTwFc1ack

---

As a short disclaimer, I'd like to mention that I'm not associated or affiliated with any of the brands eventually shown, displayed, or mentioned in this video.

---

All my work and personal interests are also discoverable on other different sites:

- My Website - https://joaofilipesabinoesperancinha.nl/
- Reddit - https://www.reddit.com/user/jesperancinha
- Credly - https://www.credly.com/users/joao-esperancinha/badges
- Pinterest - https://nl.pinterest.com/jesperancinha/
- Facebook - https://www.facebook.com/joaofisaes/
- Spotify - https://open.spotify.com/user/jlnozkcomrxgsaip7yvffpqqm
- Daily Motion - https://www.dailymotion.com/jofisaes
- Bluesky - https://bsky.app/profile/jesperancinha.bsky.social

---

If you have any questions about this video please put a comment in the comment section below and I will be more than happy to help you or discuss any related topic you'd like to discuss.

If you want to discover more about my open-source work please visit me on GitHub at:

- GitHub - https://github.com/jesperancinha
Transcript
00:00In a previous video,
00:29we had a look at five different Linux commands,
00:32and we tested those in a Ubuntu distribution.
00:36These commands are commands that we can install,
00:39and we have tested,
00:40and they basically should work with Debian distributions,
00:42but also with different Linux distributions as well,
00:45perhaps with different package managers.
00:47We installed our commands back then with apt.
00:51In this video, we are having a look at five different commands,
00:54five different commands that perhaps they are not very well known,
00:58perhaps you don't know them very well,
01:00perhaps you haven't heard of them,
01:01maybe you have, and if you have,
01:03let me know in the comments what you think of them
01:05after I talk about them in this video.
01:07And so the first command we are having a look at
01:10is called fuser or fuser.
01:13And fuser is an interesting command
01:15because it allows us to see which processes,
01:19which process IDs are using a certain file.
01:23It is an interesting command because we can make a distinguishment
01:26between what it is to run a command
01:29and what it is to run a file from that command,
01:31specifically Java and a jar file.
01:34That is where we are going to apply fuser in our command line.
01:38So let's see one example of that right over here.
01:41So if we go to this folder,
01:44this is the root folder that I have,
01:46but if you can try this anywhere in your system,
01:49but I will just choose one particular project that I know
01:52that runs as a jar file for a Java application.
01:56So for example, if I go to the master test drives project that I have,
02:02and then inside of it, I go to the validation company,
02:07then I have already a prebuilt jar that is a runnable jar,
02:13and we can find it in build and then libs.
02:17And then there we should get a devalidation company dot jar.
02:22So we've got this jar file that we want to run.
02:27Of course, it says permission denied
02:28because I tried to run it directly from the command line.
02:30And that of course does not work.
02:32If we now use Java dash jar,
02:37and then run the build libs,
02:43the validation company dot jar,
02:45we should then get our application running.
02:47But what we also want to do is to make sure
02:50that it runs in the background.
02:51So we just add an ampersand at the end like this,
02:54and this will now run it in the background.
02:57It doesn't matter if it's going to influence our command line
03:00because we aren't going to do anything that is going to affect that.
03:04It will just keep the application running in the background
03:07to do some tests with fuser or fuser.
03:11If we use fuser and do dash v,
03:16or let's just start first with just using it directly,
03:19and then go build libs and then the validation company dot jar.
03:24We can see that immediately tells us the location of that jar.
03:27And we see that at the end there is the process ID.
03:33It doesn't seem like a lot of information.
03:36It's only the process ID.
03:38Well, if you are used to using ps, the process snapshot,
03:41you also know that you can use it to find out the process ID
03:44of a certain thing that you are looking for.
03:46But in this case we are sure that we are running the validation company dot jar.
03:50We can also run fuser and then say verbose.
03:58And then we get this extra information.
04:00What is important here is that when we run fuser dash v,
04:04we are getting a lot of information.
04:06And one of the things that will tell us is if the files are being executed,
04:10or if they are simply being opened or anything else.
04:14If you look at the manual of fuser,
04:18we can see that fuser is an identify processes using files or circuits command.
04:25And it says it will display the process IDs of processes using the specified files or file systems.
04:32In the default display mode, each file name is followed by a letter denoting the type of access.
04:37That's what we see there.
04:38If you notice, there's an f.
04:39An f here does not mean current directory because that's with a c.
04:43An e is executable being run.
04:45An f, the one that we saw in our result of the command line, is open file.
04:51F is omitted in default display mode.
04:53And we see other things, file open for writing with uppercase f,
04:58the root directory with r, map it, file or share directory,
05:03and a placeholder omitted in default display mode.
05:08What's important here is that we understand also, let me just quit this,
05:13that in the end, there's this thing here called Java.
05:17And Java tells us something, that this file is being opened by Java.
05:22We've got already the process ID, which is 53995.
05:26But here's the interesting thing that I find about f user.
05:29With f user, you can also see that the Java process is also running.
05:39And to do that, we need to find out which Java have we started.
05:43And of course, we started this Java process from our Java home directory.
05:47So what we can do now is first see where our Java home directory is located.
05:52So if we echo it, we can see that it's located on the SDK manager folder,
05:57and the current candidate folder specifically.
06:01What we can also do is issue future dash V, and then instead of the Java file,
06:10we can then specify the home folder and the location of the Java command.
06:15So we can just say Java home slash bin slash Java.
06:24And if we do this, we will see the detailed information about the process ID.
06:29We can see that it is also 5399 as just here, 53995, 53995.
06:37And we can see that it's being run by Java.
06:40And this is also being run by Java.
06:42But of course, this makes sense because Java is the process that is running it.
06:46But we can see a small difference here.
06:48This one has an F to it, and this one has an E to it.
06:52These attributes tells us that in the first case, the jar file is opened by the Java process.
06:58And the E here tells us that Java is the executable that is running the jar file.
07:05And this also allows us to make a distinguishment between what it is to run an executable jar file
07:11and what it is to run Java.
07:13They both run together.
07:14Java will run our jar file by opening the file and then interpreting what's in that jar file
07:20to run it in our JVM, which is run by this command, Java.
07:26So this is Fuser.
07:28Now, there is another kind of command that can help us doing other things
07:33that is important for our development.
07:35If you are doing development, if you are doing code,
07:38or maybe if you are just doing some kind of readme file or an XML file,
07:43or even a simple text file may be useful.
07:46Maybe we want to compare different versions of different files.
07:49And to do that, there is a very simple command, but very efficient one in Linux, that is called com.
07:55Now com, the command com, comes out of the construction of comparison and two different files.
08:04At least, allegedly, that is as far as I could find, that's where the two M's come from.
08:09So it's C-O-M-M.
08:11And com allows us to compare files.
08:13But it is a command that, when you use it for the first time,
08:16you will see that it doesn't seem to do a lot.
08:19It doesn't seem to be very useful because it's not very clear and it seems very cumbersome.
08:22However, there is a lot of potential for this command.
08:25And that is what I want to show right now.
08:28So let's have a look at that.
08:30com, we can use it in many different files.
08:33I will use it specifically for a readme file.
08:36And specifically for a readme file that I created in this project,
08:39JE or Cloud Test Drives.
08:41The links will be also in the description.
08:43And here I have created one file, top linux commands.
08:47This is the original one.
08:48And I just copied another file to it, which is called top linuxcommands2.md.
08:53And the only difference is the one we see here with IntelliJ, group one and group two.
08:58So there's just one eye as a difference between the files.
09:02So now in the command line, let's go into that folder.
09:05And that one is located in jeorg cloud test drives.
09:14And here we've got the two different files.
09:18We've got the top linuxcommand2, top linuxcommands.
09:25We are going to compare these two.
09:27At least we are going to make tests between the two of them.
09:30Now for this, we need to do com.
09:33And then we can specify top linuxcommands md and then top linuxcommands2 md.
09:43And we get all of this text.
09:45And it seems very cumbersome.
09:47And it's difficult to interpret.
09:48So let's, first, before we start reading what's in here, let's check the manual and
09:53see what it says.
09:54If you go to man.com, we've got here that it is a compare two sorted files line by line
10:01command.
10:03But it is an interesting one because it doesn't show this in a human readable form.
10:08It is weird because it shows in the first column, the lines that are unique for line for file
10:14one.
10:15And the second column, the lines that are unique for file two.
10:17And then finally, the lines that are common for the two files.
10:22So what happens here is that what we see in our command line is something like that, but
10:27it's difficult to read because we've got all of this text here and it seems to be mixed.
10:32And further, it says file one is not in sorted order and the other is also not in sorted
10:37order.
10:39So what's important here is that we understand that the com command works on the basis of
10:44comparison line by line.
10:46And therefore, it is important to have the files sorted in a way.
10:51And when we think about that, we may think that, oh, this may not be very helpful, but
10:56it is.
10:57Let's have a look at why do I say that.
11:00The thing is, if we use this com just like this, perhaps it's difficult to interpret,
11:05it's different to parse, it's different to understand what's going on there.
11:08But here's an interesting bit.
11:10If we do dash and we do one, two, three, for example, what this will do, it will ignore
11:17all the different columns.
11:18And so we are not going to get anything except that our files are not in the sorted order
11:23and that the input is not in the sorted order.
11:26We don't get anything.
11:27But what this one, two, three means is that with one, we are ignoring column one.
11:32With two, we are ignoring column two.
11:34And with three, we are ignoring column three.
11:38That means the differences in file one, the differences in file two, and the common lines
11:43in file three.
11:45If we simply run, for example, two, three, if we are ignoring columns two and three, we will
11:55only get column one.
11:57And look at that.
11:58It says group one, only group one.
12:00Now we know from the common line that there is a difference and that file one difference
12:05from file two with this line group one.
12:10That is the difference between the two files.
12:12And the whole line is shown.
12:14Not the specific difference, but the whole line.
12:16But we still get the file one is not in sorted order and file two is not in sorted order.
12:21What we can do, if we go to the manual, we can see that there is an option, no check order.
12:27We can also go here and say, let's not check the order.
12:37So let's go, no check order.
12:40And if we do that, we get the specific line, which we now can use to tell some process, something
12:48that we are doing with the shell script that we found one line that is different in which
12:53line that is.
12:54We can also do that for the second file.
12:57Where does the second file differs?
12:58Then we are ignoring column one and column three.
13:02And if we want to check where are they exactly the same, then we just ignore columns one and
13:08two.
13:09And we get the full similarity without the specific line.
13:14So if we go back to our IntelliJ, we can see that we've got a line above, which is called commands
13:19are listed.
13:20And under the line difference, we see TLDR.
13:25And if you go here, we see that exactly that the line that is different is not there.
13:33Now there are multiple users to use this command, but we may want to, for example, see the lines
13:40that are attributed to the differences.
13:45What we can also do here is yet another thing.
13:48It's a sort of a trick that we can use combined with com.
13:52And that is a simple one.
13:54That's just saying, hey, I want to see the differences in file one in regards to file
14:00two, but I also want to see the line number.
14:03So what we can do is simply this.
14:05There's another command called number lines and the number of lines command can give us
14:14the number of lines per file.
14:16So this means that instead of using just the content of top Linux commands in D, we are going
14:21to prepend each line of the content of that file with the line number.
14:25So if we use that in this one over here as well, then we can say that we are reading both files
14:36with the line numbers prepended to each one of these lines.
14:39BA in this case means B is the body of the file, the whole body.
14:43And A means all the lines, including white spaces.
14:48So if you run this now, we've got that on line five of file one, there is a difference
14:54in regards to file two that contains the text hash hash hash hash space group space one.
15:04So this is com.
15:05Com is a very interesting command for comparing files.
15:08If you want to make shell scripts, it is very interesting.
15:12It is not as handy as using things like the file comparer in an IDE, like for example,
15:17what we just saw on IntelliJ, but it can be very useful if you want to make automation,
15:21which is also a part of making development and creating systems that do things automatically
15:26for us.
15:27The third command that I want to show you is a command that I'm not entirely sure what
15:32applications it could have, but it's interesting to know.
15:35We may think of it as something to be used perhaps in a game or perhaps where we want to
15:41generate random content with some kind of text source.
15:46It's called shuff and shuff comes from shuffling, of course, and shuff means that we are going
15:53to use this command to shuffle lines of that file and shuffling lines of that file can be
16:01used in many different ways.
16:02There are different switches for this command, but it is an interesting one to have a look at
16:07because it may be worth knowing that it exists.
16:12And we may want to use something like that in automation.
16:15So let's go back to the place where we were in J or cloud test drives.
16:21And there, let's just first make a list and do cat of top Linux commands.
16:31By the way, cat means concatenation, if you don't know that.
16:36Now, we've got the whole text of the file.
16:39Now, if we apply shuff to this file, we can see that all lines have been shuffled.
16:53No line or text has been changed.
16:56They have all changed location.
16:58So they are now in different places in our file.
17:01It's also interesting if we run this with NL.
17:05So if we run this with the number lines command, and then we see the result of it,
17:14we can see that it shuffles the line numbers as well.
17:19Then we can see that all the text has been changed and the lines have changed order in the result.
17:25But we're going to do other things with shuff.
17:27So if you look at the manual, man, shuff, we can see that it's got different options.
17:34It's got echo.
17:35It's got input range.
17:36It's got a head count.
17:38It's got the output file.
17:39If you want to save this to a file, the result.
17:42If we want to get some random source bytes from another file.
17:48If you want to repeat the lines, this is an interesting one.
17:52If we just use dash R to our text, it's going to keep generating random lines forever.
18:02It doesn't stop.
18:03I don't know where would this be useful.
18:05Perhaps if we are making tests, for example, and we want an endless source of random text,
18:10this could be useful for that.
18:12So this could be useful in automating tests, for example.
18:17Not for this specific test file, but if we are making some kind of text based and text processing program,
18:27then perhaps this could be useful to automate tests.
18:31Another one that we've got here is the head count.
18:35And head count has also an interest to use.
18:38We are going to try just that one before we move on to the next command, the fourth command.
18:43If we go over here and say head count, just add head count, and then let's say four.
18:55It will just print out the four first lines of whatever we configure the command to be.
19:02In this case, we, we said that we want to repeat.
19:05So perhaps if we, yeah, this is one example.
19:08So it keeps repeating lines randomly.
19:11And now we've got in the first four lines that have been generated two equal lines,
19:16because it will pick them randomly to generate an output that we can then put out to a file.
19:21Let's try that one as well.
19:23We have seen that that was, I think, output file.
19:28File, no, let's check again, shove dash dash o.
19:35So that's an interesting one.
19:37Go dash dash o, or dash o.
19:41Then we can say output dot text.
19:45Put dot text.
19:48If we do that, and we do a cat output text,
19:53then we see that that is the result of our generated text.
19:58All right, so the fourth command on the list is another weird one,
20:03and it's also probably not very well known.
20:05It's called tac.
20:07Think about it.
20:08Tac.
20:09What does tac sound like?
20:11T-A-C.
20:13If you guessed cat, you're right.
20:17It's the reverse of cat.
20:19It's the reverse of concatenation.
20:21Now let's think about it.
20:23What does the reverse of concatenation could possibly do?
20:27Perhaps what you just thought.
20:29Perhaps not.
20:30But if you thought that it is reverting the text,
20:34it is reverting the text.
20:35That's exactly what it does.
20:36We can see the context of a file,
20:38or concatenate context of a file,
20:40in a reversed order.
20:42How does that reverse order occurs?
20:44That reverse order occurs per line.
20:47So that means that we will get the text reversed,
20:49not the reversal of each individual line,
20:52but the reversal of the text.
20:55So let's see what happens when we run tac in these experimental files.
21:01If we now run tac on the top
21:10Linux commands file,
21:12the original one,
21:14we see that it's completely reversed in terms of lines.
21:18So it starts out with the last line,
21:20with the button to go back,
21:22and then it shows the different lines of the different commands for group one.
21:28By the way, this is group two, this video.
21:31And it finally presents group one,
21:33where we saw the difference between the first file and second file,
21:36and then all the commands listed are used in Linux, blah, blah, blah.
21:39And then at the end, we've got the title of the readme file.
21:43So this is tac, tac cat.
21:47And on that note,
21:49the last command we are looking at in this video is called bat, B-A-T.
21:56Bat in Debian distributions is called batcat.
22:01And batcat, I tried to find out where the,
22:05does the name batcat comes from.
22:07Apparently it comes from bat, some kind of analogy to a bat, to an actual bat,
22:13because a bat has very different systems of echolocation.
22:17They can detect things, they can detect sounds,
22:20they have an ability to visualize in a different way.
22:24And they are usually very, very sharp animals in that sense.
22:28They can hunt very well.
22:30And the analogy is made because batcat allows us to visualize text,
22:36but not in a usual way, not in the way that we are used to.
22:40And for developers and programmers,
22:41this is one of the best things that we could use in the command line
22:45without the use of an IDE.
22:47Let me explain that.
22:49Batcat allows us to visualize text with highlights.
22:52So let's have a look at that.
22:54If I go now out of this project,
22:56and I go to, for example, a very simple project,
22:59this one, which is at this moment,
23:02probably this is a project that I should move to a module inside of Kotlin development,
23:09or example project.
23:11But it's still in a separate project.
23:13If we look at it from the source files,
23:16if we go to the source, main Kotlin,
23:18and then here we find json2builder.kt.
23:22This is a Kotlin file.
23:23If we want to see the contents of this file in a very easy way from the command line,
23:28we normally just do cat.
23:31And that's usually something that we don't want to do even in the command line.
23:35We always want to use an IDE to read code files, source code files.
23:39Then, of course, we are limited to our command line.
23:42And so sometimes we wish there was something else that we could use,
23:46instead of just a cat, for example.
23:48Or if you want to use that vim,
23:52you could maybe use that and try to see something there.
23:55There are ways to use vim with highlights.
23:58But that's not the one, that's not an ideal situation.
24:05We can use batcat for these cases.
24:08If we use batcat,
24:12the most interesting thing that I find about this command
24:15is what we are going to see right now.
24:17If I run this, we will see the whole file,
24:21not only highlighted, but also with the lines of code.
24:26And we can read this file just as we would read it in an IntelliJ,
24:30or in an Eclipse, or in a NetBeans, or whatever.
24:34We can read it just like we read in those IDEs.
24:39Those highly sophisticated IDEs look pretty much like this,
24:43except that they sit in a graphic user interface.
24:46But further than that, it's just this.
24:50Now, of course, we don't have IntelliSense.
24:52And of course, we don't have auto complete.
24:54And of course, we don't have options.
24:55We don't have refactoring.
24:56But if we get limited to this situation,
24:59we can still see the code in a highlighted way,
25:02which will definitely improve the way we read code with this command.
25:06And we can use it with other different things.
25:07If I go here to this folder,
25:09I'm pretty sure I've got other different files.
25:11If I go to the root, for example, make file, do we have a make file?
25:15Yes, we do.
25:16So if we just say bad cat, and then make file,
25:21we see everything highlighted.
25:22And we also have the benefit of using more.
25:25Look at that, we can just go up and down the file,
25:28and we can just read it.
25:29And we can perform searches.
25:32Let's search for depth, for example.
25:34And then it goes and it highlights all the depth that we want to see.
25:37And we can go up and down the file and just see anything we want to see
25:42and just read it.
25:43Because we can notice already that this is highlighted.
25:47This is one of the commands that we can run via a make file.
25:50And we've got an export which is highlighted with this pink purple color.
25:57These are definitely in purple, which are variables.
26:00For example, this switch dash s.
26:04Everything is highlighted in a way that we can immediately read the file.
26:09And it is much easier to read.
26:13Bad cat also provides support to other different files.
26:16If we go bad cat, for example, settings.
26:20This is a properties JSON based file.
26:24And it does show that as show the first property JSON to build a pattern.
26:29That's the name of the project.
26:30It doesn't matter what it does at this point.
26:33We can also see, for example, why not the Gradle build file?
26:36Let's see what it highlights with the build Gradle file.
26:39Let's see build Gradle.
26:40And then we can look at it.
26:42And it's probably not the best highlighting ever.
26:45But we do see that it has highlighted the group name, the version name, the set.
26:52So the method name, it has also highlighted.
26:55We've got these dots all with a different color, with a red color.
26:59And what further can we see here?
27:01If we go further down, we can see that the JVM tool chain is also highlighted.
27:08So in this case, we are using the JVM 21.
27:11And further, there isn't much more.
27:16Let's have a look if we can find another file that we perhaps find it also interesting.
27:22For example, a bat file.
27:24The bat file, which is a batch file that we can use in Windows, not in Linux.
27:28But let's try and see if it will work well with a bat file.
27:35Gradle w.bat.
27:41And it works perfectly.
27:43We can see that we've got all of these REM lines.
27:45This is just basically comments in batch.
27:48And we can see the for loops.
27:50We can see the echoes.
27:52Everything here seems to be correctly highlighted,
27:55or at least in a way that we can easily read what's in there.
27:58Of course, these are automated files generated by Gradle.
28:01But we can still try to read what it does.
28:04And we can see very well that, for example, here, Windows NT.
28:10We've got here the switch D or Gradle app name.
28:15So we've got everything pretty nicely highlighted.
28:18And we can use this from the command line.
28:21So we had a look at fuser, at com, with number lines.
28:26That's two in one command.
28:28Then we had a look at shove.
28:31Then we had a look at cat.
28:33And then finally, we had a look at bat cat.
28:36So we actually had a look at five different processors and visualizers.
28:40And one single process analyzer and inspector.
28:44All right, everyone.
28:46So these were five commands that I wanted to show you.
28:48Actually, we had a look at six.
28:51Only two of them were combined in one.
28:53And I hope these five different commands were useful for you.
28:57And I hope that you now feel that you have more tools available to you.
29:01And that you eventually remember these commands to use you when you actually need them.
29:05If you enjoyed this video, please make sure to give us one big like.
29:10Don't forget to subscribe to this channel to learn more about software development and systems and anything related to IT.
29:17Make sure to read the description.
29:20I will put there the sources.
29:22And as usual, we'll put all the important information about this video and the topics that were discussed in it.
29:29And of course, I would love to hear your comments.
29:32So, until the next video, make sure to stay tech, keep programming, be kind, and have a good one.
29:43Bye.
29:44Bye.
29:45Bye.
29:46Bye.
29:47Bye.
29:48Bye.
29:49Bye.
29:50Bye.
29:51Bye.
29:52Bye.
29:53Bye.
29:54Bye.
29:55Bye.
29:56Bye.
29:57Bye.
29:58Bye.
29:59Bye.
30:00Bye.
30:01Bye.
30:02Bye.
30:03Bye.
30:04Bye.
30:05Bye.
30:06Bye.
30:07Bye.
30:08Bye.
30:09Bye.
30:10Bye.
30:11Bye.
30:12Bye.
30:13As a short disclaimer, I'd like to mention that I'm not associated or affiliated with
30:27any of the brands eventually shown, displayed or mentioned in this video.

Recommended