In this chapter, we’ll explore how commands work in Linux. You’ll learn about command syntax, options, and arguments, as well as how to get help when you’re stuck. By the end of this chapter, you’ll understand how to use commands effectively and customize them to suit your needs.
Commands are the building blocks of Linux. Whether you’re navigating the filesystem, editing text, or managing processes, everything revolves around commands. Understanding how commands work will make you more efficient and confident in using Linux.
A typical Linux command has three parts:
1. Command: The program or utility you want to run (e.g., ls, cp, mv).
2. Options: Modifiers that change behavior of the command (e.g., -l, -a).
3. Arguments: The targets or inputs for the command (e.g., file names, directory paths).
Here’s an example:
$ ls -l /home/user
ls is the command.-l is an option (long listing format)./home/user is the argument (the directory to list).Linux provides several ways to get help with commands. Here are the most common methods:
Man: The Manual PagesThe man command displays the manual page for a command. It’s like a built-in user manual.
$ man lsThis opens the manual page for the lscommand. Use the arrow keys to navigate, and press q to quit.
--Help: Quick HelpMany commands support the --helpoption, which provides a brief summary of how to use the command.
$ ls --helpWhatis: One-Line DescriptionsThe whatis command gives a one-line description of a command.
$ whatis ls
ls (1) - list directory contentsNot all commands are created equal. Here are the main types of commands in Linux:
These are commands that are part of the shell itself (e.g., cd, echo, pwd). You can check if a command is built-in using the type command:
$ type cd
cd is a shell builtinThese are standalone programs located in directories like /bin, /usr/bin, or /usr/local/bin. For example:
$ type ls
ls is /bin/lsAliases are custom shortcuts for commands. You can create your own aliases to save time. For example:
$ alias ll='ls -la'Now, typing ll will run ls -la.
Sometimes, you might not know where a command is located. Here’s how to find it:
Which: Locate A CommandThe which command shows the full path of a command.
$ which ls
/bin/lsWhereis: Locate Binary, Source, And Manual PagesThe whereis command shows the location of the binary, source, and manual pages for a command.
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gzThe shell keeps a history of the commands you’ve typed. Here’s how to use it:
Use the history command to see a list of previously executed commands.
$ history!! to repeat the last command.!n to repeat the command with history number n.Press Ctrl + r to search your command history interactively. Start typing part of the command, and the shell will autocomplete it.
Command substitution allows you to use the output of one command as an argument for another. Use $(...) or backticks (`...`) for this.
$ echo "Today is $(date)"
Today is Wed Oct 4 12:34:56 UTC 2023Efficiency in command-line work can be greatly improved with tools like hstr for managing command history, aliases and functions for creating shortcuts, and tldr for quick command references.
hstr provides an interactive search interface for recalling previous commands, while aliases and functions allow you to chain multiple commands or set default flags. tldr offers concise examples of common commands, making it easier to find the right syntax without sifting through lengthy manual pages.
# Install hstr
$ sudo apt install hstr
# Install tldr
$ sudo apt install tldr
# Example usage:
$ hstr
$ alias ll='ls -lah --color=auto'
$ function greet() { echo "Hello, $1!"; }
$ tldr lsLet’s put your new skills to the test:
1. Use man to read the manual page for the cpcommand.
2. Create an alias called ll for ls -la.
3. Use which to find the location of the grep command.
4. Use command substitution to display the current date and time.
That’s it for Chapter 5! You’ve now learned how to work with commands effectively. In the next chapter, we’ll dive into redirection—how to control input and output using powerful tools like pipes and redirects. Until then, practice using commands and exploring their options to deepen your understanding.