CLI basics
Command Line Interface:
allows for text-based communication with a computer.
Prompt: [user]@[hostname]:[current_directory]$
User: current user logged in Hostname: the computer's name
Prompt, also called "PS1"
When you type in commands into Terminal, it will always take the following format:
[command][argument...]
Command can be a path to a file, or a command it is already aware of. Arguments are strings that are passed to the program that you are executing.
Flags are special arguments. They start with a dash "-" for the abbreviated form, and two dashes "--" for full word flags.
Notes: CTRL+C to stop or exit out of any executable process
Commands
| Command | Description |
|---|---|
| ~ | Home directory |
| / | Root directory |
| ./ | Current directory |
| ../ | Directory one level up |
| cd | Change directory |
| man | Manual (documentation) |
| echo | Print text to STDOUT |
| ls | List files and directories in current directory |
| pwd | Display path of current working directory |
| ** | Asterisk is a wildcard. Can be any number of characters. |
| cp | Copy |
| mv | Move or rename (or both) |
| mkdir | Make new directory (use -p for multiple directories nested together) |
| touch | Create new file |
| rm | Remove one or more files or directories (use -r for directories) |
| -r | Flag. Recursive. Use when command affects multiple nested files/directories |
| -p | Flag. Use to make multiple nested directories |
| -a | Flag. Include hidden files |
| -h | Flag. Makes file sizes 'human readable' by showing size in kb, mb and gb |
| -l | Flag. Shows permissions, size and other details |
| -c | Flag. Create |
| -z | Flag. Zip |
| -f | Flag. File |
| -lah | Get more detailed information about files and directories |
| . | When "." precedes a file name, it means the file is hidden |
| cat | Reads file and displays content |
| more | Display contents of file, starting at top letting user scroll down |
| less | Display contents of a file in an even more interactive way. |
| head | Display first part of file |
| tail | Display last part of file |
| export | Before a variable, means the variable is available outside |
| groups | Displays groups that the user is part of |
| tar | Archival command that can archive, compress and extract files |
Example: mv "file name or directory" "location" (location is relative to current directory)
Environment
In programming, variables are used to store data and to be able to reference and retrieve that data at a later point using a name.
Type env to list the environment variables.
In the command cd $HOME, the $HOME part is a reference to the HOME variable, and is replaced by the path to your home directory when the command is run. In other words, running cd $HOME is the same as running cd /home/ubuntu, assuming your home directory is /home/ubuntu.
One of the most important environment variables is PATH
most of the paths end in /bin. This is because bin is short for "binary", and bin is a standard directory name for executable files, or programs
Summary - The PATH variable determines which directories are searched when a command is entered - PATH is an ordered, colon-delimited, list of directories that contain executable files - The order of the directories in the PATH variable is first-found-first-execute - If you use a /, ., or ~ before your command, the command line will interpret that as an actual path to a file, and will not use the PATH variable - You can add to PATH to make more commands available without having to memorize their exact path - Modifications to PATH, or any environment variable, on the fly will not be permanent; permanent modifications should be done in an environment file, like .bashrc
Permissions
Permissions are assigned to files and folders, not users and groups.
2 parts: ownership and access types. Ownership:
1- user "u"
2- group "g"
3- other "o"
"a" is used to denote all
Access:
1- read "r"
2- write "w"
3- execute "x"
"-" mean permission NOT granted.
Each level of ownership can be granted one or all of the access types (total of 2^9 = 512 combinations).
chmod
Set or edit permissions groups Displays groups that the user is part of
Root user is the super user that can read, write and delete any file.
The sudo command allows you to do something as a super power. Assuming the root momentarily. You'll be using your own password instead of the root password.
Changing context in Terminal
- Database management commands: mysql, psql, redis-client, mongo
- Text editors: vim, pico, nano, emacs
- REPLs (Read-Evaluate-Print-Loop), which are basically interactive scripting consoles: irb, python, php -a
- System monitoring: top, htop
- Reading files or manuals: man, less, more
- Window/Session handling: byobu, screen, tmux
Programs that follow the pattern of reading user input, evaluating the input, printing results of the input to the screen, and then allowing for more input, are called REPLs.
Interactive interfaces for scripting languages such as Ruby, Perl, Python, and PHP are commonly referred to as REPL.