An online markdown blog and knowledge repository.
This page was created to store random notes, throughts, and snippets regarding:
The primary resource for these notes was through reading Ryans Tutorials.
NOTE: I am no longer making any effort to verify any of these work in Windows PowerShell, as I have moved to a pure-Linux workstation.
Many of these concepts will also apply to Unix and other unix-based operating systems.
The command line, or Shell, is the text-only I/O mechanism for humans telling Linux what to do, and for Linux to tell human operators what it is doing or has done (or nothing at all, just a prompt awaiting input).
On my system I am using Oh My ZSH! which is a bash knock-off with lots of interesting themes that make bashing-around a little easier (for example: using Git) and fun (colors, layouts, fonts, markers, etc).
Despite having an "add on shell" like ZSH, I can still "drop to a Bash" shell with the command bash
(Bourne Again SHell).
I could also use sh
but since ZSH is installed, it really just loads ZSH again.
If I was in a remote terminal session I could force-load ZSH with sh
or zsh
at the prompt and it would work.
In Windows, I stick with PowerShell because it has much greater capability than 'cmd.exe', and can be well-integrated into the OS and to apps like Visual Studio.
.txt
is usually associatd with Notepad.exe
and is of FileTyle text
or plaintext
.file_name.txt
is not the same as FileName.txt
.Quote Around Spaces or get frustrated (your choice).
'
or double "
quotation marks to identify a file or folder name that contains a space.
ls spacey folder name
returns an error.ls 'spacey folder name'
works.Use a backslash \
to escape (nullify) the special meaning of reserved characters like spaces e.g.:
user@bash: pwd
/home/toor
user@bash: cd My\ Music
user@bash: pwd
/home/toor/My Music
Invoke man
command followed by the command name you want information about to get an on-screen instruction MANual.
Exit MAN pages by pressing q
on the keyboard.
To search all MAN pages from Terminal: man -k <search_term>
To search terms WITHIN a MAN page display: / <search_term>
then press n
for Next page.
man
clears the screen on exit. You can keep the output in the terminal scrollback using less. [Bellingham Codes participant: Remington]
For example, I want to see a section of MAN pages about SSH:
man -P "less -X" ssh
To execute a script in a different directory: sh> (cd /dir/where/script/is/ ; sh script-name.sh [args])
Doing this will contain the script execution within the child SH process, and if the script forces closure at the end, you won't lose your existing SH session.
Remember, EVERYTHING is a file so <pathspec>
actually means filename
or foldername
or foldername/filename
etc.
List files in current directory: ls [options] <filespec>
List files in a list with file details (long listing) within the '/etc/ directory: ls -l /etc
List hidden files: ls -a
There are MANY options, see the LS MAN pages for more.
Just prefix the file or folder name with a dot:
my_configurations
can be made hidden by renaming to .my_configurations
config
can be made hidden by renaming to .config
Use
ls -a
to list hidden files in a directory.
Create a directory: mkdir <name>
Create a directory tree: mkdir <parent_name>/<child_name>
Remove files and empty directories: rm [options] <filespec>
Recursively remove files and directories: rm -r <filespec>
Remove an empty directory: rmdir <name>
Create a new blank file: touch <path>/<filename>[.ext]
Copy a file or directory: cp [options] <source_path> <dest_path>
Recurse over directories: cp -r <source_path> <dest_path>
Move/Rename files or directories: mv [options] <source_path> <dest_path>
You can move directories without using recuse option.
Note: Moving directories will rename if paths are the same except:
Bulk-copy files and rename them, but file extension: basename -s .jpg -a *.jpg | xargs -n1 -i cp {}.jpg {}_original.jpg
PATHs can be absolute or relative.
Linux file strucuture is hierarchical.
Folders ARE files, but with a 'd' flag set, so folders and files are part of the path.
Absolute paths start with a forward slash /
e.g.: /etch
Relative paths identify a location 'relative' to the pwd and will not begin with a forward slash i.e. ../project/code401
is one folder 'back' from pwd and one folder 'forward' from the 'project' folder.
Home path is ~
which is equivalent to \home\$username
Zero or more characters: *
Single character: ?
A range of characters: []
List files in the pwd whose 1st character is a 'Q' or 'q', 2nd character is within the range 0-9, 3rd character is within the range 0-3, and any number of other character following: ls [Qq][0-9][0-3]*
Not: ^
Note about Not: As the first character within a range wildcard causes the filter to eliminate files that match that first character range wildcard characters.
Remember: Wildcards affect the entire pathspec, which includes directories and filenames and file extensions.
My favorite when in Bash/ZSH.
Simple, to the point, plain-text editor.
Search exists but is limited.
A much more capable text editor.
Should be kept in mind for driving around text files in a native Linux viewer/editor.
All CLI, no UI.
Two modes:
i
to change to INSERT mode and --INSERT--
will be displayed at bottom of screen.[esc]
to change to EDIT mode.Launch VI: vi <filename>
<filename>
does not exist already.Command usage:
Commands starting with a colon require pressing [Enter]
to execute them.
Save and Close (multple options):
ZZ
:q!
:w
:wq
:set nu
Advice:
Display all contents of a file: cat <filespec>
Displays file contents.
View portion of a file: less <filespec>
Use arrow keys to scroll up and down, b
to go "back a page", and q
to quit.
Use tail <filespec>
to get a quick 'end-of-the-file' view. tail -n NUM
to show a specific number of lines at the end of the file.
Grep is used to filter data for viewing: grep
Note: See more grep usage in Grepping
Redirect Standard Out (STDOUT) to a file: >
Append STDOUT to the end of a file: >>
Redirect Standard Error Out (STDERR) to a file: 2>
Pass contents of a file to a program as Standard Input (STDIN): <
Feed STDOUT of the program on the left, as STDIN to the program on the right: |
When multiple piped commands all require 'sudo', add it to the commands at the start of each pipe (see example below).
# the following will fail due to lack of permissions
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /usr/share/keyrings/adoptium.asc
# the following will succeed
sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /usr/share/keyrings/adoptium.asc
Use semicolons to chain commands together.
Essentially, it will run commands, in order, immediately after each previous command returns.
Does not conditionally manage any previous command's succees (or failure) code e.g. 0 (pass) or non-0 (fail).
See this StackOverflow question and its responses for more
Run grep with extended regex: egrep
or grep -E
Ignore case: -i
Return NON MATCHING lines: -v
Select only whole-word matches: -w
Print count of matching lines: -c
Print name of each file containing the match: -l
(normally used when grep is invoked with wildcards in file arg)
Print the number before each line that matches: -n
Recursive (all files in given pathspec): -r
BEFORE context and AFTER context: -B 4 -A 4
prints 4 lines prior-to and following the match.
Regex: Follow standard regex rules, including Multipliers and Anchors.
Note: Check out regularexpressions101 for an easy tool to test RegEx before you 'buy' results. :-)
When displaying or searching file contents, it can be helpful to filter the results to minimize clutter.
Show the first n lines: head -n number
Show the last n lines: tail -n number
Sort lines in a given way: sort
Word Count, including characters and lines: wc
Search for a given pattern: grep
Developed by Apple Inc. for macOS and *nix OSes.
Cannonical includes it with Ubuntu 18.x and above.
Check out the Web GUI for administration.
Check version and architecture: dpkg -l cups-browsed
View device info: lpstat -v
Note: At midnight the CUPS Logging mechanism 'rolls over' and a CUPS reset is initiated, which causes a popup notification every night at midnight. There is a way to work around this (basically stop forced CUPS daemon restart) but there is no harm in allowing the restart to be executed.
Linux permissions place rules on what can be done with a file:
Read: r
Write: w
Exectute: x
Linux defines three groups that permissions can be applied to:
Show permissions with ls -l <pathspec>
Result example: -rwxr----x 1 pi owner 1.2K Jan 1 00:00 /home/pi/file.txt
Dissected, from left to right:
-
, Directory = d
, so this is a FILE.Change Permissions on a file (meaning everything): chmod [permissions] [filespec]
Get version (long): uname
Get specific Linux kernel version and type: uname -sr
Environment variables like PATH=
are stored in /etc/environment
, as well as .zshrc
and .config
files, often in the user profile area (but there could be others).
update-alternatives --list java
displays path to java JDK.printenv
printenv VAR_NAME
e.g. printenv JAVA_HOME
echo $PATH
= Update $PATH with a new entry: export PATH=$PATH:/opt/package/example/bin
zsh
or another package to do it for you..bashrc
: Contains bash configuration and environment variables, command history, and prompt customization like colors and content.
function_name() { commands... }
.PS1
: Defines the properties of the 'default prompt'.\[\033[01;32m\]
. I.e.: echo -e '\033[1;33mThis is yellow text\033[0m
.!
to a bash configuration like PS1
, it might be required to use single-quote characters '
instead of double-quote characters "
, because !
(and many others) have special meaning and not considered text.To include the current Git branch in the prompt:
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
(no return statement or semicolon necessary).PS1
definition line like: ...\[033[1;33m\]$(branch_function)\[...
.Loading variables in the current context becomes the 'parent context' set of variables.
If the parent (e.g. ZSH) launches another shell, its variables are then pushed to the child process.
To force specific variables into the child process, bash for example: zsh> VAR_ONE="Foo" VAR_TWO="BAR" sh
would load child process 'sh' with those two additional enironemnt variables preceeding it.
Configure aliases to common commands, scripts, and executables to make your Linux SH (ZSH etc) life simpler.
To see a list of existing aliases in shell: alias
ZSH Aliasing:
Add an alias for IntelliJ IDEA:
alias idea="~/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/nnn.mmmm.vv/bin/idea.sh
idea .
and your project will open.Add an alias for AndroidStudio:
androidstudio
alias androidstudio=$STEP_1_PATH`androidstudio .
and your project will open.Installing JetBrains Toolbox before installing Android Studio or IDEA and use that to install those (and any other IDEs), then follow these steps to manual set up idea .
or studio .
to launch from current directory:
Shell scripts location
The Alias for IDEA would look like:
set-alias -Name "idea" -Value "$env:userprofile\AppData\Local\JetBrains\Toolbox\scripts\idea.cmd"
alias idea={path}
Beyond creating, copying, moving, and deleting files, use these commands to manage the file system and disks.
Find the size of directories in pwd: du -sh ./*
Disk space utilization report: df -h
Find files modified within last 24 hours in a specified directory: find $directory -mtime -1
Which: Finds where a command lives which code
WhereIs: Like 'which' but includes lib and man file locations: whereis code
Search for files based on many properties: Modified, permissions, owners, symlinks, directory, filename, and rwx permissions.
find . -name 'my_file.txt' -type f # find 'my_file.txt' file starting from here
find /home/username/Downloads -name '*tar*' -type f # find files containing 'tar' in name starting in Downloads
find . -name "jdk" -type d # find jdk directory starting from here
List PCI Hardware/Driver info: lspci
or lspci -k
or lspci -nnk
Check LSB Module State: lsb_release -a
Report on hardware driver info: sudo lshw
Get IP info from an interface: ip a
Get network driver info: inxi -n
Many system program logs are stored in /var/log
Device Message Logs: sudo dmesg
Messages in Logs related to network(s): sudo dmesg | grep iwl
Network Manager Log: sudo journalctl -b 0 -u NetworkManager
Cancel a running operation: [CTRL] + C
Cancel a process by its ID: kill $process_id
Force canceling a 'stubborn' process: kill $process_id -9
Show list of running processes and IDs: ps
Put currently running process onto a background (paused) thread: [CTRL] + z
List current background processes: jobs
Move background process to foreground: fg $job_number
List ALL services, running AND not running: service --status-all
Use grep to find specific not/running: service --status-all | grep '\[+\]'
List services using systemctl: systemctl list-units
Check a specific service state using ps: ps -ef | grep postgres
Config files are generally stored in /etc
Commonly used program binaries are stored in /bin
Other program binaries, perhaps related to users (rather than system) are stored in /usr/bin
Reinstall Ubuntu package 'linux-firmware': sudo apt-get install --reinstall linux-firmware
Apt is the primary software manager utility: apt get install $package
DPKG is the underlying package management system: dpkg -i $deb_package
As stated at AskUbuntu.com: dpkg => apt-get, aptitude => Synaptic, Software Center
The Local Apt Repo is usually stored in /etc/apt/sources.list.d/pgdg.list
An example repo update will look something like this (for postgresQL):
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Common steps to update Apt and Install from new sources:
Installed packages will probably have an entry in $PATH that should be interrogated.
Listing and Removing packages might leave behind $PATH entries.
List existing packages: dpkg -l $package_name_in_single_quotes
Remove a package: dpkg -r $package_name
Purge remaining package artifacts: dpkg -P $package_name
Check out yarnpkg.com
Also Linuxize.com
Ensure you are in the project root.
Install globally: npm install --global yarn
Check version: yarn --version
(currently 1.22.19)
New Project: yarn init [project-name]
Add dependency:
yarn add [package]
>yarn add [package]@[version]
>yarn add [package]@[tag]
Upgrade: yarn upgrade [package]
Remove dependency: yarn remove [package]
Install/upgrade all dependencies: yarn install
Help file usage template: convert [options ...] file [[options ...] file ...] [options ...] file
More information can be found at legacy.imagemagick.org/Usage/.
Convert Quality: convert $input_file_path.img -quality nn $output_file_path.img
changes compression level on jpeg/miff/png.
Resize: convert -adaptive-resize 200x200
adjusts width and height using 'mesh interpolation' (limit to less than 50% larger or smaller otherwise use -resize
).
Scale an image: convert -scale ${percentage}%
. A simplified version of -resize
.
Compress Image: convert -compress type
Add a Caption: Consider creating a label instead. See the Usage link above for details.
Vertical or horizontal flip: convert -flip
or convert -flop
, respectively.
NOTE: This Section Is Under Development
Tar
Tempate (from tar --usage): `tar [options] -C DIR -f ARCHIVE [--exclude=PATTERN]
Template: tar -c [-f ARCHIVE] [OPTIONS] [File...]
Example to create a tar from target directory to named directory: tar -c directory.tar.gz -zvf file_list
EXCLUDE FILES while TARing: Insert --exclude=PATTERN
before source directory.
RECURSION: Automatic. It is the default behavior of TAR. Use --norecursion
to turn it off.
TAR References:
GZIP Manual at Gnu.org
This section contains notes from a Microsoft Reactor session about the very basics of Linux.
Host Gwyneth Pena-Siguenza, MSFT Cloud Developer Advocate.
Agenda:
Hardware: RAM, CPU, USB devices, etc. Kernel: In-memory; Assigns tasks to CPU; Manages hardware; Inteface between running programs and hardware. User Processes: All programs that are running and managed by the Kernel e.g. Email, Code, word processing, browser, etc. User Mode/Space: Access to only subset of memory and "safe CPU operations"; cannot crash entire system. Kernel Mode/Space: Unrestricted access to processor and RAM; Can crash entire system.
Storage area for bits and bytes. All running processes are made up of bits - instructions for the CPU. The state of every process resides in RAM.
State: A collection of bits in RAM that represent the current action of a running process.
CPU is in charge of operating on memory.
The Kernel is the collection of non-tangible operations and state within the CPU and RAM.
Process Mangement:
Memory Management:
Device Management:
System Calls:
Summary:
Main Memory allocated by kernel for user processes.
Every process running lives in user space.
Organization:
Components use each other: Usually on the same level or below.
User Space talks to the Kernel: SysCalls used to comms between User Space and Kernel Space.
Virtualization:
Containerization:
Users: Entity associated with username and ID. Permissions and boundaries are applied to allow (or prevent) access to files, services, etc.
Superuser:
MMU: Memory Management Unit.
Root: '/' Dev: '/dev' Device files Etc: '/etc' Core system config directory, user passwords, boot, device, networking, and other setup files. Library: '/lib' Binaries: '/bin' System Bin: '/sbin' System-based binaries. Not available to non-superusers. System executables, boot files, low-level system repair/maintenance, etc). User: '/usr' Options: '/opt' Temp: '/tmp' Temporary files. Boot: '/boot' Contains boot loader and the kernel (loader). Binaries: '/bin' Executables and most Unix commands. Many others: Too many to list, and varies by Linux distribution.
Everything in Linux is seen and accessible as a file. This allows comon tools to manage files, data, processes, etc.
USR:
DEV:
Editing how kernel boots will require low-level development skills.
Three primary components of Linux: User Processes <-> Kernel <-> Hardware
Check out notes from Microsoft Reactor session linux on azure.
A great deal of the basics were gleened from Ryan's Tutorials
Specific grep, filter, and some other commands were copied from Ryan's Tutorials' Linux Cheatsheet and Grep Cheatsheet and whenever possible, variables were changed to protect the innocent.
Apt and DPKG details were gleened from this AskUbuntu.com article
TechRepublic discusses adding an openGPG key, now that apt-key is deprecated.
ChrisJean.com for tips on 'which' and 'find' tools.
Introduction to Linux on Azure
Return to Root readme