touch file.ext -- creates a file.ext in current folderwhich execuatable -- returns where the executeable is being picked up from aka the path of the executableecho "string" -- prints the stringchmod u+x file.ext sets the file permission to executable for user#!/bin/bash
greeting=hellow
name=tux
echo $greeting $name
+, -, *, /, **, %
| operator | usage |
|---|---|
| + | addition |
| - | substraction |
| * | multiplication |
| / | division |
| ** | exponentioation |
| % | modulus |
$((expression))
var=$((10**2)) 10 pow 2
Above method fractions are not calcualted correctly for that use bc command which stands for bash calculator
echo "scale=2;22/7" | bc # scale sets number of decimal points
# 3.14
read variable_name
#!/bin/bash
echo "Enter a number"
read a
echo "you've entered $a"
| Operator | syntax |
|---|---|
| equality | -eq |
| greater than | -gt |
| greater than or equal to | -ge |
| less than | -lt |
| less than or equal to | -le |
| not equal | -ne |
Remember always use single square bracket don't use double even if other tutorials say so as double square bracket isn't compatible with all shells.
Important parts to note are, bash is space sensitive aka remember this structure. if followed by space followed by a square bracket followed by space then statement then space the square bracket close then semicolon, same with elif miss this structure your conditional won't work !!
if [ condition ];
then
statement
elif [ condition ]; then
statement
else
statement
fi
With numbers
!/bin/bash
for i in {1..5}
do
echo $i
done
with strings
#!/bin/bash
for i in a b c
do
echo $i
done
#!/bin/bash
i=1
while [ $i -lt 10 ];
do
echo $i
done
cat file.name more file.name
$@ will contain arguments for the script
for x in $@
do
echo $x
done
Runs a command based on the intervel provided
intervel structure is as follows
[minute|Range 0-59] [hour| Range 0-23] [day| range 0-31] [month| range 0-12] [weekday| range 0-6, 0 means sunday] additionally use * to say any
run crontab -l to list all scheduled tasks
Find is a command that is used to find files in the local directory
find . -type f -name "*.sh" -exec mv {} new/ \; -size +1M -user tux -group tux -perm 777 -mtime +1 -atime -1 -ctime 1
Where parameters are as follows
| Parameters | Description |
|---|---|
| -type | File type, possible options are f for file, d directory, l symoblic link, c character devices aka keyboard/mouse, b block devices aka cd drive, rom, usb etc |
| -name | Name of the file |
| -size | Size of the file |
| -user | Owner of the file |
| -group | Group of the file |
| -perm | Permission of the file |
| -mtime | Modification time of the file, +X means modified X days ago, -X means modified within X days, X means exactly X days, X is an integer |
| -atime | Access time of the file, +X means accessed X days ago, -X means accessed within X days, X means exactly X days, X is an integer |
| -ctime | Creation time of the file, +X means created X days ago, -X means created within X days, X means exactly X days, X is an integer |
| -exec | Execute a command on the file, used as follows -exec mv {} new/ \; subsitutes file name in {} |
Cut is a command that's used to extract substring of a string passed to it. Apart from that it also has features like selecting specific columsn of a csv with any delimiter.
echo "string" | cut -c 1-3 returns str
Here's how to give a range, note that counting starts with 1 aka one indexed
| Range | Description |
|---|---|
| 1-10 | return range [1,10] |
| -10 | return range [,10] aka start to 10 |
| 10- | return range [10,] aka 10 to end |
| 10- | return range [10,] aka 10 to end |
| 1,3,5 | return the entries at positions {1,3,5} |
Additional options
| Options | Description |
|---|---|
| -b | for each line in the bytes passed, print characters in the range passed after this flag cut -b <range> |
| -c | for each line in the characters passed, print characters in the range passed after this flag cut -c <range> |
| -d | pass in the delimiter to use |
| --fields | Return only specified fields from the range, use with delimeters `cat test.csv |
Head prints first n lines or n bytes based on the input passed
head -c 3 "string" returns str
Additional options
| Options | Description |
|---|---|
| -n | returns first n lines, head -n 3 file.ext |
| -c | returns first n characters head -c 3 file.ext |
Usage: tail [OPTION]... [FILE]...
Tail prints last n lines or n bytes based on the input passed
tail -c 3 "string" returns str
Additional options
| Options | Description |
|---|---|
| -n | returns last n lines, tail -n 3 file.ext |
| -c | returns last n characters tail -c 3 file.ext |
| -f | follow the file, aka if text/logs are being appended to the file keep showing them |
Usage: tr [OPTION]... SET1 [SET2]
This maps one set of characters to another set of characters and returns it. It also squeezes and deletes characters. Squeeze means if we have repeating characters in a string it keeps only one character
| Options | Description |
|---|---|
| -c | use the complement of SET1 |
| -d | delete characters in SET1, do not translate |
| -s | replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character |
| -t | first truncate SET1 to length of SET2 |
SETs are specified as strings of characters. Most represent themselves.
Interpreted sequences are:
| Sequence | Description |
|---|---|
| \NNN | character with octal value NNN (1 to 3 octal digits) |
| \ | backslash |
| \a | audible BEL |
| \b | backspace |
| \f | form feed |
| \n | new line |
| \r | return |
| \t | horizontal tab |
| \v | vertical tab |
| CHAR1-CHAR2 | all characters from CHAR1 to CHAR2 in ascending order |
| [CHAR*] | in SET2, copies of CHAR until length of SET1 |
| [CHAR*REPEAT] | REPEAT copies of CHAR, REPEAT octal if starting with 0 |
| [:alnum:] | all letters and digits |
| [:alpha:] | all letters |
| [:blank:] | all horizontal whitespace |
| [:cntrl:] | all control characters |
| [:digit:] | all digits |
| [:graph:] | all printable characters, not including space |
| [:lower:] | all lower case letters |
| [:print:] | all printable characters, including space |
| [:punct:] | all punctuation characters |
| [:space:] | all horizontal or vertical whitespace |
| [:upper:] | all upper case letters |
| [:xdigit:] | all hexadecimal digits |
| [=CHAR=] | all characters which are equivalent to CHAR |
Translation occurs if -d is not given and both SET1 and SET2 appear. -t may be used only when translating. SET2 is extended to length of SET1 by repeating its last character as necessary. Excess characters of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to expand in ascending order; used in SET2 while translating, they may only be used in pairs to specify case conversion. -s uses the last specified SET, and occurs after translation or deletion.
Write sorted concatenation of all FILE(s) to standard output.
sort [OPTION]... [FILE]...
sort [OPTION]... --files0-from=F
With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. Ordering options:
| option | long option | description |
|---|---|---|
| -b | --ignore-leading-blanks | ignore leading blanks |
| -d | --dictionary-order | consider only blanks and alphanumeric characters |
| -f | --ignore-case | fold lower case to upper case characters |
| -g | --general-numeric-sort | compare according to general numerical value |
| -i | --ignore-nonprinting | consider only printable characters |
| -M | --month-sort | compare (unknown) < 'JAN' < ... < 'DEC' |
| -h | --human-numeric-sort | compare human readable numbers (e.g., 2K 1G) |
| -n | --numeric-sort | compare according to string numerical value |
| -R | --random-sort | shuffle, but group identical keys. See shuf(1) |
| --random-source=FILE | get random bytes from FILE | |
| -r | --reverse | reverse the result of comparisons |
| --sort=WORD | sort according to WORD: general-numeric -g human-numeric -h, month -M, numeric -n, random -R, version -V | |
| -V | --version-sort | natural sort of (version) numbers within text |
| --batch-size=NMERGE | merge at most NMERGE inputs at once; for more use temp files | |
| -c | --check, --check=diagnose-first | check for sorted input; do not sort |
| -C | --check=quiet, --check=silent | like -c, but do not report first bad line |
| --compress-program=PROG | compress temporaries with PROG; decompress them with PROG -d | |
| --debug | annotate the part of the line used to sort, and warn about questionable usage to stderr | |
| --files0-from=F | read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input | |
| -k | --key=KEYDEF | sort via a key; KEYDEF gives location and type |
| -m | --merge | merge already sorted files; do not sort |
| -o | --output=FILE | write result to FILE instead of standard output |
| -s | --stable | stabilize sort by disabling last-resort comparison |
| -S | --buffer-size=SIZE | use SIZE for main memory buffer |
| -t | --field-separator=SEP | use SEP instead of non-blank to blank transition |
| -T | --temporary-directory=DIR | use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories |
| --parallel=N | change the number of sorts run concurrently to N | |
| -u | --unique | with -c, check for strict ordering; without -c, output only the first of an equal run |
| -z | --zero-terminated | line delimiter is NUL, not newline |
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given, use the entire line as the key. Use --debug to diagnose incorrect key usage.
SIZE may be followed by the following multiplicative suffixes: % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.
returns unique characters
uniq [OPTION]... [INPUT [OUTPUT]]
| option | long option | description |
|---|---|---|
| -c | --count | prefix lines by the number of occurrences |
| -d | --repeated | only print duplicate lines, one for each group |
| -D | print all duplicate lines | |
| --all-repeated[=METHOD] | like -D, but allow separating groups with an empty line; METHOD={none(default),prepend,separate} | |
| -f | --skip-fields=N | avoid comparing the first N fields |
| --group[=METHOD | show all items, separating groups with an empty line; METHOD={separate(default),prepend,append,both} | |
| -i | --ignore-case | ignore differences in case when comparing |
| -s | --skip-chars=N | avoid comparing the first N characters |
| -u | --unique | only print unique lines |
| -z | --zero-terminated | line delimiter is NUL, not newline |
| -w | --check-chars=N | compare no more than N characters in lines |