Pet – A Command Line Snippet Manager for Linux

If you manage a lot of Linux systems via command line, you must be aware of the hectic task to remember all the commands you use on daily basis. You often need to go into your system’s history and find the required commands from there. Pet is a command line based tool which makes a system administrator’s life easy. It is an easy to use snippet manager, which saves your command snippets and you can easily view and use them when needed. Command line junkies find this tool extremely important, it is completely a freeware script, the setup process is pretty simple and it runs on almost all flavors of Linux operating system as well as Mac OS. It is built using the well known Go programming language and requires some python modules like Peco and brew to work properly. It is written by Teppei Fukuda and its source code is available on Github. In this tutorial, we will discuss its prominent features, installation process, and some usage example. Stay with us, it’s going to be an interesting session.

Important Features of Pet

Here are some of the noteworthy features of this utility, it can:

  • register your new command snippets pretty quickly.
  • search through the local repository of command snippets.
  • easily edit and run command snippets.
  • use Gist (GitHub-based online code repository) to sync your snippets.

Feeling excited? Let’s get started on its installation and configuration process now.

Installing and Using Pet

We will be demonstrating the installation and usage process of Pet on Ubuntu 16.10 system, the same set of instructions should work for any older version of Ubuntu and Debian based systems. As mentioned in the introductory paragraph, it requires Peco and brew to work properly. Run following two commands respectively to install Brew and Peco on your Linux system.

sudo apt-get install linuxbrew-wrapper
sudo brew install peco

Once the pre-requisites have been properly installed, run following command to download the source file for Pet.

sudo wget

Depending on your network speed, it should take a couple of minutes to complete the download process. Run the following mentioned command to unzip the downloaded file.


Once the unzip process is complete, you should be able to see an executable script, named “pet” here. Run the following command to copy this script file to proper location so it may be available for use for all users.

sudo cp pet /usr/local/bin

There you go, Pet has been configured successfully now. Run “pet” command on the terminal to verify that it is working properly. Expected output should be as depicted in the following screenshot.


Let’s demonstrate some examples, in order to add the new snippets, use the following command:

pet new

It will prompt you the two details, “Command” and “Description”. I have following command I often use on my Linux web servers to identify if my server is under DDOS attack or not.

netstat -nap | grep \:80\  | awk ‘{print $5}’ | tr “:” ” ” | awk ‘{print $1}’ | sort | uniq -c | sort -n

Below screenshot will clarify how I added this snippet to Pet.

pet add

Similarly, you can use the following command to list the currently added snippets.

pet list

You can search through added snippets using the following command:

pet search

You can easily register the previously executed commands to the pet database by edit your .zshrc file. Use vi or vim editor to open the .zshrc file and append the following lines there:

function prev() {
PREV=$(fc -lrn | head -n 1)
sh -c “pet new `printf %q “$PREV”`”

Now you don’t need to add each command manually to Pet, your system will automatically add all the commands you use to the snippet manager. Your daily work life is lot easy now 🙂

Sync Snippets to Gist

As mentioned in the feature list, you can sync your snippets with online Gist repository, for this purpose you must have a Github account, once you have GitHub account, you can get a GIST Token easily. Use the following command to sync your snippets to Gist, it will prompt for Token ID and you should be good to go.

 pet sync -u

Here is the sample output of this command:

 pet sync -u
Gist ID: 1P93IUdf4e06d117097en976BHY
Upload success

Similarly, you can download your snippets from Gist to your system using the following command. It is pretty useful when you want to migrate your snippet data to any other system.

pet sync

Hope you enjoyed this article, we have discussed all possible usages of Pet utility here. It is an extremely lightweight, easy to use, and stable utility which is a must-have application for any command line Linux lover. It is under continuous development and we hope to see many improvements to this application in near future. If you have any questions or feedback, fee free to let us know in the comments section of this article.

What Is The Shell?

When we speak of the command line, we are really referring to the shell. The shell is a program that takes keyboard commands and passes them to the operating system to carry out. Almost all Linux distributions supply a shell program from the GNU Project called bash. The name “bash” is an acronym for “Bourne Again SHell”, a reference to the fact bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

Terminal Emulators When using a graphical user interface, we need another program called a terminal emulator to interact with the shell. If we look through our desktop menus, we will probably find one. KDE uses konsole and GNOME uses gnome-terminal, though it’s likely called simply “terminal” on our menu. There are a number of other terminal emulators available for Linux, but they all basically do the same thing; give us access to the shell. You will probably develop a preference for one or another based on the number of bells and whistles it has. Your First Keystrokes So let’s get started. Launch the terminal emulator! Once it comes up, we should see something like this:

[me@linuxbox ~]$

This is called a shell prompt and it will appear whenever the shell is ready to accept input. While it may vary in appearance somewhat depending on the distribution, it will usually include your username@machinename, followed by the current working directory (more about that in a little bit) and a dollar sign. If the last character of the prompt is a pound sign (“#”) rather than a dollar sign, the terminal session has superuser privileges. This means either we are logged in as the root user or we selected a terminal emulator that provides superuser (administrative) privileges. Assuming that things are good so far, let’s try some typing. Enter some gibberish at the prompt like so:

[me@linuxbox ~]$ kaekfjaeifj

Since this command makes no sense, the shell will tell us so and give us another chance: bash: kaekfjaeifj: command not found

[me@linuxbox ~]$

Command History If we press the up-arrow key, we will see that the previous command “kaekfjaeifj” reappears after the prompt. This is called command history. Most Linux distributions remember the last 1000 commands by default. Press the down-arrow key and the previous command disappears.

Cursor Movement Recall the previous command with the up-arrow key again. Now try the left and right-arrow keys. See how we can position the cursor anywhere on the command line? This makes editing commands easy.

A Few Words About Mice And Focus: While the shell is all about the keyboard, you can also use a mouse with your terminal emulator. There is a mechanism built into the X Window System (the underlying engine that makes the GUI go) that supports a quick copy and paste technique. If you highlight some text by holding down the left mouse button and dragging the mouse over it (or double clicking on a word), it is copied into a buffer maintained by X. Pressing the middle mouse button will cause the text to be pasted at the cursor location. Try it. Note: Don’t be tempted to use Ctrl-c and Ctrl-v to perform copy and paste inside a terminal window. They don’t work. These control codes have different meanings to the shell and were assigned many years before Microsoft Windows.

Your graphical desktop environment (most likely KDE or GNOME), in an effort to behave like Windows, probably has its focus policy set to “click to focus.” This means for a window to get focus (become active) you need to click on it. This is contrary to the traditional X behavior of “focus follows mouse” which means that a window gets focus just by passing the mouse over it. The window will not come to the foreground until you click on it but it will be able to receive input. Setting the focus policy to “focus follows mouse” will make the copy and paste technique even more useful. Give it a try if you can (some desktop environments such as Ubuntu’s Unity no longer support it). I think if you give it a chance you will prefer it. You will find this setting in the configuration program for your window manager.

Try Some Simple Commands Now that we have learned to type, let’s try a few simple commands. The first one is date. This command displays the current time and date.

[compose@amp ~]$ date
Sun Mar 19 04:10:43 UTC 2017

A related command is cal which, by default, displays a calendar of the current month.

2017-03-19 10_18_51- - Remote Desktop Connection.png

To see the current amount of free space on your disk drives, enter df:

2017-03-19 10_21_28- - Remote Desktop Connection.png

Likewise, to display the amount of free memory, enter the free command.

2017-03-19 10_22_16- - Remote Desktop Connection.png

Ending A Terminal Session We can end a terminal session by either closing the terminal emulator window, or by entering the exit command at the shell prompt:

[me@linuxbox ~]$ exit

The Console Behind The Curtain Even if we have no terminal emulator running, several terminal sessions continue to run behind the graphical desktop. Called virtual terminals or virtual consoles, these sessions can be accessed on most Linux distributions by pressing CtrlAlt-F1 through Ctrl-Alt-F6. When a session is accessed, it presents a login prompt into which we can enter our username and password. To switch from one virtual console to another, press Alt and F1-F6. To return to the graphical desktop, press Alt-F7.

Summing Up As we begin our journey, we are introduced to the shell and see the command line for the first time and learn how to start and end a terminal session. We also see how to issue some simple commands and perform a little light command line editing. That wasn’t so scary was it?

Compatibility with Older Systems RHEL 7

If an ACL has been set on any file on a given file system, that file system has the ext_attr attribute. This attribute can be seen using the following command:
# tune2fs -l filesystem-device
A file system that has acquired the ext_attr attribute can be mounted with older kernels, but those kernels do not enforce any ACLs which have been set.
Versions of the e2fsck utility included in version 1.22 and higher of the e2fsprogs package (including the versions in Red Hat Enterprise Linux 2.1 and 4) can check a file system with the ext_attr attribute. Older versions refuse to check it.

Archiving File Systems with Acls RHEL 7

By default, the dump command now preserves ACLs during a backup operation. When archiving a file or file system with tar, use the --acls option to preserve ACLs. Similarly, when using cp to copy files with ACLs, include the --preserve=mode option to ensure that ACLs are copied across too. In addition, the -a option (equivalent to -dR --preserve=all) of cp also preserves ACLs during a backup along with other information such as timestamps, SELinux contexts, and the like. For more information about dump, tar, or cp, refer to their respective man pages.
The star utility is similar to the tar utility in that it can be used to generate archives of files; however, some of its options are different. Refer to Table 4.1, “Command Line Options for star” for a listing of more commonly used options. For all available options, refer to man star. The star package is required to use this utility.

Table 4.1. Command Line Options for star

Option Description
-c Creates an archive file.
-n Do not extract the files; use in conjunction with -x to show what extracting the files does.
-r Replaces files in the archive. The files are written to the end of the archive file, replacing any files with the same path and file name.
-t Displays the contents of the archive file.
-u Updates the archive file. The files are written to the end of the archive if they do not exist in the archive, or if the files are newer than the files of the same name in the archive. This option only works if the archive is a file or an unblocked tape that may backspace.
-x Extracts the files from the archive. If used with -U and a file in the archive is older than the corresponding file on the file system, the file is not extracted.
-help Displays the most important options.
-xhelp Displays the least important options.
-/ Do not strip leading slashes from file names when extracting the files from an archive. By default, they are stripped when files are extracted.
-acl When creating or extracting, archives or restores any ACLs associated with the files and directories.

Retrieving Acls RHEL 7

To determine the existing ACLs for a file or directory, use the getfacl command. In the example below, the getfacl is used to determine the existing ACLs for a file.

Example 4.4. Retrieving ACLs

# getfacl home/john/picture.png
The above command returns the following output:
# file: home/john/picture.png 
# owner: john 
# group: john 
If a directory with a default ACL is specified, the default ACL is also displayed as illustrated below. For example, getfacl home/sales/ will display similar output:
# file: home/sales/ 
# owner: john 
# group: john 


Setting Default Acls RHEL 7

To set a default ACL, add d: before the rule and specify a directory instead of a file name.

Example 4.3. Setting default ACLs

For example, to set the default ACL for the /share/ directory to read and execute for users not in the user group (an access ACL for an individual file can override it):
# setfacl -m d:o:rx /share

Setting Access Acls RHEL 7

There are two types of ACLs: access ACLs and default ACLs. An access ACL is the access control list for a specific file or directory. A default ACL can only be associated with a directory; if a file within the directory does not have an access ACL, it uses the rules of the default ACL for the directory. Default ACLs are optional.
ACLs can be configured:
  1. Per user
  2. Per group
  3. Via the effective rights mask
  4. For users not in the user group for the file
The setfacl utility sets ACLs for files and directories. Use the -m option to add or modify the ACL of a file or directory:
# setfacl -m rules files
Rules (rules) must be specified in the following formats. Multiple rules can be specified in the same command if they are separated by commas.
Sets the access ACL for a user. The user name or UID may be specified. The user may be any valid user on the system.
Sets the access ACL for a group. The group name or GID may be specified. The group may be any valid group on the system.
Sets the effective rights mask. The mask is the union of all permissions of the owning group and all of the user and group entries.
Sets the access ACL for users other than the ones in the group for the file.
Permissions (perms) must be a combination of the characters r, w, and x for read, write, and execute.
If a file or directory already has an ACL, and the setfacl command is used, the additional rules are added to the existing ACL or the existing rule is modified.

Example 4.1. Give read and write permissions

For example, to give read and write permissions to user andrius:
# setfacl -m u:andrius:rw /project/somefile
To remove all the permissions for a user, group, or others, use the -x option and do not specify any permissions:
# setfacl -x rules files

Example 4.2. Remove all permissions

For example, to remove all permissions from the user with UID 500:
# setfacl -x u:500 /project/somefile