mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-05-14 23:24:46 +00:00
Upload of pre-existing files
This commit is contained in:
commit
4c21152830
150 changed files with 730703 additions and 0 deletions
200
Bash/Bash Commands.md
Normal file
200
Bash/Bash Commands.md
Normal file
|
@ -0,0 +1,200 @@
|
|||
# Bash Commands
|
||||
|
||||
**NOTE**: Square brackets (`[]`) denotes optional commands/flags/arguments. Uppercase denotes placeholders for arguments.
|
||||
|
||||
## Basic Commands
|
||||
|
||||
### Elevated Priviledges and Users
|
||||
|
||||
[sudo vs su](https://unix.stackexchange.com/questions/35338/su-vs-sudo-s-vs-sudo-i-vs-sudo-bash/35342)
|
||||
|
||||
```bash
|
||||
sudo su # login as root (user must be sudoer, root password not required) DANGEROUS
|
||||
sudo -s # act as root and inherit current user enviroment (env as is now, along current dir and env vars) SAFE (can modify user enviroment)
|
||||
sudo -i # act as root and and use a clean enviroment (goes to user's home, runs .bashrc) SAFEST
|
||||
sudo COMMAND # run a command w\ root permissions
|
||||
sudo -u USER COMMAND # run command as user
|
||||
|
||||
su # become root (must know root password) DANGEROUS
|
||||
su - USER # change user and load it's home folder
|
||||
su USER # change user but dont load it's home folder
|
||||
```
|
||||
|
||||
### Getting Info
|
||||
|
||||
```sh
|
||||
man COMMAND # show command manual
|
||||
help COMMAND # show command info
|
||||
whatis COMMAND # one-line command explanation
|
||||
apropos COMMAND # search related commands
|
||||
which COMMAND # locate a command
|
||||
history # list of used commands
|
||||
|
||||
id # Print user and group information for the specified USER, or (when USER omitted) for the current user
|
||||
```
|
||||
|
||||
### Moving & Showing Directory Contents
|
||||
|
||||
```sh
|
||||
pwd # print working (current) directory
|
||||
ls [option]... [FILE]... # list directory contents ("list storage")
|
||||
cd rel_path # change directory to path (rel_path must be inside current directory)
|
||||
cd abs_path # change directory to path
|
||||
cd .. # change directory to parent directory
|
||||
cd ~ # go to /home
|
||||
cd - # go to previous directory
|
||||
pushd PATH # go from current directory to path
|
||||
popd # return to previous directory (before pushd)
|
||||
```
|
||||
|
||||
### Creating, Reading, Copying, Moving, Modifying Files And Directories
|
||||
|
||||
```sh
|
||||
touch FILE # change FILE timestamp fi exists, create file otherwise
|
||||
cat [FILE] # concatenate files and print on statndard output (FD 1)
|
||||
cat >> FILE # append following content ot file (Ctrl+D to stop)
|
||||
file FILE # discover file extension and format
|
||||
stat FILE # display file or file system status
|
||||
|
||||
tail # output the last part of a file
|
||||
tail [-nNUM] # output the last NUM lines
|
||||
|
||||
more # filter for paging through text one screenful at a time
|
||||
less # opposite of more (display big file in pages), navigate with arrow keys or spacebar
|
||||
|
||||
cut # remove sections from each line of files
|
||||
cut -[d --delimiter=DELIM] # use DELIM instead of TAB for field delimiter
|
||||
cut [-f --fields=LIST] # select only these fields
|
||||
|
||||
df # report file system disk space usage
|
||||
|
||||
rm FILE # remove file or directories
|
||||
rm DIRECTORY -r # remove directory an all its contents (recursive)
|
||||
rmdir DIRECTORY # remove directory only if is empty
|
||||
|
||||
mkdir DIRECTORY # make directories
|
||||
|
||||
mv SOURCE DESTINATION # move or raneme files
|
||||
mv SOURCE DIRECTORY # move FILE to DIRECTORY
|
||||
|
||||
cp SOURCE DESTINATION # copy SOURCE to DESTIANTION
|
||||
```
|
||||
|
||||
### Files Permissions & Ownership
|
||||
|
||||

|
||||
|
||||
```sh
|
||||
chmod MODE FILE # change file (or directory) permissions
|
||||
chmod OCTAL-MODE FILE # change file (or directory) permissions
|
||||
|
||||
chown [OPTION]... [OWNER][:[GROUP]] FILE... # change file owner and group
|
||||
chgrp [OPTION]... GROUP FILE... # change group ownership
|
||||
```
|
||||
|
||||
**File Permissions**:
|
||||
|
||||
- `r`: Read. Can see file content
|
||||
- `w`: Write. Can modify file content
|
||||
- `x`: Execute. Can execute file
|
||||
|
||||
**Directory Permissions**:
|
||||
|
||||
- `r`: Read. Can see dir contents
|
||||
- `w`: CRUD. Can create, rename and delete files
|
||||
- `x`: Search. Can access and navigate inside the dir. Necessary to operate on files
|
||||
|
||||
***Common* Octal Files Permissions**:
|
||||
|
||||
- `777`: (`rwxrwxrwx`) No restrictions on permissions. Anybody may do anything. Generally not a desirable setting.
|
||||
- `755`: (`rwxr-xr-x`) The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users.
|
||||
- `700`: (`rwx------`) The file's owner may read, write, and execute the file. Nobody else has any rights. This setting is useful for programs that only the owner may use and must be kept private from others.
|
||||
- `666`: (`rw-rw-rw-`) All users may read and write the file.
|
||||
- `644`: (`rw-r--r--`) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change.
|
||||
- `600`: (`rw-------`) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private.
|
||||
|
||||
***Common* Octal Directory Permissions**:
|
||||
|
||||
- `777`: (`rwxrwxrwx`) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting.
|
||||
- `755`: (`rwxr-xr-x`) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users.
|
||||
- `700`: (`rwx------`) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others.
|
||||
|
||||
### Finding Files And Directories
|
||||
|
||||
```sh
|
||||
find [path] [expression] # search file in directory hierarchy
|
||||
find [start-position] -type f -name FILENAME # search for a file named "filename"
|
||||
find [start-position] -type d -name DIRNAME # search for a directory named "dirname"
|
||||
find [path] -exec <command> {} \; # ececute command on found items (identified by {})
|
||||
|
||||
[ -f "path" ] # test if a file exists
|
||||
[ -d "path" ] # test if a folder exists
|
||||
```
|
||||
|
||||
### Other
|
||||
|
||||
```sh
|
||||
tee # copy standard input and write to standard output AND files simultaneously
|
||||
tee [FILE]
|
||||
command | sudo tee FILE # operate on file w/o using shell as su
|
||||
|
||||
echo # display a line of text
|
||||
echo "string" > FILE # write lin of text to file
|
||||
echo "string" >> FILE # append line o ftext to end of file (EOF)
|
||||
|
||||
wget URL # download repositories to linux machine
|
||||
|
||||
curl # dovnlaod the contents of a URL
|
||||
curl [-I --head] # Fetch the headers only
|
||||
|
||||
ps [-ax] # display processes
|
||||
kill <PID> # kill process w/ Process ID <PID>
|
||||
killall PROCESS # kill process by nane
|
||||
|
||||
grep # search through a string using a REGEX
|
||||
grep [-i] # grep ignore case
|
||||
|
||||
source script.sh # load script as a command
|
||||
diff FILES # compare files line by line
|
||||
|
||||
# sudo apt install shellcheck
|
||||
shellcheck FILE # shell linter
|
||||
|
||||
xargs [COMMAND] # build and execute command lines from standard input
|
||||
# xargs reads items form the standard input, delimites by blanks or newlines, and executes the COMMAND one or more times with the items as argumests
|
||||
watch [OPTIONS] COMMAND # execute a program periodically, showing output fullscreen
|
||||
watch -n SECONDS COMMAND # execute command every SECONDS seconds (no less than 0.1 seconds)
|
||||
```
|
||||
|
||||
## Data Wrangling
|
||||
|
||||
**Data wrangling** is the process of transforming and mapping data from one "raw" data form into another format with the intent of making it more appropriate and valuable for a variety of downstream purposes such as analytics.
|
||||
|
||||
```bash
|
||||
sed # stream editor for filtering and transforming text
|
||||
sed -E "s/REGEX/replacement/" # subsitute text ONCE (-E uses modern REGEX)
|
||||
sed -E "s/REGEX/replacement/g" # subsitute text multiple times (every match)
|
||||
|
||||
wc [FILE] # print newline, word and byte countd for each file
|
||||
wc [-m --chars] FILE # print character count
|
||||
wc [-c --bytes] FILE # print bytes count
|
||||
wc [-l --lines] FILE # print lines count
|
||||
wc [-w --words] FILE # print word count
|
||||
|
||||
sort [FILE] # sort lines of a text file
|
||||
|
||||
uniq [INPUT [OUTPUT]] # report or omit repeated lines (from INPUT to OUTPUT)
|
||||
uniq [-c --count] # prefix lines w/ number of occurrences
|
||||
uniq [-d --repeated] # plrint only duplicare lines, one for each group
|
||||
uniq [-D] # plrint only duplicare lines
|
||||
|
||||
paste [FILE] # merge lines of files
|
||||
paste [-d --delimiters=LIST] # use delimiters from LIST
|
||||
paste [- --serial] # paste one file at a time instead of in parallel
|
||||
|
||||
awk '{program}' # pattern scanning and processing language
|
||||
awk [-f --file PROGRAM_FILE] # read program source from PROGRAM_FILE instead of from first argument
|
||||
|
||||
bc [-hlwsqv long-options] [FILE] # arbitrary precision calculator language
|
||||
bc [-l --mathlib] [FILE] # use standard math library
|
||||
```
|
256
Bash/Bash Scripting.md
Normal file
256
Bash/Bash Scripting.md
Normal file
|
@ -0,0 +1,256 @@
|
|||
# Bash Cheat Sheet
|
||||
|
||||
[Bash Manual](https://www.gnu.org/software/bash/manual/)
|
||||
|
||||
`Ctrl+Shift+C`: copy
|
||||
`Ctrl+Shift+C`: paste
|
||||
|
||||
## Bash Use Modes
|
||||
|
||||
Interactive mode --> shell waits for user's commands
|
||||
Non-interactive mode --> shell runs scripts
|
||||
|
||||
## File & Directories Permissons
|
||||
|
||||
File:
|
||||
|
||||
- `r`: Read. Can see file content
|
||||
- `w`: Write. Can modify file content
|
||||
- `x`: Execute. Can execute file
|
||||
|
||||
Directory:
|
||||
|
||||
- `r`: Read. Can see dir contents
|
||||
- `w`: CRD. Can create, rename and delete files
|
||||
- `x`: Search. Can access and navigate inside the dir. Necessary to operate on files
|
||||
|
||||
## File Descriptors
|
||||
|
||||
`FD 0` "standard input" --> Channel for standard input (default keyboard)
|
||||
`FD 1` "standard output" --> Channel for the default output (default screen)
|
||||
`FD 2` "standard error" --> Channel for error messages, info messages, prompts (default keyboard)
|
||||
File descriptors chan be joined to create streams that lead to files, devices or other processes.
|
||||
|
||||
Bash gets commands by reading lines.
|
||||
As soon as it's read enough lines to compose a complete command, bash begins running that command.
|
||||
Usually, commands are just a single line long. An interactive bash session reads lines from you at the prompt.
|
||||
Non-interactive bash processes read their commands from a file or stream.
|
||||
Files with a hashbang as their first line (and the executable permission) can be started by your system's kernel like any other program.
|
||||
|
||||
### First Line Of Bash
|
||||
|
||||
`#!/bin/env bash`
|
||||
Hashbang indicating whitch interpreter to use
|
||||
|
||||
### Simple Command
|
||||
|
||||
```bash
|
||||
[ var=value ... ] command [ arg ... ] [ redirection ... ] # [.] is optional component
|
||||
```
|
||||
|
||||
### Pipelines (commands concatenation)
|
||||
|
||||
```bash
|
||||
command | file.ext # link the first process' standard output to the second process' standard input
|
||||
command |& file.ext # link the first process' standard output & standard error to the second process' standard input
|
||||
```
|
||||
|
||||
### Lists (sequence of commands)
|
||||
|
||||
```bash
|
||||
command_1; command_2; ... # execute command in sequence, one after the other
|
||||
command_1 || command_2 || ... # execute successive commands only if preceding ones fail
|
||||
```
|
||||
|
||||
### COMPOUND COMMANDs (multiple commands as one)
|
||||
|
||||
```bash
|
||||
# block of commands executed as one
|
||||
<keyword>
|
||||
command_1; command_2; ...
|
||||
<end_keyword>
|
||||
|
||||
{command_1; command_2; ...} # sequence of commands executed as one
|
||||
```
|
||||
|
||||
### Functions (blocks of easely reusable code)
|
||||
|
||||
`function_name () {compound_command}`
|
||||
Bash does not accept func arguments, parentheses must be empty
|
||||
|
||||
## Command names & Running programs
|
||||
|
||||
To run a command, bash uses the name of your command and performs a search for how to execute that command.
|
||||
In order, bash will check whether it has a function or builtin by that name.
|
||||
Failing that, it will try to run the name as a program.
|
||||
If bash finds no way to run your command, it will output an error message.
|
||||
|
||||
## The path to a program
|
||||
|
||||
When bash needs to run a program, it uses the command name to perform a search.
|
||||
Bash searches the directories in your PATH environment variable, one by one, until it finds a directory that contains a program with the name of your command.
|
||||
To run a program that is not installed in a PATH directory, use the path to that program as your command's name.
|
||||
|
||||
## Command arguments & Quoting literals
|
||||
|
||||
To tell a command what to do, we pass it arguments. In bash, arguments are tokens, that are separated from each other by blank space.
|
||||
To include blank space in an argument's value, you need to either quote the argument or escape the blank space within.
|
||||
Failing that, bash will break your argument apart into multiple arguments at its blank space.
|
||||
Quoting arguments also prevents other symbols in it from being accidentally interpreted as bash code.
|
||||
|
||||
## Managing a command's input and output using redirection
|
||||
|
||||
By default, new commands inherit the shell's current file descriptors.
|
||||
We can use redirections to change where a command's input comes from and where its output should go to.
|
||||
File redirection allows us to stream file descriptors to files.
|
||||
We can copy file descriptors to make them share a stream. There are also many other more advanced redirection operators.
|
||||
|
||||
### Redirections
|
||||
|
||||
```bash
|
||||
[x]>file # make FD x write to file
|
||||
[x]<file # make FD x read from file
|
||||
|
||||
[x]>&y # make FD x write to FD y's stream
|
||||
[x]<&y # make FD x read from FD y's stream
|
||||
&>file # make both FD 1 (standard output) & FD 2 (standard error) write to file
|
||||
|
||||
[x]>>file # make FD x append to end of file
|
||||
x>&-, x<&- # close FD x (stream disconnected from FD x)
|
||||
[x]>&y-, [x]<&y- # replace FD x with FD y
|
||||
[x]<>file # open FD x for both reading and writing to file
|
||||
```
|
||||
|
||||
## Pathname Expansion (filname pattern [glob] matching)
|
||||
|
||||
`*` matches any kind of text (even no text).
|
||||
`?` matches any single character.
|
||||
`[characters]` mathces any single character in the given set.
|
||||
`[[:classname:]]` specify class of characters to match.
|
||||
`{}` expand list of arguments (applies command to each one)
|
||||
|
||||
`shopt -s extglob` enables extended globs (patterns)
|
||||
|
||||
`+(pattern [| pattern ...])` matches when any of the patterns in the list appears, once or many times over. ("at least one of ...").
|
||||
`*(pattern [| pattern ...])` matches when any of the patterns in the list appears, once, not at all, or many times over. ("however many of ...").
|
||||
`?(pattern [| pattern ...])` matches when any of the patterns in the list appears, once, not at all, or many times over. ("however many of ...").
|
||||
`@(pattern [| pattern ...])` matches when any of the patterns in the list appears just once. ("one of ...").
|
||||
`!(pattern [| pattern ...])` matches only when none of the patterns in the list appear. ("none of ...").
|
||||
|
||||
## Command Substituition
|
||||
|
||||
With Command Substitution, we effectively write a command within a command, and we ask bash to expand the inner command into its output and use that output as argument data for the main command.
|
||||
|
||||
```bash
|
||||
$(inner_command) # $ --> value-expansion prefix
|
||||
```
|
||||
|
||||
## Shell Variables
|
||||
|
||||
```bash
|
||||
varname=value # variable assignement
|
||||
varname="$(command)" # command sobstituition, MUST be double-quoted
|
||||
"$varname", "${varname}" # variable expansion, MUST be double-quoted (name substituited w/ varaible content)
|
||||
|
||||
$$ # pid
|
||||
$# # number of arguments passed
|
||||
$@ # all arguments passed
|
||||
${n} # n-th argument passed to the command
|
||||
$0 # name of the script
|
||||
$_ # last argument passed to the command
|
||||
$? # error message of the last (previous) comman
|
||||
!! # executes last command used (echo !! prints the last command)
|
||||
```
|
||||
|
||||
## Parameter Expansion Modifiers (in double-quotes)
|
||||
|
||||
`${parameter#pattern}` removes the shortest string that matches the pattern if it's at the start of the value.
|
||||
`${parameter##pattern}` removes the longest string that matches the pattern if it's at the start of the value.
|
||||
`${parameter%pattern}` removes the shortest string that matches the pattern if it's at the end of the value.
|
||||
`${parameter%%pattern}` removes the longest string that matches the pattern if it's at the end of the value.
|
||||
`${parameter/pattern/replacement}` replaces the first string that matches the pattern with the replacement.
|
||||
`${parameter//pattern/replacement}` replaces each string that matches the pattern with the replacement.
|
||||
`${parameter/#pattern/replacement}` replaces the string that matches the pattern at the beginning of the value with the replacement.
|
||||
`${parameter/%pattern/replacement}` replaces the string that matches the pattern at the end of the value with the replacement.
|
||||
`${#parameter}` expands the length of the value (in bytes).
|
||||
`${parametr:start[:length]}` expands a part of the value, starting at start, length bytes long.
|
||||
Counts from the end rather than the beginning by using a (space followed by a) negative value.
|
||||
`${parameter[^|^^|,|,,][pattern]}` expands the transformed value, either upper-casing or lower-casing the first or all characters that match the pattern.
|
||||
Omit the pattern to match any character.
|
||||
|
||||
## Decision Statements
|
||||
|
||||
### If Statement
|
||||
|
||||
Only the final exit code after executing the entire list is relevant for the branch's evaluation.
|
||||
|
||||
```bash
|
||||
if command_list; then
|
||||
command_list;
|
||||
elif command_list; then
|
||||
command_list;
|
||||
else command_list;
|
||||
fi
|
||||
```
|
||||
|
||||
### Test Command
|
||||
|
||||
`[[ arggument_1 <operator> argument_2 ]]`
|
||||
|
||||
### Arithmetic expansion and evaluation
|
||||
|
||||
`(( expression ))`
|
||||
|
||||
### Comparison Operators
|
||||
|
||||
```bash
|
||||
[[ "$a" -eq "$b" ]] # is equal to
|
||||
[[ "$a" -ne "$b" ]] # in not equal to
|
||||
[[ "$a" -gt "$b" ]] # greater than
|
||||
[[ "$a" -ge "$b" ]] # greater than or equal to
|
||||
[[ "$a" -lt "$b" ]] # less than
|
||||
[[ "$a" -le "$b" ]] # less than or equal to
|
||||
```
|
||||
|
||||
### Arithmetic Comperison Operators
|
||||
|
||||
```bash
|
||||
(("$a" > "$b")) # greater than
|
||||
(("$a" >= "$b")) # greater than or equal to
|
||||
(("$a" < "$b")) # less than
|
||||
(("$a" <= "$b")) # less than or equal to
|
||||
```
|
||||
|
||||
### String Compatison Operators
|
||||
|
||||
```bash
|
||||
[ "$a" = "$b" ] # is equal to (whitespace atoun operator)
|
||||
|
||||
[[ $a == z* ]] # True if $a starts with an "z" (pattern matching)
|
||||
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching)
|
||||
[ $a == z* ] # File globbing and word splitting take place
|
||||
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching)
|
||||
|
||||
[ "$a" != "$b" ] # is not equal to, pattern matching within a [[ ... ]] construct
|
||||
|
||||
[[ "$a" < "$b" ]] # is less than, in ASCII alphabetical order
|
||||
[ "$a" \< "$b" ] # "<" needs to be escaped within a [ ] construct.
|
||||
|
||||
[[ "$a" > "$b" ]] # is greater than, in ASCII alphabetical order
|
||||
[ "$a" \> "$b" ] # ">" needs to be escaped within a [ ] construct.
|
||||
```
|
||||
|
||||
## Commands short circuit evaluation
|
||||
|
||||
```bash
|
||||
command_1 || command_2 # if command_1 fails executes command_2
|
||||
command_1 && command_2 # executes command_2 only if command_1 succedes
|
||||
```
|
||||
|
||||
## Loops
|
||||
|
||||
```bash
|
||||
for var in iterable ; do
|
||||
# command here
|
||||
done
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue