Command line interface

From English Wiki
Jump to navigation Jump to search

The command line interface (CLI) or shell interface is an old fashioned, non-graphical interface in a shell, which is still convenient for serious Linux users. It may seem old fashioned, but seasoned Linux afficionados find it to be much faster, more convenient, more effective, and more powerful for many tasks than using a GUI.


1 Command line interface: General commands

Command Function
Move up in command history
Move down in command history
!! Repeat last command
sudo !! Repeat last command with sudo privileges
com1 && com2 Run command #2 if command #1 is successful (command1 && command2)
com1 ; com2 Run multiple commands, e.g., command1 ; command 2 ; command3 (execution proceeds whether previous commands were successfully executed or not)
com1 || com2 Run multiple commands: Run command2 if command1 is unsuccessful (command1 || command2 runs command2 if command1 does not execute successfully)
!cmd# Re-execute a command by its history number
cmd –help Brief help information on a command
cmd & Start a process and send it to the background
cmd < file Read input of command from a file
cmd 2> file Error output (stderr) of cmd to file
& cmd2 stderr of cmd1 to cmd2
cmd1; cmd2 Execute separate commands in succession
# Shell prompt symbol used for superuser
> Redirect: Send output of a process to a file (create a new file or overwrite an existing file)
>& OR &> Send output to a location
>> Append: Append output of a process to the end of an existing file
| command2)
$ Shell prompt symbol for normal user
1> Send stdoutput (standard output, e.g., to the screen) to a location
1>2& Redirect stdout (standard output) to stderr (error file)
2> Send stderror (error messages) to a location
2> /dev/null Redirect stderr to bitbucket
apropos keyword Display command names based on keyword search
bc Launch a high-precision calculator shell
cal Show a calendar for the current month
cal -3 Shows calendar for three months: previous, current, and following month
cal -n no. Shows calendar for specified number of months around the current date
cal month Shows calendar for specified month
cal month# year Shows calendar for specified month (by number) and year (e.g., cal 1 1812 = January 1812)
cal year Shows calendar for a specified year (from 01 to 9999 CE)
cd - Move to previous directory
chsh Change shell
clear Clear screen
CTL-ALT [F1, F2, F3...] Switch to alternate system terminal (tty)
date -d Print formatted date.
dmesg -k Display system messages
echo text Echo a message to the standard output (i.e., the screen)
echo text > file Redirect text to a new file (or overwrite an existing file)
at time at 12.00)
echo “ “Hello, world.” ” Display a message containing quotation marks or other escape characters, e.g., “ ‘ [ ] | / \ ( ) { } * ?
exit Exit the current shell
export A command for assigning or removing an environment variable.
expr Evaluate an expression.
history Displays history of previously entered commands in the shell
grep keyword Search history for a particular command
localectl Show locale related environmental settings
ls -S List and sort files by file size
man cmd Full manual entry for command
screenfetch Display system, OS and DM information
script Record terminal session activity and create output for the session in a file
source script . script Load a configuration file into current script or shell session
stderr Standard error, i.e., error messages
stdin Standard input (input to a command)
stdout Standard output, e.g., of a command to the screen
stty Set key commands or shortcuts
su - Log in as superuser
sudo Root or superuser privilege to execute commands
sudo -ll Show commands that the user is allowed to execute
sudo !! Rerun last command with sudo privileges
tee Read stdin and send output to stdout and file


1.1 Sudo behavior

If you find the timeout for sudo authentication to be too short, this can be modified using the program visudo (which should come pre-installed). Visudo allows for modifying the sudo configuration file and locking it at the same time so that no other programs modify it.

 sudo visudo 

At the end of the file, append the following line to change the timeout, for example, to 30 minutes.

Defaults:USER timestamp_timeout=30 


2 CLI environment variables

Command Function
$HOME Home directory
$HOME/.bashrc Location of user bash configuration file
$PATH Executable search path, i.e., paths for commands and executable files
$SHELL Current shell
alias Show defined aliases
alias [alias]=”[command]” Define a new alias in the bash environment file
cat etcvconsole.conf See keyboard settings for terminal sessions
echo "­STR­ING­" Output string
echo $NAME Output value of variable NAME
echo $Variable_name Output the value of a defined variable on the terminal.
env Display environment variables.
export NAME = value set "­value to NAME
export PATH="$PATH:/some/addition" Export new variable to bash configuration
export VAR Export a variable to the current shell and child processes
export Variable = value Assign an environment variable a new value.
localectl Show locale related environmental settings
printenv Print defined environmental variables
source .bashrc Reload bash shell settings after editing configuration file
Unset Remove or delete a variable.
variable_name = variable_value Assign a variable value to variable name
vi /etc/l­oca­le.gen Edit localization file for environmental variables


3 CLI keyboard shortcuts

Command Function
!! Execute a previously executed command.
CTL + C Kill a running process.
CTL + G Exit command history.
CTL + K Cut a line fragment succeeding the cursor.
CTL + O Execute the previously recalled command.
CTL + R Recall the last used command.
CTL + U Cut a line fragment preceding the cursor.
CTL + W Cut a single word preceding the cursor.
CTL + Y Paste from clipboard.
CTL + Z Stop or suspend a currently active process.
exit Sign off from an active session.



4 See also