5 Ways to Make Linux Commands Work the Way You Want
Linux & macOS Terminal
Quick Links
-
Change Behavior With Command-Line Options
-
Create Aliases for Common Uses
-
Set Environment Variables When You Run the Command
-
Use Permanent Environment Variables
-
Edit Application-Specific Config Files
When you’re getting to grips with Linux and the command line, it’s important to unlock its full power. Most commands you run have a wealth of settings you can tweak to change what they do. You just need to know where to look.
1 Change Behavior With Command-Line Options
The most basic way to change how commands run is by passing options as arguments. You’ve probably used arguments without thinking about it; when you run a command like ls, for example:
ls docs/
Here, docs/ is an argument that tells ls which file to list. The command will run its default action: display the names of files in the docs directory, in columns across the screen.
However, ls has many options that you can use to change its behavior. These take the form of an argument beginning with a minus sign (“-“), between the command name and its other arguments:
Option |
Behavior |
---|---|
-l |
List files in long format. |
-a |
Include hidden files. |
-d |
List directories as files instead of showing their contents. |
-t |
Sort files showing the most recent first. |
-F |
Label file types using a trailing character. |
Use an option by typing it, surrounded with spaces, after the command’s name. You can combine single-letter options so you don’t need to keep repeating the minus sign:
ls -a .
ls -lt
ls -Fdatl /usr/local/bin
Many Linux commands also support long options which look like this:
ls --all
This is equivalent to ls -a. You cannot combine long options and they can involve a lot of typing, but they’re much easier to understand. Therefore, you might prefer to use them until you are more familiar with a command, and they’re perfect for scripts that you might share with other users.
Use
the man command
to find out more about
command usage and which options you can use
.
2 Create Aliases for Common Uses
An alias is like a shortcut to a command with pre-defined options. Aliases have many benefits but their main purpose is to increase convenience and save time. So, if you want the ls command to always show a long listing, like this:
ls -l
You can define an alias like this:
alias ls='ls -l'
Now, when you run the ls command, it will always use the -l option to display files in the long format. If you want to keep the default behavior of ls, you can use a new name for the alias, e.g.
alias ll='ls -l'
Running this on the command line will set a temporary alias for your current session. You can list all current aliases by running the command with no arguments:
alias
Your system may set up certain aliases by default, but you can set your own, permanently, in an appropriate startup file. The exact file location will depend on your system and the shell you run. Typically, on Linux, you’ll want to add aliases to ~/.bashrc and, on macOS, to ~/.zshrc.
3 Set Environment Variables When You Run the Command
Commands will often use environment variables to change how they operate. A command’s man page should list these in a section headed “Environment” or “Environment Variables.” Here’s an example, showing the man page for the less command:
You can use an environment variable by setting it when you run a command, a bit like how you use options:
NAME=VALUE command
For example, the less pager supports an EDITOR environment variable. This lets you change the command that runs when you press “v” to edit a file. You can set this variable to your preferred editor when you run the command:
EDITOR=vim less ./myfile
The less program even supports a variable—LESS—that lets you pass any option to it, as an alternative to using aliases. Here’s an example where I use LESS to set two options instead of passing them as arguments:
4 Use Permanent Environment Variables
You may want some environment variables to apply by default permanently. The previous example showed how you can set options using a variable, but there’s little point if you’re still typing out the options each time you run a command. Instead, you can define environment variables that will apply when you start your terminal session.
Again, how you do so will depend on your exact system and how you like to set things up. A common approach is to use the /etc/environment file which applies to all users of your system. This is a text file listing environment variables, not a shell script:
You can add any environment variable setting to this file using the same “NAME=VALUE” format.
Environment variables become really powerful when they are standardized and several commands can share them. For example, many commands support the PAGER and EDITOR variables, so if you have a preference, just set it once and all supporting commands will automatically obey.
If you want to set user-specific environment variables, you can do so in a file like ~/.bashrc (Linux) or ~/.zshrc (macOS). These are script files so they can do a lot more, including using the symbol “~” which expands to your home directory.
5 Edit Application-Specific Config Files
The final type of configuration depends on the command—or application—you are using. Each app is free to use whichever configuration format it wants, although plain text formats are most popular under Linux.
You’ll usually have some of these files in your home directory (e.g. ~/.vimrc) or, on modern systems, in a hierarchy under the hidden folder ~/.config.
Here are some examples of different types of config file.
Run-Control Files
These are typically hidden files ending in “rc”; the .bashrc file covered earlier is one case. These files can be full-blown scripts—as in the case of .bashrc—or more data-driven, listing settings like .vimrc.
INI-like
Some config files borrow from the Windows INI format for configuring (“initializing”) programs. In this format, settings are grouped into sections, each of which is headed with a term in square brackets. Individual settings use a familiar “NAME = VALUE” syntax.
A good example of this format is the .gitconfig file that the git version control system uses:
JSON
JSON is one of the most popular data interchange formats currently in use. It borrows JavaScript’s syntax for object definition and lets you define more complex structures in configuration files. The cleed RSS feed reader uses JSON as its configuration format which looks like this:
Scripts
Some programs reuse the bash script format for their configuration. This adds a lot of functionality with the downside that it can get more complicated. The system information tool, neofetch, uses this approach which makes it highly customizable:
Bespoke
Some apps have such a complicated configuration that they resort to their own, bespoke format. The Apache web server is a perfect example. Supporting an enormous range of website setups, this app’s documentation is mostly about its vast configuration.
Linux configuration can be daunting because there is so much of it, and each program can deal with it slightly differently. But this means your toolset is immensely flexible, so it’s well worth getting to grips with command settings. Learn how to read man pages, experiment with run-control files, and live your Linux life to its fullest!