How to Linux
A brief guide on how to use Linux command lines.
Terminals and Shells
Linux systems support a basic user interface over the TTY (TeleTYpewriter) application. This application renders text and takes user input, offering a framework for running a shell. The shell is the application that hosts a command-line, allowing users to issue commands to the operating system. On most systems, the shell will be bash or zsh.
Bash/Zsh
The first word sent to bash/zsh is interpreted to be a program to run, and following words are arguments that are sent to the program:
program argument1 argument2 ...
For instance, if you wanted to list files in your Documents directory, you may run
/bin/ls /home/username/Documents
In this case, there is a program at /bin/ls that we are running, and we are passing it the parameter Documents.
Variables
Sometimes, a certain phrase may be typed often enough to where we may want to store it in a variable. For instance /home/username is your user directory, and is very commonly accessed. This path is by default stored in the variable HOME. We use the character $ to let bash know that we are referring to a variable, so the command above becomes
/bin/ls $HOME/Documents
Because the home directory is so commonly used, there is also an alias that binds ~ to $HOME
/bin/ls ~/Documents
If we want to set our own variables, we can use the = operator
docs="$HOME/Documents"
/bin/ls $docs
Search Paths
Linux systems often stores many programs in some directory. For instance, /bin typically holds a few dozen commands we often need to use. To avoid needing to specify the full path to the program, there is a variable PATH that lists the directories for the shell to search for programs. If we set PATH to include /bin, then we will be able to run ls without specifiying where it is.
PATH="/bin" ls ~/Documents
Export
Sometimes we want a variable to be globally available to all programs the shell runs. If this is the case, we need to run the program export to make this happen.
export PATH="/bin"
Output Redirection
Typically, when a program prints some output, it displays it on-screen. However, we may want to send the programs output somehwere else, such as into a file. We can redirect the output using the >> operator. For instance, if we want to save the list of documents to a file dirs.txt in our home directory, then we can run
ls ~/Documents >> ~/dirs.txt
In addition to
Saving Configuration
It would suck to have to configure the shell every time, so most operating systems will load a configuration file, typically ~/.bashrc or ~/.bash_profile (sometimes both). These files contains a list of commands to run whenever the shell is started, for instance, if we want to ensure /bin is always in our search path, we store
echo 'export PATH="/bin"' >> ~/.bashrc
(Don't run this. Your shell already has a defined PATH with many other directories included).
Manual
Linux has manuals for each program. To install the manuals, run
Ubuntu
sudo apt install man-db
Arch Linux
sudo pacman -S man-db
To view the manual for a program, run
man program
for instance, to view the manual for ls
man ls
press q to exit the manual.