0% found this document useful (0 votes)
9 views4 pages

Terminals

Terminal (linux) notes

Uploaded by

John Irving
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Terminals

Terminal (linux) notes

Uploaded by

John Irving
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

A terminal is technically just a program that lets you issue text commands and

returns their output. The program that actually runs that command is called a shell
Shells are often called "REPLs", standing for:
Read, Eval(uate), Print, Loop
i.e., shells read the commands you type, evaluate those commands (usually by
running other programs), print the output of those commands, and give you a new
prompt to type and repeat
the cat command prints the contents of a text files. Multiple files can be listed,
which will print in succession.

The head command prints the first n lines of a file, where n is a specified number:
ex) head -n 10 file1.txt
tail is similar, but it prints the last n lines.

both default to 10.

The more and less commands prints the contents of a file one page/line at a time.
less is functionally the same as more, but has more features. You generally should
use less, but use more if less isn't installed.
You can use the less command with the -N flag to show line numbers (i.e. less -N
filename.txt)
use spacebar to scroll down a page, and b to scroll up a page.

The move command (mv) can move a file or directory to a new location or reame a
file.

cp can be used to copy files

grep allows toy to search for text in files:


grep "hello" hello.txt will print out every line that contains the word
"hello". This is case sensitive.
multiple files can be greped:
grep "hello" hello.txt hello2.txt
to use grep on an entire directory and all of its subdirectories, use the -r
argument:
grep -r "hello" . (. is the alias for current directory) (~ is the alias for
home directory)

Use find to search for a file with the exact name :


find some_directory -name hello.txt

Permissions:
First characher tells you if you're looking at a file or directory:
-: regular file (-rwxrwxrwx)
d: directory (drwxrwxrwx)
The next 9 characters describe the permissions of Owner, Group, and Others in
that order. Each group of 3 character represent reading, writing and exexutiing in
that order.
rwx: all permissions
rw-: read and write, but not execute
r-x: read and execute, but not write

ex) -rwxrwxrwx: a file where anyone can do anything


-rwxr-x-r-x: a file where anyone can read and execute, but only the
owner can write
drwxr-xr-x: a directory where antone can read (ls the contents) and
execute (cd into it) but only the owner can write and modify the contents
drwx------: a directory where only the owner can read, write and
execute.
Changing permissions:
chmod -R u=rwx,g=,o= DIRECTORY
In the command above, u means "user" (aka "owner"), g means "group",
and o means "others". The = means "set the permissions to the following", and the
rwx means "read, write and execute".
The g= and o= mean "set group and other permissions to nothing".
The -R means "recursively", which means "do this to all of the contents
of the directory as well".

sudo elevates a command to the root user (superuser), which has access
to everything and can do anything.
be cautious with what commands you use as superuser:
rm (remove) with the flags r and f run on the root directory will
delete all files on your system.
r (recursive) deletes everything inside and f (force).
chown (change owner) changes the owner of a file.

Compilation vs Interpretation:
A compiled program is a program that has been converted from a human-readable
source code into machine code (binary.
Programming languages like Go, C, and Rust are compiled languages that
produced compiled programs.

Interpreted Programs are programs executed by another program. The program which
executes an interpreted program is called an interpreter.
Interpreted languages include Python, Ruby and JavaScript and .sh shell
script files.

You can run any executable by typing its file path into your shell.
To run an executable that requires an interpreter, the computer needs to be told
what program to use.
This is accomplished by a "shebang", a line at the top of a script. It's
format is:
#! interpreter [optional-arg]

sh - The Bourne shell. This is the original Unix shell and is POSIX-compliant.
It's very basic and doesn't have many quality-of-life features.
bash - The Bourne Again shell. This is the most popular shell on Linux. It
builds on sh, but also has a lot of extra features.
zsh - The Z shell. This is the most popular shell on macOS. Like bash, it does
what sh can do, but also has a lot of extra features.

Both zsh and bash are "sh-compatible" shells, meaning they can run .sh scripts, but
they also have extra features that generally make them more pleasant to use.
For your purposes, the differences between zsh and bash are not super significant.

There is another type of variable called "environment variables". They are


available to all programs that you run in your shell.

To create an environment variable, use the export command:export NAME="Lane"

The PATH environmental variable is built-in to the shell. It is a list of


directories that your shell will look when yo run a command.
If you run ls, it will look in each directory listed for an executable called
"ls". If it doesn't, it will give you an error like "command not found."

Echoing $PATH lists all directories the shell will look when executing
commands, each seperated by colons (:) ex.
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
use the nano command to edit files: nano ~/.bashrc

use the man (manual) command to bring up docummentation for programs. ex) man ls

By convention, most production-ready CLI tools have a "help" option that will print
out some information about how to use the tool. It's usually accessed with one of
the following:

--help (flag)
-h (flag)
help (first positional argument)

To find an exit code for a program that ran unsuccesfully, use echo $? after
receiving the error.
0 means no error
1 is used for a default or catch-all error.
Others are more specific.

Standard Output (Standard out, stdout) refers to the default place where programs
print their output. For Python that's print(), and shell that's echo.

Standard Error (stderr) is similar, but for errors.


Stdout and stderr can be redirected using the > and 2> operators. > redirects
stdout and 2> redirects stderr.
ex) echo "Hello world" > hello.txt
cat hello.txt
# Hello world
The above redirects the stdout (echo) to a text file (hello.txt)

cat doesnotexist.txt 2> error.txt


cat error.txt
#cat: doesnotexist.txt: no such file or directory
the above directs the stderr into a textfile (error.txt)

Standard Input:
default way a programs reads input text. In python thie is input().
ex) name = input("What is your name? ")
print("Hello,", name)

In shell, it's read.


ex) echo "Please enter your email:"
read EMAIL

Piping (|): takes stdout of the program on the left and "pipes" it into the stdin
of the program on the right:
ex) echo "Have you heard the tragedy of Darth Plagueis the Wise?" | wc -w
# 10

Sometimes a program will get stuck and you'll want to stop it. Common reasons for
this are:

You made a typo in the command and it's not doing what you want
It's trying to access the internet but you're not connected
It's processing too much data and you don't want to wait for it to
finish
There is a bug in the program causing it to hang

In these cases, you can stop the program by pressing ctrl + c. This sends a
"SIGINT" signal to the program, which tells it to stop.

Kill

Sometimes a program is in such a bad state (or is so malicious) that it doesn't


respond to the SIGINT, in which case the best option is to use another shell
session (new terminal window) to manually kill the program.
Syntax

kill PID

PID stands for "process ID". Every process that's running on your machine has
a unique ID. The ps, "process status" command can be used to list the processes
running on your machine, and their IDs:

ps aux

The "aux" options just mean "show all processes, including those owned by
other users, and show extra information about each process".

Unix Philosophy:
1. Write programs that do one thing and do it well.
Why programs like ls, grep, and less exist. They do one thing and
do it well.
ls lists files and directories; grep searches for text in a file;
less displays text

2. Write programs to work together.


Programs that do only one thing work well together. Forexample
you can pipe the output of grep into less to display results interactively:
grep "hello" some_file.txt | less

3. Write programs to handle text streams, because that is a universal


interface.
text stream is just text. It is more powerfl in terms of
extensibility and interoprability standpoint than graphical.

Package Managers: Handle downloading, installing, updating removing software and


managing dependencies.
APT (Advance Package Tool) is the primary manager for Ubuntu.

Neovim:
You start in "normal" mode. Press "i" to enter insert mode to edit text. Hit
esc to re-enter normal mode.
type :w to save and :q to exit.

Webi (Webinstall.dev) is another package manager. It installs things directly from


the web, without the need of a local tool like apt or brew.
Find the package you want to install and follow the instructions (generally
copying and pasting a command into the shell)

You might also like