The graphical interface is often more intuitive and easier to get started with but the Command Line interface has its own advantages that make it essential for power users and software developers. The text-based nature of Command Line lends itself well to the following:
- automate tasks
- share the steps/scripts with your team
- document the instructions to achieve a task
- manage the changes to a script using git
To top it up, the Linux command line system is incredibly well-designed. It provides tools (commands) that act as building blocks and can be combined with other tools to achieve complex tasks with relative ease. Famously, Donald Kuth wrote a 17-page long program to print the top n most common words from a file in his book Literate Programming (1992). Mcllroy wrote a solution using Command Line tools which was short enough to put in a tweet:
tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q
This shows the power of Linux command line. At the heart of the system is the ability to combine commands. Here are the different ways of combining commands in bash and zsh shells:
Command Substitution
cat `fzf`
fzf
is used to fuzzy find a file and return its path that is passed to cat
command. cat
prints the file contents to standard output.
cat $(fzf)
Above is the more modern syntax for command substitution:
Combine using pipe
ls -l /bin | less
The pipe operator passes stdout of a command into stdin of another. Here is the directory list (ls -l /bin
) is passed into less
to display one page at a time.
Redirecting Output
echo Sample Text > outfile
The output is redirected to a file instead of stdout. outfile
is created or overridden.
echo Sample Text >> outfile
Appended to outfile
.
cp nonexistent.txt file.txt 2> errors
We can redirect stderr with the symbol 2> followed by a filename.
cp myFile.txt file.txt &> errors
To redirect both stdout and stderr to the same file, use &> followed by a filename.
Input Redirection
<(any command here)
runs the command in a subshell and presents its output as if it were contained in a file.
wc <(echo I like linux)
wc
counts lines, words, and characters but it excepts a file. We can overcome that by input redirection. Here is the output:
1 3 13 /dev/fd/11
Chaining commands
cmd1 && cmd2
Only runs the second command if the first is successful.
cd dir || mkdir dir
Only runs the second if the first one fails.
echo $SHELL; echo $USER
Run commands unconditionally.
Subshell
$ (cd /usr/local && ls)
Enclose a command in parentheses and it runs in a subshell.
It does change to the directory of your current shell.
An interactive subshell can be launched by typing ‘bash’ (or ‘zsh’, depending on your shell). Bash shell is an ordinary program that lives in /bin alongside ls
, mkdir
etc.
To exit the subshell, press Ctrl+D.
Run in background
wc my_extremely_huge_file.txt &
Use &
at the end to run the command in the background.
The ampersand is also a list operator, like && and ||, allowing you to run multiple tasks.
To send a running command to background, press Ctrl-Z to suspend the command temporarily and return to the shell prompt; then type bg to resume running the command in the background.
To view all jobs running, type ‘jobs’.
Store the result of a command
now="$(date)"
echo "$now"
The result of a command can be stored in a variable and used in other commands.
Run text as a command
bash -c 'echo “Hello”'
bash
command takes a string and runs it as a command. Very useful for building the command on the fly using strings.
Brace Expansion
mkdir {a..z}
This is going to create 26 directories.
xargs command
xargs merges the input strings (from stdin) and the command template to produce and run new, complete commands
find -name \*.pdf | xargs rm
Find all file names ending with .pdf, then remove them.