ZSH

Your computer and the Internet can teach you a lot about how to use it. You have to do the work. You have to study and learn how your computer works. Your computer is the most important source of information about how to install, operate, maintain and repair your computer and the software running on it.

Investigate your terminal and your shell, to make yourself a high performance Linux user. Your terminal is the dashboard for managing your computer. Zsh is one of the most important languages you can use to communicate with your computer.

Bash is usually the default shell for Linux operating systems. Whenever I install a new Linux installation, I usually change my default shell from Bash to Zsh. If you want to use Zsh, you’ll probably have to install it.

Zsh is a more advanced shell. It does everything Bash does and a few more things. It’s easy to install. Use the chsh command (change shell) to change your default shell. Use chsh without sudo to just change your user account to Zsh. Use it with sudo to change the root account.

sudo apt install zsh 

works fine. Then use

whereis zsh

to find the path to zsh and then use

sudo chsh -s /usr/bin/zsh

to set it as your default shell. You’ll have to restart your shell for Zsh to run. You may have to restart your computer.

Get in the habit of using your terminal as much as possible. Install your applications with your shell. I read somewhere that installing them on the command line, rather than your package manager, affects how they’re installed. Maintain a smooth running Linux, by using the command line to install most of the applications on your system.

Use, “sudo apt install”, or, “sudo pkcon install”, to install applications. These are commands you will use often and using them will help you get familiar with the terminal and shell. Use the terminal as much as possible. Use your graphical tools when you need to.

Use Discover and Muon Package Manager to supplement your shell, rather than replace it. Install applications from the command line, whenever you can. You can use Discover and Muon to find applications, especially extensions to applications, and then install them using the command line. You can install applications with Discover or Muon, when that is the best strategy.

Explore your file system. Pay particular attention to the /etc directory. That is where the configuration files for many of your applications are. That is where you can get in there and adjust the global settings for your applications, if you want to.

The configuration files for your user account are usually hidden files in your Home directory. You can find them by selecting the hamburger, the three lines at the top right corner of Dolphin, and selecting, “Show Hidden Files” or by entering ls -a on the command line.

The files for the applications you install on your system are in the /etc or the /opt directory. If you want to edit the configuration of the applications in the root account, make sure you edit the correct file for each application, in order to prevent it from being clobbered when the application is updated. You may also need to create a custom.d file with your custom configuration, linked to the default config file, to prevent updates from clobbering your custom settings. Clobbered means the config file is reset to the default settings, whenever that application is updated.

I’m still learning how to use the command line myself. Learning the file structure of Linux is a very complicated puzzle. Writing scripts is like writing in a foreign language. Zsh is actually a computer programming language.

Be careful. Make a back up copy of your files, before you start changing things, so you can replace it in case you break something.

One of the first things I always do is to add this snippet to my .zshrc file

# prompt before overwrite  
alias rm='rm -i'           
alias cp='cp -i'           
alias mv='mv -i' 

It commands the shell to ask me if I really want to move or delete any files. This protects me from accidentally deleting important files. You can copy and paste that right into your .zshrc file. It will work just like it is and it is one of the most important safety features for using Linux.

AWK, Lua and ZSH scripting are very valuable skills. C, C++, Rust, QML and JavaScript are languages we will be focusing on learning. Get familiar with AWK to learn how to communicate with your computer. It is one of the most powerful shell commands. C, C++, QML and JavaScript are the languages that Linux and KDE are written in.

Have fun and be careful, learning how to use Linux to expand your consciousness onto our global cloud of artificial intelligence. Be creative. Add value to the cloud. Make something valuable and trade it in our one world wide free marketplace of ideas, products and services.

Using all these tools to create something valuable is way more interesting and fun than playing any game.

Learn how to use C, C++, QML, Python and JavaScript to create applications. Websites are just one particular kind of application. They depend on a browser to run them. Native applications run on your desktop. Your browsers are native applications that you can run web applications in.

There are many clouds of artificial intelligence. Microsoft owns one. Apple owns one. Google owns once. China owns one. KDE owns one. The Internet itself is a global cloud of artificial intelligence.

Learning how to use your shell well, is an important stage of learning how to create your own, “do it yourself,” cloud of artificial intelligence and to harness other clouds of artificial intelligence for your own benefit. The following is a tiny fraction of what you can do with AWK and ZSH.

zsh Grammar

The Zsh (Z shell) grammar defines the syntax and structure of commands and scripts for Zsh, which is an extended Bourne shell with many enhancements, including improved tab completion, better globbing and a more powerful scripting language. Here is a superficial introduction to some key aspects of Zsh’s grammar.

Commands and Command Lines

A simple command consists of a command name followed by arguments, such as, zsh

echo "Hello, World!" 

Each argument can be a word, variable or command substitution. Commands can be connected with | to form pipelines where the output of one command becomes the input of the next: zsh

ls -l | grep "txt$"

Commands can be separated by ;, &, && or || for different control flow:

  • ; runs commands sequentially.
  • & runs commands in the background.
  • && runs the next command, only if the previous one succeeds.
  • || runs the next command, only if the previous one fails.

Variables are assigned with =, with no spaces around the =. var_name=value, for example. Variables can be local or global, with local used for function-local variables. Variables are expanded with $: echo $var_name. Braces can be used for clarity or when the variable is followed by characters: zsh echo ${var_name}.

Arrays are defined by parentheses: array=(item1 item2 item3). Accessing elements: ${array[1]} for the first element (Zsh uses 1-based indexing for arrays by default).

Control Structures

Conditionals:

If statements: zsh

if [[ condition ]]; then
  commands
elif [[ another_condition ]];
  then other_commands
else
  default_commands
fi

Loops:

for loop:

for i in item1 item2; do
  echo $i
done

while and until loops: zsh

while [[ condition ]]; do
  commands
done

case statement for pattern matching: zsh

case "$var" in
  pattern1) commands ;;
  pattern2) other_commands ;;
  *) default_commands ;;
esac

Functions

Functions can be defined with: zsh

function func_name() {
  commands
}

Or simply: zsh

func_name() {
  commands
}

Functions are called like commands: zsh

func_name arg1 arg2

Globbing

Zsh extends globbing with features like extended glob operators like **/ for recursive directory matching. Glob qualifiers (Q) for specifying file attributes.

Redirection

Use standard redirection operators like >, <, >>, <<, but with additional features like: >| to force overwrite even with noclobber set. >& for redirecting both stdout and stderr.

Special Variables

Use $0, $1, $2… for script’s name and arguments. $# for the number of arguments. $? for the exit status of the last command.

Quoting

Use single quotes (‘) for literal strings. Double quotes (“) for allowing variable and command substitution. Backslashes (\) for escaping characters.

Here Documents

For multi-line input: zsh

cat << EOF
This is a here document.
EOF

Aliases

Aliases define command shortcuts: zsh

alias ll='ls -l'

Zsh’s grammar is rich and flexible, offering many features for both interactive use and scripting. It extends the POSIX shell syntax with considerable enhancements, making it a powerful tool for shell scripting and command-line work. Remember, while Zsh is compatible with many Bash scripts, there are nuances and features unique to Zsh that might require consideration when writing cross-shell compatible scripts.

Zsh is highly configurable. You can use plugins to add or enhance zsh capabilities. You really want to figure out where the $PATH is on your computer and understand how to edit it if you need to. You need to know where your configuration files are and be able to edit them.

Investigate and learn how to create hard and soft links in your file system. Often, rather than editing a config file, you add another file with a .d extension and link it to the config file. That way, when the program is updated, your custom configuration settings don’t get clobbered by the update.

Repetition is a good learning technique. Reading these commands over and over again will get them into your memory. Sometimes research is like reading a dictionary, not very interesting, but valuable information. Make sure you read through this information often. Get it installed in your long term memory.

Shell Commands

man pages are the official Linux manual for the programs on your computer. Type man followed by the command you want to investigate.

Info pages are similar to man pages, written in a different style, with a little more info.

Command - Description

ls - list files in pwd
cd - change directory
pwd - present working directory
touch - create new file
mkdir - create new directory
echo - print something in the shell
whoami - prints current user name
whereis - locates the binary, source and manual files for the specified command names.
which - locate a command

cat - concatenate files to standard output
chgrp - change file group ownership
chmod - change file access permissions
chown - change file owner and group
cp - copy files and directories
date - print or set the system data and time
dd - convert and copy a file
df - report filesystem disk space usage
dmesg - print or control the kernel message buffer
echo - display a line of text
false - do nothing, unsuccessfully
hostname - show or set the system's host name
kill - send signals to processes
ln make links between files
login - begin a session on the system
ls - list directory contents
mkdir - make directories
mknod - make block or character special files
more - page through text
mount - mount a filesystem
mv - move/rename files
ps - report process status
rm - remove files or directories
rmdir - remove empty directories
sed - The `sed' stream editor
sh - POSIX compatible command shell
stty - change and print terminal line settings
su - change user ID to super user
sync - flush filesystem buffers
true - do nothing, successfully
umount - unmount file systems
uname - print system information

Environment variables

Environment variables are placeholders for data that can change. For example, the HOME variable can change depending on who is logged in.

export VARIABLE=example
prompt$ echo $VARIABLE
example

Delete a variable with

prompt$ unset VARIABLE

Use sudo to make global variables.

Here is a list of common environment variables in Linux:

$USER - your current user name.
$SHELL - the path to the current command shell.
$PWD - the current working directory.
$HOSTNAME - the host name of the computer
$HOME - your home directory.
$MAIL - the location of the user's mail spool
$LANG - your current language.
$TZ - your time zone.
$PS1 - the default prompt in Bash.
$TERM - the current terminal type.
$DISPLAY - the display used by X.
$HISTFILESIZE - the maximum number of lines in the history file.
$EDITOR - the user's preferred text editor,
$MANPATH - the list of directories to search for man pages.
$OSTYPE - the type of operating system.

PATH Environment Variable

A command or script must be located in one of the directories listed in your PATH, to be executed when you prompt for it on your command line. Use

echo $PATH

to display the path on your computer. You can run a program that is in your path from anywhere, just by entering the name of the program. Your computer will automatically search your $PATH and run the program. You can add a directory to your path with the command

export PATH="$HOME/bin:$PATH"

bin is the name of the directory, located in your home directory in this case, that you are adding to your $PATH.

The PATH environment variable in Linux (and Unix-like systems) is a list of directories where executable programs are located. When a command is entered in the shell, it searches through these directories in order, looking for an executable with the given name.

PATH is an environment variable, which means it’s part of the environment in which commands are executed. It’s accessible to all processes started from the shell where it’s set. The value of PATH is a colon-separated list of directories:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Each directory in this list is a place where the shell looks for commands. When you type a command like ls, the shell does not search the entire filesystem. Instead, it checks if the command is a built-in (like cd or echo). If not built-in, it searches through each directory listed in PATH for an executable file named ls.

The order of directories in PATH is significant because the shell stops searching as soon as it finds an executable with the specified name. If there are multiple executables with the same name in different directories, the one in the directory listed first in PATH will be executed.

By default, the current directory (./) is not included in the PATH for security reasons. To run a program in the current directory, you must use ./program_name.

Not including the current directory in PATH prevents accidental execution of scripts or programs in the working directory, reducing the risk of executing malicious software. System-wide PATH settings are usually in /etc/environment, /etc/profile or equivalent files for different shells. User-specific settings are in personal configuration files like .zshrc or .profile.

The PATH variable can be set or modified in several ways. Shell configuration files include .bashrc, .bash_profile, or .profile for Bash, .zshrc for Zsh, etc.

Environment Command: bash

export PATH=$PATH:/new/directory

Session Specific: Directly in the terminal for the current session: bash

PATH=$PATH:/new/directory

To add a new directory to PATH for the current session: bash

export PATH=$PATH:/home/user/bin

To make this change permanent, you would add the line to your shell’s startup file.

When installing new software, you might need to adjust PATH if the executables are not in a directory already listed. Developers often add their project’s bin directory or tools directories to PATH for easier access. Users might keep personal scripts in a dedicated directory and add it to PATH for easy command execution.

If there are executables with the same name in different PATH directories, ensure the intended one is in a directory earlier in the list. An excessively long PATH can slightly slow command lookup, although this is typically negligible.

Understanding and managing the PATH variable is crucial for effective use of the Linux command line, as it directly affects how commands are found and executed.

Inode

An inode (index node) in Linux is a data structure used to store all the information about a file except its name and actual data content. It’s essentially a unique identifier for each file within a filesystem.

Components of an Inode:

  • Inode Number: A unique number within the filesystem, used to locate the inode.
  • File Type: Indicates whether the entry is a file, directory, symbolic link, etc.
  • Permissions: Read, write and execute permissions for owner, group and others.
  • Ownership: The user ID and group ID of the file’s owner.
  • Timestamps:
    • Access time (atime)
    • Modification time (mtime)
    • Change time (ctime, when file’s inode was last changed)
  • Size: The size of the file in bytes.
  • Links Count: The number of hard links pointing to this inode (including the original name of the file).
  • Data Block Pointers: Pointers to the data blocks where the file’s content is stored on disk. This includes direct, indirect, double indirect and even triple indirect blocks for very large files.

When a new file is created, the filesystem allocates an unused inode from the pool of available inodes. The file’s name is stored in a directory entry, which includes the inode number. The directory itself is just a special type of file that contains a list of filenames and their corresponding inode numbers.

The system looks up the filename in the directory to find the associated inode number. Using the inode number, it locates the inode on disk. The inode provides all the metadata needed to access the file’s content through its data block pointers.

Hard links are additional directory entries that point to the same inode. Thus, multiple names can refer to the same file data, but all share the same inode. When you delete a hard link (including what might be considered the “original” file), the link count in the inode decreases. The file data remains on disk until this count reaches zero.

Symbolic links (symlinks) have their own inodes, but these inodes store the path to another file or directory rather than data block pointers. They act like pointers to other files.

Deleting a file removes its directory entry but doesn’t immediately free up the data blocks. Instead, the link count in the inode is decremented. If it reaches zero, the inode is marked for reuse and over time, the data blocks can be reclaimed for new data.

Each filesystem has a fixed number of inodes created at format time. Once all inodes are used, no more files can be created, even if there’s still free space on the disk. This situation is known as “inode exhaustion.”

Commands like ls -i show the inode number of files.
stat command provides detailed inode information: bash

stat filename

You can check inode usage with df -i: bash

df -i

Understanding inodes helps in managing disk space, particularly in scenarios where the number of files is very high relative to their size. Knowledge of inodes aids in creating backup strategies, especially when dealing with hard links or ensuring data integrity across different systems. Tools like extundelete for ext4 filesystems use inode information to attempt file recovery after accidental deletion. In complex file system issues, examining inode data can be crucial for diagnosing problems.

In essence, inodes are the backbone of file management in Unix-like systems, providing a structured way to manage file metadata and access file contents efficiently.

In Linux, creating links is an essential part of file management, allowing for multiple names (references) to refer to the same file or directory. There are two main types of links, hard links and soft links.

A hard link is an additional name for an existing file where both names point directly to the same data on disk. Hard links share the same inode (index node) number, meaning they refer to the same file data on the filesystem. You cannot create hard links to directories to prevent cyclic references and maintain filesystem integrity. Hard links must be on the same filesystem; you can’t create a hard link to a file on a different partition. Use the ln command without the -s option to make a hard link:

ln existing_file new_hard_link

bash

echo "Hello, World!" > original.txt
ln original.txt hard_link.txt

Now, both original.txt and hard_link.txt refer to the same file content. If you change one, you change the content for both.

A symbolic link, or symlink, is a file that points to another file or directory by name. Unlike hard links, symlinks work like shortcuts or pointers. Symlinks have their own inode but contain the path to the target file or directory. You can create symlinks to files, directories or even non-existent paths. Symlinks can point to files on different filesystems. Use the ln command with the -s option: bash

ln -s target_file_or_directory symlink_name

Example bash

mkdir dir
ln -s dir symlink_to_dir

Here, symlink_to_dir is a symbolic link pointing to dir. If you move or rename dir, symlink_to_dir will become broken unless updated.

Deleting one hard link decreases the link count by one. The file data remains until all links (including the original file name) are removed. Deleting the target of a symlink leaves a “dangling” symlink pointing to a non-existent file, but deleting the symlink itself does not affect the original file.

Hard Links share the same permissions as the original file. Symbolic Links have their own permissions, but these are only relevant when accessing the symlink itself. The permissions of the target file determine what operations can be performed after following the link.

Hard links might be handled differently by backup tools, potentially leading to multiple copies of the same data if not managed correctly. Symlinks are often preserved in backups but might require careful restoration.

Hard Links are useful for keeping multiple names for the same data when you don’t want to duplicate data, like in software development or when managing multiple configurations.

Ideal for creating shortcuts, managing system configurations (like linking /bin/sh to a specific shell), or when you need to reference files across different filesystems.

Listing Links:

ls -li shows inode numbers, helping you identify hard links.
ls -l shows symlinks with an arrow pointing to their target.

Removing Links:

rm symlink removes the symlink but not the target.
rm hard_link removes one name for the file; the file's data persists if there are more links.

Finding Links:

find . -type l finds all symlinks.
find . -links +1 finds files with more than one hard link (including the original).

Understanding and using links effectively can significantly enhance file management and system organization in Linux.

Wild Cards

Wild cards are character variables

? matches a single character. 
* matches any characters, including any number of characters.
[1-9] matches any single digit.

Streams

Streams are inputs to and outputs from programs.

Standard input (stdin) is the stream of data into a program. stdin usually comes from the keyboard, but can be redirected from another source, such as a file or an ssh connection.

Standard output (stdout) is the stream of data out of a program. stdout usually goes to the screen, but can be redirected to go somewhere else, such as a file or an ssh connection.

Standard error (stderr) is a stream of data for printing diagnostic or error messages. stderr usually goes to the screen, but can be redirected to a file or other device, depending on what the parent process sets up.

Redirecting streams

linux redirection symbols

> - redirects standard output to a file. If the file already exists, it is overwritten.
>> - redirects output to a file, appending to the file if it already exists.
2> - directs stderr to a file.
2>> - appends stderr to a file.
&> - directs stdout and stderr to a file.
< - directs contents of a file to the command. 
<< - accepts text as stdin on the following lines. 
<> - the specified file is used for stdin and stdout.    

Pipe data between programs

Use the pipe symbol | to make the stdout of one program be the stdin of another program.

For example, ps -A | sort -k 2 | head

ps  -A - lists all processes running on your system. 
sort -k 2 - sorts the processes using the process name in the second column.
head - only prints the first ten lines of the output.

Sort lines of text in a file

The sort command is used to sort lines of text in a file. sort all by itself, orders the lines alphabetically.

-r - reverses the order
-k - sorts the file by a certain column,
-k 2 sorts the data using the second column.

grep

grep is used to find certain info, within large amounts of info. grep is case sensitive by default. Use -i option for case insensitive search.

grep [OPTIONS] PATTERN FILE

ps -A | grep htop 
ps -A - lists all running processes. 
grep htop - limits the output to lines with the word htop in it. 

find

The find command searches for files in the directory tree, starting from the specified location.

find PATH EXPRESSION

You can search files by filename, size, permissions, group, UID, etc.

name - searches by filename
perm - searches for files with certain permissions
size - searches by file size
type d - searches for directories
type f - searches for files
user - searches by user
group - searches by group

Wildcards must be enclosed in double quotes. When using -size, use c for bytes, k for kilobytes, M for megabytes and G for gigabytes.

locate

The locate command searches a database of names. locate finds files by name only. Use

sudo updatedb

to update the database and locate recently added files.

Word Count

wc displays the number of lines, words and bytes in a file. Options -l, -w -c limit the output to lines only, words only or bytes only, respectively.

File Type

file command will tell what kind of file a selected file is.

whereis

The whereis command is used to locate the binary, source and manual pages of a command.

head

You can display the first ten lines of a file with the head command. Use -n to change the number of lines. You can display more than one file with head.

tail

The tail command displays the last 10 lines of a file by default. Use the -n option to change the number of lines. The -f option keeps the file open and displays new lines as they are added to the file.

tee

Split the output of a program with the tee command. The output will be displayed on the screen and entered into a designated file. The file will be overwritten by default. Use -a option to append it, rather than overwriting it.

Swapping the <esc> and <caps lock> keys

Entering this command into your .zshrc file switches the esc and caps lock keys globally.

setxkbmap -option caps:swapescape

Source

geek-university
grok