Command line tools
Command line tools refer to software programs that can be used via a command line interface (CLI), rather than through a graphical user interface (GUI). These tools allow users to perform tasks by typing text commands into a terminal or console window. Command line tools are prevalent in various operating systems, including Linux, macOS, and Windows, and they are essential for system administration, software development, and data processing.
These tools provide powerful functionality that can often be combined or scripted to automate complex workflows. Examples of command line tools include file manipulation programs like cp and mv, text editors like vim and nano, and networking tools like ping and ssh. They are appreciated for their speed, flexibility, and the control they provide to the user for managing the system and handling files.
Tips and tricks
Stop a command
- Press Ctrl + C: interrupt the currently running command and return to the prompt.
This is super useful if you accidentally start a command that takes too long or behaves unexpectedly. It also works if a command is waiting for input and you want to cancel it. Finally, it can be a quick way to start over if you made a mistake typing a command.
Autocomplete
- Press Tab: complete when there's only one match.
- Press Tab twice: show choices when there are multiple matches.
This works for commands, files, folders, and many options. For example:
gi→gitcd Doc→cd Documents/git ch→git checkout/git cherry-pick
Command history
- Press Up / Down: navigate through commands you've run before.
- Press Ctrl + R: reverse search through your command history (type to filter, Enter to run).
history: print your history list.!!: rerun the last command.
Arguments, quotation marks and special characters
When passing arguments to commands, be mindful of spaces and special characters. When dealing with paths or filenames that contain spaces, enclose the paths in quotation marks (" or ') to ensure they are treated as a single argument. Alternatively, you can escape the space with a backslash (\). For example:
- Wrong:
rm My Project(will be interpreted as removing two different paths:MyandProject) - Correct:
rm "My Project"git add 'My Project'git add My\ Project
If you don't close the quotation marks properly, the terminal will think you're still typing the argument and wait for you to finish it (displaying dquote> on a new line). Either close the quotation mark on the new line or cancel this by pressing Ctrl + C.
Multiline arguments
Pressing Enter in the terminal usually executes the command you've typed right away.
However, sometimes you want to split a long command or argument across multiple lines for better readability. In this case, you can type a backslash (\) at the end of the line to indicate that the command continues on the next line. For example:
git rebase -i HEAD~5 \--autosquash \--keep-empty
However, there's a little gotcha here: using backslashes like this only affects how the command is displayed in the terminal; it does not insert actual line breaks into the argument itself. For example:
echo "First line\Second line"
Results in:
First lineSecond lineTo put the actual line breaks into the string, you must include the newline character (represented by \n) within the quotes, like this:
echo "First line\nSecond line"Or, if you want to format it across multiple lines:
echo "First line\n\Second line"
This will produce:
First line
Second line