6 Simple Linux Utilities You Didn’t Know You Needed
Let’s take a look at some fantastic examples of Linux programs exemplary of this pragmatic, utilitarian world.
1. sum
About as simple as it gets. There are only four accepted parameters to this command, one being the --help
flag. The sum
utility will produce a checksum and block count for the specified input file.
Let’s make a simple text file with the following content:
line 1 line 2 line 3
And run it through the sum
utility:
> sum file.txt 22592 1
On the left is the checksum for the file and the right is the number of blocks it occupies in the filesystem. Brutally simple.
This is useful for performing a quick differential between files or checking to see if a file has been changed since the last checksum.
Also check out the cksum
utility to perform a checksum and count the bytes instead of blocks.
2. mktemp
This utility allows you to create temporary files. Sometimes you need to store a bit of information in a file until a process has completed. In order to do this quickly and safely you can simply use the mktemp
utility.
Rather than setup file creation in the /tmp
directory manually, using mktemp
you get everything done for you in one simple command. This prevents you from having to check if the file exists already or select a unique filename.
Simply running mktemp
will produce the name and path of your new temp file:
/tmp/tmp.xWrpgjoJgk
Now when you need to store some data in a temp file you won’t have to reinvent the wheel. Let Linux do it for you.
3. namei
Path diving on Linux can become quite complex. Especially if you’re following a handful of symlinks around. The namei
utility will show you the hierarchy of a single path and the twists and turns it takes to get to the end of it.
For example, let’s say we have a directory with a test file inside it:
/tmp/real_dir/test_file
We’ll create a symlink to this directory called symlink_dir
and then use namei
to determine the path to the test file via the symlink:
f: /tmp/symlink_dir/test_file
d /
d tmp
l symlink_dir -> /tmp/real_dir
d /
d tmp
d real_dir
- test_file
As you can see, namei
shows us the full path we take to end up at the test_file
inside of real_dir
. This includes traversing the symlink_dir
and pointing back at real_dir
.
Rather than use something like ls
to tediously dig into each directory or potential symlink in your path you can use namei
and get a clear picture immediately.
4. sg
Whether you know this command already or not, the incredible usefulness of it is huge. The sg
utility allows you to switch groups and execute a command as that group. For example, if you wanted to execute something as the admin
group but your user isn’t a member of it. To do this you could use:
sg admin 'ls -lah'
This will execute ls -lah
as the admin
group and then immediately return you to your original group membership.
The sg
command is great for things like testing overall group permissions or running scripts with different privileges.
5. last
Security is paramount on any Linux machine, especially if its located in the cloud. If you think someone may have logged into a particular host and performed some malicious activity or errant command, the last
utility can help.
tate pts/0 1.2.3.4 Wed Jan 26 01:17 still logged in
tate pts/0 1.2.3.4 Wed Jan 26 01:17 still logged in
tate pts/0 4.5.6.7 Wed Jan 26 01:04 - 01:04 (00:00)
tate pts/0 4.5.6.7 Wed Jan 26 01:01 - 01:02 (00:01)
tate pts/0 4.5.6.7 Tue Jan 25 20:55 - 00:00 (03:05)
Using last
you can see the login activity of all users and key information like their remote IP, login time and current status.
Also note how last
shows you the duration of each session. If there is a high number of extremely short duration sessions, it may mean some script or automated system is logging in and out of your system. This may help you track down any suspicious or unwanted activity occurring on your system.
6. tac
Everyone knows the cat
command. It is one of the first commands other than ls
that you probably used on Linux. But do you know the cat
command’s evil twin?
The tac
command (which is literally “cat” spelled backwards) does almost the exact same thing as cat
. It still displays the contents of a file, but now it does it backwards.
For example, let’s say I had a file with the following in it:
line 1
line 2
line 3
line 4
line 5
If I cat
this file, the usual output appears. If I tac
the file I get this:
line 5
line 4
line 3
line 2
line 1
Sure, this may seem silly at first, but imagine you had a rather large log file and wanted to display its contents starting from the most recent entry. Using tac
you can easily feed the output of such a log file into either your console or another application for processing.