Command line:
Part 2
Welcome
Back
:)
Editing Scripts
You can use the vi or vim commands to edit files
Typically there is no difference between the two.
The vi editor operates based on modes.
The mode you are in dictates which
options you have available to you.
Editing Scripts
When the editor first opens, it is in "normal" mode.
Content can be added using "insert" mode
Editor commands are issued using "command-line" mode
There are several other modes, but
most of the action happens in these three modes.
Hit Esc to exit the current mode
and go back to normal mode
Normal Mode
Use 0 to move the cursor to the beginning of the line
Use $ to move the cursor to the end of the line
Move the cursor on character using h, j, k, or l
Use b to move the cursor to beginning of the current word
Use e to move to the end of the current word
Use w to move to the beginning of the next word
Insert Mode
Use A, a, I, i, O or o to enter insert mode
Command-line Mode
Allows you to enter a command at the bottom of the screen
Triggered by entering a :, ?, /, or !
:q quits the application
Scripts
Files that contain commands are called "scripts"
When you open a new terminal window,
the ~/.bash_profile script is run.
This prepares the initial state of the environment
before the first prompt is displayed.
Scripts
It is common for Linux systems to only run ~/.bash_profile once
when the user first logs in.
The ~/.bashrc script is then run with each new shell.
To avoid duplication many times .bashrc is run explicitly within .bash_profile.
Environment Variables
The bash shell supports the ability to set variables and
use their values in commands.
Variables used to configure the behavior
of the shell environment use the convention
of being all upper-case separated by underscores.
These are generally referred to as environment variables.
The PATH environment variable is special in that
the shell uses it value to find most commands.
Environment Variables
Set the value of a variable
variable="some value"
Get the value of a variable
ls -la $variable
You can use the echo command to
check the value of a variable
$PATH
The value is a list of directory paths delimited by a single colon.
E.g. PATH=/usr/local/bin:/bin
When a command is entered that isn't built into the shell,
it will start at the left-most path and look for the command.
Only the first instance of the command
Is ever run, so order is important.
Getting Data From
Other Sources
The curl command can be used to
send network requests to other machines.
curl http://www.example.com
By default it will output
the response data from the request
Saving to a file
Use the -O flag to save the request to a file in the current directory.
curl -O http://www.example.com
Use the -o flag to specify which file
to save the response to.
curl -o example.html http://www.example.com/
Following Redirection
It is common for web requests to result in
location headers that redirect the client to another URL
Curl does not follow the location headers by default.
Use the -L flag to follow location headers.
curl -L http://bitly.com/VDcn
Just the Headers
Use the -I flag to just get the head of the response
curl -I http://www.google.com
curl -L -I http://m.google.com
Copy as Curl in Chrome
The Chrome browser supports exporting
network requests as curl from
within the "Network" tab of the developer tools
Remote Terminal
You can open a shell session on another
machine using the ssh command
ssh my.other.machine
This should prompt you for a username and password.
You can supply a username as part of the host name as well.
ssh username@my.other.machine
Running a command remotely
You can pass a command as the last argument to ssh.
The session will be closed immediately after the command finishes
ssh my.other.machine 'ps eax'
Or
ssh my.other.machine -- ps eax
Port Forwarding
You can use ssh to forward network traffic
from a local port to a port on a remote machine
ssh -L 8888:google.com:80 my.other.machine
This opens a shell on my.other.machine
then forwards it's port 8888 to google.com on port 80
Customization
While its important to memorize the basics, the flexibility of the command-line is all about composability.
Because of that, the most useful commands can sometimes be so long and convoluted it makes it difficult to remember/type them.
Use the alias command to set shortcuts
to commonly used commands
alias ll="ls -la"
Hit me up with questions.
jared.k.stilwell@gmail.com
@meany_face (twitter)
Command Line: Part 2
By Jared Stilwell
Command Line: Part 2
- 834