Learning cli
Learning How to Use a Command Line Interface.
All jokes aside,commandline interfaces (or clis) are some of the most powerfull and flexable means of workng with computers. Source from Code Acadamy
date 8-27-2015
Manipulating Objects:
Options modify the behavior of commands:
ls -alists all contents of a directory, including hidden files and directoriesls -llists all contents in long formatls -torders files and directories by the time they were last modified Multiple options can be used together, likels -alt
From the command line, you can also copy, move, and remove files and directories:
cpcopies filesmvmoves and renames filesrmremoves filesrm -rremoves directories- [!]Wildcards are useful for selecting groups of files and directories.
pwdprints current working directory.whereis filenam/commandShows the location of a command or file.
Redirection:
cat: Prints the contents of a file to the terminal.wc: Outputs # of lines, words, and characters in a file.sort: Takes std-in and orders it alphabetically for std-out.uniq: Stands for unique; filters adjacent duplicate lines in a file. best if used with sort: [ex.][sort f.txt | uniq > out.txt]grep: Global regular expression print.
--i : Makes grep case insensitive.
--R : recursively searches all files in a directory for a target. Outputs filenames & lines containing matches.
– [ex] : grep -R topic /file/in/specific/path/
--l : Stands for list files with matches.
sed: Stream editor; takes input & mods it based on an expression.- [*]
sed 's/snow/rain/g' forests.txt([s]/“search str”/“replacement str”/[g]) in forests.txt [s][substitution], [g][global].
- [*]
Output commands:
>overwrites “doca.txt” with “docb.txt” docb.txt > doca.txt>>appends one document to another “tacks docb to end of doca”
Input commands:
<inputs contents of a file into a command.|Pipe command:, pipes info from left to right ->.
IN SHORT:
The common redirection commands are:
>redirects standard output of a command to a file, overwriting previous content.>>redirects standard output of a command to a file, appending new content to old content.<redirects standard input to a command.|redirects standard output of a command to another command.A number of other commands are powerful when combined with redirection commands:
sort: sorts lines of text alphabetically.uniq: filters duplicate, adjacent lines of text.grep: searches for a text pattern and outputs it.sed: searches for a text pattern, modifies it, and outputs it.
Environment:
~/.bash_profile: file used to store environment settings. when a session starts, it will load the contents of the bash profile before executing commands.~ : represents the home directory. . : indicates a hidden file.sourcemakes changes available right away in the current session.source ~/.bash_profile: activates the changes in~/.bash_profilefor the current session.
alias: enables the creation of keyboard shortcuts or “aliases”. [ex.]alias pd="pwd"export: used to export environment variables to all child sessions initiated from the session you are in. [!] to use said variable you must specify a$in front of it.$
PS1: environment Variable that defines the makeup and style of the command prompt.$
PATH: environment Variable that stores a list of directories separated by a colon.env: stands for “environment”
IN SHORT:
The environment refers to the preferences and settings of the current user.
The nano editor is a command line text editor used to configure the environment.
~/.bash_profileis where environment settings are stored. You can edit this file with nano.environment variables are variables that can be used across commands and programs and hold information about the environment.
export VARIABLE="Value": sets and exports an environment variable.USER: is the name of the current user.PS1: is the command prompt.HOME: is the home directory. It is usually not customized.PATH: returns a colon separated list of file paths. It is customized in advanced cases.env: returns a list of environment variables.
KEYBOARD SHORTCUT REFERENCE:
TAB: Auto completes a partially entered command.Ctrl+Shift+C: Copies selected text in the command line. assumes you are using a GUI CLI!Ctrl+Shift+V: Pastes copied text in the command line. assumes you are using a GUI CLI!Ctrl+c: Kills currently running process in the command line. sendsSIGTERMCtrl+r: enables you to search previously entered command history.