Skip to content

Commit 544fe5c

Browse files
committed
Added more notes to the Linux Essentials
1 parent c0db973 commit 544fe5c

File tree

1 file changed

+353
-3
lines changed

1 file changed

+353
-3
lines changed

LPILinuxEssentials.md

Lines changed: 353 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ Uses a sources list located in `etc/apt/sources.list`
7575
| `dpkg --remove $package_name` | Removes Debian package |
7676
| `dpkg --purge $package_name` | Removes dependencies |
7777

78-
[Back to table of contents](#toc)
7978

8079
## <span id="cli-basics"></span>Command-line basics
8180
Shells are command-line interpreters that accept commands that are then sent to the OS kernel for processing. See list of popular shells I saved as an img on my iPad. You can use any shell installed on the computer by typing its name on the CLI.
@@ -241,7 +240,6 @@ Searches for executables and man page files
241240

242241
whereis cd
243242

244-
[Back to table of contents](#toc)
245243

246244
## <span id="cli-help"></span>Getting CLI help
247245

@@ -288,4 +286,356 @@ Searches for executables and man page files
288286
| `.htm`, `.html` | any web browser, often less |
289287
| `.odt` | LibreOffice, OpenOffice.org, any word processor |
290288
| `.pdf` | `.xpdf`, Adobe Reader |
291-
| `.tif`, `.png`., `.jpg` | Gimp
289+
| `.tif`, `.png`., `.jpg` | Gimp
290+
291+
## <span id="linux-file-system"></span>The Linux file system
292+
The Linux file system and the file system hierarchy standard (FHS)
293+
294+
- The Linux file system uses a hierarchy structure to organize data.
295+
- Linux systems have a standard set of subdirectories at the root.
296+
297+
| directory | description |
298+
| :--- | :--- |
299+
| bin | executables necessary to run the OS |
300+
| boot | bootloader files to boot Linux |
301+
| dev | devices that send/receive data sequentially (printers/mice); devices that are block-oriented (HDs, flash drives) |
302+
| etc | text-based config files used by the system |
303+
| home | home folders for users |
304+
| lib -> usr/lib | code libraries for programs in the bin or sbin directories |
305+
| lib64 | 64-bit libraries |
306+
| media | used by some distros to mount external devices |
307+
| mnt | used by some distros to mount other external devices |
308+
| opt | contains files for programs you can install manually |
309+
| proc | pseudo file system for processes |
310+
| root | root user's home directory |
311+
| sbin | mgmt and config files |
312+
| srv | where services save their files (e.g., httpd) |
313+
| sys | hardware within system |
314+
| tmp | temporary files created by file system |
315+
| usr | application files |
316+
| var | Linux variable data and log files |
317+
318+
### <span id="file-system"></span>Disk file systems
319+
- __ext2:__ second extended file system. Data is stored in hiearchical fashion (dirs/files). 2 TB is max file size.
320+
- __ext3:__ updated version of ext2. Adds journaling, which records transactions. If the system restarts uncleanly, the entire file system doesn't need to be checked entirely as with ext2.
321+
- __Reiser:__ similar to ext3 in that journaling makes recovery much quicker.
322+
- __ext4:__ updated version of ext3.
323+
324+
325+
## <span id="files"></span>Files and directories
326+
- `touch -d "February 1 2017" file.txt`: Allows you to specify the modification timestamp.
327+
- `mkdir -p newdir/newsubdir/newsubdir2`: Create a directory and its parents if they don't exist.
328+
- `rmdir $dir`: Can only delete empty directories
329+
- `cp -puR srcfile dstfile`:
330+
+ __`p`__ = preserve original ownership
331+
+ __`u`__ = update (only if src is newer or dst doesn't exist)
332+
+ __`R`__ = recursive
333+
- `mv srcfile directory/`: Move/rename file to indicate directory
334+
335+
336+
## <span id="archives-compression"></span>Archives and compression
337+
338+
### <span id="archives"></span>Archives
339+
- __tar:__ Stands for “tape archive.” Combines files but doesn’t compress them. You can pass flags to both archive and compress, since Gzip and Bzip2 are for compressing only. **Note that the order of the flags matters for `tar` command.**
340+
+ `-c` = create archive
341+
+ `-f` = read the archive from or write the archive to the specified file
342+
+ `-t` = list archive's content w/o extracting it
343+
+ `-x` = extract tarball
344+
+ `-v` = verbose output (lists files extracted)
345+
+ `-z` = compress using gzip
346+
+ `-j` = compress using bzip2 (like gzip but more resources intensive)
347+
348+
__Archive (no compression)__
349+
350+
| Command | Notes |
351+
| :--- | :--- |
352+
| `tar -cf tarball.tar dir-to-tar` | creates the tarball from dir-to-tar directory |
353+
| `tar -cf tarball.tar file1 file2` | creates the tarball from files indicated |
354+
355+
__Unarchive (no compression)__
356+
357+
| Command | Notes |
358+
| :--- | :--- |
359+
| `tar -tf tarball.tar` | show contents of tarball w/o unarchiving |
360+
| `tar -xf tarball.tar` | extract tarball |
361+
| `tar -xvf tarball.tar` | extract tarball, verbose |
362+
363+
__Archiving with compression__
364+
365+
While not required, it's best practice to indicate the compression used as part of the file name.
366+
367+
| Command | Notes |
368+
| :--- | :--- |
369+
| `tar -czf tarball.tar.gz dir-to-tar` | use gzip to compress |
370+
| `tar -cjf tarball.tar.bz2 dir-to-tar` | use bzip2 to compress |
371+
372+
__Unarchiving compressed tarballs__
373+
374+
| Command | Notes |
375+
| :--- | :--- |
376+
| `tar -xzf tarball.tar.gz` | extract compressed gzipped archive |
377+
| `tar -xjvf tarball.tar.bz2` | extract compressed bzip2 archive, verbose |
378+
379+
380+
### <span id="compression"></span>Compression
381+
- __zip:__ Like in Windows. It's the only command that both compresses and archives. It's also the simplest b/c it has few options.
382+
+ Examples: Compress with `zip` and extract with `unzip`
383+
* `zip file.zip file1 [file2]` = create a zipped archive of the specified files
384+
* `zip -r file.zip dir-to-zip`
385+
- `-r` is required to go into directories and zip their contents, or else you'll zip an empty directory
386+
* `unzip file.zip`
387+
- __gzip:__ Compressed file format. Not for archiving, but for compressing.
388+
+ Compress with `gzip` and extract with `gunzip`.
389+
+ File extension is __.gz__.
390+
+ __Note:__ Gzip deletes the original unless you pass the `-c` flag, or the `-k` flag for "keep."
391+
+ Examples:
392+
* `gzip file.tar` = will replace original file
393+
* `gzip -c file.tar > file.tar.gz` = will keep original file and create file.tar.gz
394+
* `gzip -k file.tar` = same as above
395+
* `gunzip file.tar.gz` = uncompress file
396+
- __bzip2:__ Better than Gzip, especially for bigger files. Not for archiving, but for compressing.
397+
+ Compress with `bzip2` and extract with `bunzip`. File extension is __.bz2__
398+
+ Examples:
399+
* `bzip2 file.tar`
400+
* `bunzip2 file.tar.bz2`
401+
402+
403+
## <span id="searching"></span>Searching for and extracting data from files
404+
405+
### <span id="viewing-text"></span>Viewing text
406+
407+
| Command | Purpose |
408+
| :--- | :--- |
409+
| `cat` | display contents of a file |
410+
| `less -M` | reads file with pagination. <br><br>use `/` to search fwd and `?` to search backwards<br><br> `-M` shows lines you're reading and total, plus percentage <br><br>`-N` shows line numbers on the left <br><br> `G` goes to beginning and `shift` + `G` goes to end |
411+
| `head` | read first 10 lines of a file. `-n $num_lines` |
412+
| `tail` | read last 10 lines of a file; `-f` = follow |
413+
| `find` | locates file on system<br><br>`find . -type d` find directories<br>`find . -type f` find files<br>`find . -iname "file*.` allows globbing |
414+
415+
### <span id="analyzing-text"></span>Analyzing text
416+
- `grep`: searches for a string; allows globbing<br>
417+
+ `-r` = recursive
418+
+ `-i` = case insensitive
419+
+ `-n` = show line numbers
420+
+ `-w` = expression is searched for as word
421+
+ Examples:
422+
* `grep ^Sirloin file1.txt`
423+
* `grep -i dhcp /var/log/messages`
424+
* `grep -n dhcp /var/log/messages`
425+
* `grep -rnw '.' -e 'domo'` searches all files in the current folder for the expression
426+
- `sort`: sorts text alphabetically
427+
+ `-r` = reverse alphabetically
428+
- `cut`: Remove text from file and print specified fields to stdout
429+
+ `-d` = delimiter. E.g., `-d" "` = space character as the delimiter
430+
+ `-f` = which field to print (based on the delimiter)
431+
+ Examples:
432+
* `cut -d" " -f 6-` = start from field 6 through EOL
433+
- `wc`: word count. Note that you can specify multiple files.
434+
+ `-w` = words
435+
+ `-l` = lines
436+
+ `-c` = chars
437+
438+
### <span id="pipes"></span>Pipes and regular expressions
439+
You can pipe the output of one command as the input for another command:
440+
441+
grep -i republic plato_republic.txt | less
442+
grep -i republic plato_republic.txt | wc -w
443+
444+
#### Regular expressions for `grep` command
445+
446+
| Expression | Description | Example |
447+
| :--- | :--- | :--- |
448+
| * | 0+ repeats of preceding character string or regex | `file*` |
449+
| . | any single char (grep) | `.cc` |
450+
| ? | 0+ of proceeding chars | `f?le` |
451+
| ^ | appears at beginning | `^.b` |
452+
| $ | appears at end | `^...$` 3 chars |
453+
| `\b<needle>\b` | word boundary (must match exactly) | `\bwww\b` |
454+
| [nnn] | one char btw braces | `[abc]` |
455+
| [^nnn] | no chars btw braces | `[^abc]` |
456+
| [a-z] | any single char in range | `[a-x]` |
457+
| [1-90] | any digit between 1-9, and 0 | `` |
458+
459+
### <span id="redirection"></span>I/O Redirection
460+
461+
#### Redirecting output
462+
Output is normally displayed on the screen but can be redirected to files or to other commands as input.
463+
464+
tail /var/log/messages > logtemp.txt # redirect stdout
465+
tail /var/log/messages 1> logtemp.txt # same as above
466+
tail /var/log/messages >> logtemp.txt # append
467+
468+
cat bogusfile.txt 2> errors.txt # redirect stderr
469+
cat bogusfile.txt 2>> errors.txt # append
470+
471+
command 1> outfile.txt 2> errfile.txt # redirect to separate files
472+
473+
474+
## <span id="scripting"></span>Turning commands into a script
475+
476+
### <span id="text-editing"></span>Basic text editing
477+
478+
#### nano
479+
- `ctrl` + `k`: cut line
480+
- `ctrl` + `u`: paste line
481+
- `ctrl` + `w`: search for text
482+
- `ctrl` + `t`: spell check
483+
- `ctrl` + `\`: find and replace
484+
- `ctrl` + `g`: view help
485+
- `ctrl` + `x`: exit
486+
487+
#### vim
488+
- `vimtutor` = built in tutorial from beginner to advanced
489+
- Insert mode via `i`, `INSERT`, `s`, `o`, `a`
490+
- There are command mode adn command line mode. Enter command line mode from command mode via `:`
491+
+ `v` = enter visual mode. `V` = highlights the line; `ctrl` + `V` = visual block
492+
* `y` = "yank" or copy highlighted text
493+
+ `p` = "put" or paste text
494+
+ `shift` + `a` = append text at end of line
495+
+ `u` = undo last change
496+
+ `h` = move left
497+
+ `j` = move down
498+
+ `k` = move up
499+
+ `l` = move right
500+
+ `dw` = delete word under cursor
501+
+ `dd` = deline line under cursor (5dd = delete 5 lines)
502+
+ `shift` + `g` = go to bottom of file
503+
+ `gg` = go to top of file
504+
+ `:w` = write to disk
505+
+ `:wq` or `x` = write to file and quit
506+
+ `q!` = quit without saving
507+
508+
### <span id="shell-scripting"></span>Shell scripting
509+
- `#!/bin/bash` = specify an interpreter, (called the shebang)
510+
- __Arguments:__ $1 -> first arg, $2 -> second arg, $? -> exit code/status
511+
- __`&&`:__ execute command 2 only if command 1 exits normally
512+
- __`||`:__ execute command 2 only if command 1 exits abnormally
513+
- You can combine `&&` and `||` as such:<br>`rm file1.txt && echo "file deleted" || echo "file not deleted"`
514+
- When scripts are first created, they are not executable. Fix this with `chmod +x <file name>`
515+
516+
#### Basic if statement
517+
518+
if [ condition ]
519+
then
520+
command
521+
fi
522+
523+
524+
# example
525+
526+
if ["1" == "1"]
527+
then
528+
echo "They are the same"
529+
fi
530+
531+
#### Basic else statement
532+
533+
if [ condition ]
534+
then
535+
command
536+
else
537+
command
538+
fi
539+
540+
541+
# example
542+
543+
if [ "$PWD" == "$HOME" ]
544+
then
545+
echo "You are home."
546+
else
547+
echo "You are in $PWD."
548+
549+
#### Loops
550+
551+
for i in {1..10}
552+
do
553+
echo "$i"
554+
done
555+
556+
#### Example bash script 1
557+
558+
```bash
559+
#!/bin/bash
560+
561+
# My daily routine script
562+
563+
# If user enters "day", show calendar and date
564+
SHOWDAY=$1
565+
566+
if [ "$1" == "day" ]
567+
then
568+
# Display the calendar
569+
cal
570+
571+
# Display the date/time in UTC
572+
date -u
573+
fi
574+
575+
# Daily greeting
576+
printf "\nHello there, $LOGNAME.\n\n"
577+
578+
if [ "$PWD" == "$HOME" ]
579+
then
580+
echo "You are home."
581+
else
582+
printf "You are in $PWD.\n\n"
583+
fi
584+
585+
# Show us what we have to work on today
586+
DOCUMENTS="/Users/jmartinez/Downloads/linux-essentials-practice/text-analysis"
587+
588+
for doc in "$DOCUMENTS"/*.txt
589+
do
590+
echo "$doc"
591+
done
592+
```
593+
594+
#### Example bash script 2
595+
Create the script to set variable values based on args
596+
597+
```bash
598+
#! /bin/bash
599+
# list contents of a dir and write the output to a file
600+
# usage: ./findlist.sh $LOCATION $FILENAME
601+
602+
LOCATION=$1
603+
FILENAME=$2
604+
605+
if [ -z "$LOCATION" ]
606+
then
607+
echo "Please provide location argument"
608+
exit 0
609+
fi
610+
611+
if [ -z "$FILENAME" ]
612+
then
613+
echo "Please provide a filename"
614+
exit 0
615+
fi
616+
617+
ls -lh $LOCATION > $FILENAME
618+
echo
619+
echo "Script is complete and indexed $LOCATION."
620+
echo
621+
echo "###########################"
622+
echo "Displaying contents of $FILENAME"
623+
echo "###########################"
624+
cat $FILENAME
625+
```
626+
627+
628+
## <span id="linux-os"></span>The Linux operating system
629+
630+
### <span id="linux-diffs"></span> Windows, Mac, and Linux differences
631+
- Windows has a lot of proprietary software and active directory.
632+
- Apple uses proprietary hardware and software
633+
- It's now easier to switch to Linux b/c many applications are web based.
634+
- CLI: Windows has PowerShell and macOS doesn't have a CLI-only mode
635+
636+
### <span id="linux-lifecycle"></span> Linux lifecycle management
637+
- Design
638+
- Develop
639+
- Deploy
640+
- Manage
641+
- Retire

0 commit comments

Comments
 (0)