Skip to main content

SHELL

https://cheat.sh/ https://www.nushell.sh/

Table of Contents

  1. Bash/Zsh
  2. Commands for System Information
  3. Commands for File Permissions
  4. Commands for Networking
  5. Commands for Archives and File Compression
  6. [Commands for ]
  7. Commands for Hardware Information
  8. Misc Commands
TermsDefintions
Operating System / Kernela microkernel (also known as μ-kernel) is the near-minimum amount of software that can provide the mechanisms needed to implement an operating system (OS). These mechanisms include low-level address space management, thread management, and inter-process communication (IPC)

What is the difference between the terms shell and terminal?

In the context of technology, the terms "shell" and "terminal" are related but refer to different concepts.

  1. Shell: Shell refers to a program that provides an interface for users to interact with an operating system. It acts as a command interpreter, allowing users to enter commands and execute them. The shell interprets the commands and communicates with the operating system to execute them. It provides features such as file management, process control, and scripting capabilities. Examples of popular shells include Bash (Bourne Again SHell), PowerShell, and Zsh.

  2. Terminal: A terminal, also known as a terminal emulator or console, is a program or device that provides a text-based interface for users to interact with the shell. It emulates the behavior of a physical terminal and allows users to enter commands and receive text-based output from the shell. The terminal provides a window or console where users can type commands and view the results. It can be a command-line interface (CLI) or a graphical user interface (GUI) application. Examples of terminal applications include GNOME Terminal, macOS Terminal, and Windows Command Prompt.

To summarize, the shell is the program that interprets and executes commands, while the terminal is the program or device that provides the interface for users to interact with the shell. The terminal serves as a bridge between the user and the shell, allowing users to enter commands and view the output.

.bashrc & .bash_profile

Mac OS connect Global Packages

export PATH="\$HOME/.npm-global/bin:\$PATH"
npm link @angular/cli
  • open with default text editor open ~/.bashrc

  • open with vs code code ~/.zshrc

.bashrc

  • config file read by a shell that's both interactive and non-login
  • shell startup file

.bash_profile

  • executed when you login
  • you might put important environment variables here like PATH

Bash / Zsh Syntax

  • && for chaining tasks
  • & for running tasks simaltaneously

zsh Configuration Files

  • .zprofile (login shell)
  • .zshenv (environment variables)
  • .zshrc (interactive shell)
  • .zlogin (login shell)
  • .zlogout (when the shell exits)

Zsh setup

Zsh configuration

~/.zshrc, ~/.profile

Autocomplete

autoload -U compinit; compinit

common errors

“command not found” error messages at the command line

SOLUTION:

  • the command syntax was entered incorrectly
  • the command you are attempting to run is not installed
  • the command was deleted, or, worse, the system directory was deleted or modified
  • the users $PATH is incomplete, or $PATH has been erroneously set, reset, or cleared – this is the most common reason to see a ‘command not found’ message

nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"

SOLUTION:

Run npm config delete prefix or nvm use --delete-prefix v16.13.1 --silent to unset it. /Users/savageceo/.zshrc:source:11: no such file or directory: /usr/local/opt/powerlevel10k/powerlevel10k.zsh-theme

Linux Commands As Keyboard Shortcuts

Command Description Ctrl + C Command to kill a running process. Ctrl + Z Command to stop a currently active process. Ctrl + W Command to cut a single word preceding the cursor. Ctrl + U Command to cut a line fragment preceding the cursor. Ctrl + K Command to cut a line fragment succeeding the cursor. Ctrl + Y Command to paste from clipboard. Ctrl + R Command to recall the last used command. Ctrl + O Command to execute the previously recalled command. Ctrl + G Command to exit command history. !! Command to execute a previously executed command.

Shell / Terminal Shortcuts

Copy to Clipboard

 | pbcopy

example: copy all filenames in a directory to the clipboard

ls | pbcopy

Rename a directory

mv /home/user/oldname /home/user/newname

Bash shortcuts

mkdir \$(date %m-%d)-App

Open current directory

start .

Filepath to exe

which bash

delete from the cursor to the end of the line.

Ctrl+K

delete from the cursor to the beginning of the line.

Ctrl+U

Using ls to list directories and their total sizes

du -sh *
short version of:

du --summary --human-readable *

Table of Contents ⬆️

CommandDescription
lsIt is a list command and will display the directories and files visible under the present working directory
ls -RGoes a step further to display or list files in sub-directories of the present working directory
ls -aThe argument -a in this list command helps list all the hidden files in the present working directory
ls -alThe argument -al in this list command gives a detailed list of the displayed files and directories with extra information such as size, permission, owner, etc.
cd or cd ~Takes you to the Home directory of your Linux operating system
cd ..navigates you backwards to the parent directory of the current directory you are under.
cdcombine it with a target directory under the present working directory to navigate to it e.g cd Downloads
cd /Takes you to the root directory of your Linux operating system
cat > filenameIt will create a file called filename
cat filenameIt will display the contents of a file called filename
cat file_1 file_2 > file_3Joins the content of file_1 and file_2 and copies the result to a newly created file_3
mv file_random "defined file path"It will move the file called file_random to the target file path or destination you will define.
mv file_name new_filenameIt will rename the file called file_name to a new file name called new_filename
sudoA command used by sudoer users to execute privileged commands applicable to a root user or superuser.
rm filenameIt will delete a file called filename
manUse it with a Linux command you do not understand to get help on how to use it. E.g man sudo
historyIt will display a list of the previous Linux terminal commands you have executed under that specific session.
clearIt will clear your command-line interface if you feel it is too crowded.
mkdir directory_nameit will create a director called directory_name
rmdir my_directoryIt will delete a directory called my_directory
mvYou can use it to rename a directory or a file
pr -xIt will divide or split a file to x defined columns.
pr -hUse it to add a header to a target file.
pr -nUses Line numbers to denote a target user file.
lp -nc, lpr cIt is a printing function and in this case it will print c copies of a target file.
lp -d lp -PHelps you identify the name of the printer you are using.
apt or apt-getA Debian distro package command for installing system updates and package applications
mail -s 'mail_subject' -c 'cc-mail_address' -b 'bcc-mail_address' 'to-mail_address'Use this command to send an email from the terminal.
mail -s "mail_subject" to-mail_address < attachment_filenameUse this command to also send an email from the terminal but this time with an attachment.

Table of Contents ⬆️

Linux Commands for System Information

CommandDescription
uname -aDisplays basic system information like machine name and the supported operating system architecture.
uname -rOutputs the kernel release information like the kernel version.
lsb_release -aIf you are using any Linux distro, this command displays its Distributor ID, Description, Release, and Codename.
uptimeOutputs how long your Linux system has been active together with the load average of this duration.
hostnameOutputs your system's hostname or the name you have given your Linux machine
hostname -IOutputs the assigned IP address your system is currently using depending on the network connection it is under.
last rebootA useful command for you to gauge how many times your system restarted.
dateUseful in displaying the current date and time.
calOutputs the present month's calendar.
wLets you know who or which systems are online especially if you are in a large network.
Table of Contents ⬆️

Linux Commands for File Permissions

CommandDescription
ls -lOutputs the file type and the file permissions needed to access the target file.
chown root /uIn this command, the ownership of /u is transferred to the root user.
chown user:group file_nameThe user and group privileges linked to file_name are changed.
chmod 777 [file_name]Everyone access the file called file_name will have read, write, and execute permissions.
chmod 755 [file_name]The owner of the file called file_name will have read, write, and execute permissions while other users will only have read and execute permissions.
chmod 766 [file_name]The owner of the file called file_name has complete access to it while group and other users can only read and execute.
chown [user] [file_name]The ownership of the file called file_name is assigned or changed to a new owner called user.
chmod -c -RAssign a file the read, write, and execute permissions.
touch -a -tUseful in creating or modifying a file timestamp.
chown -c -RUseful in changing the ownership of an assigned or owned file.

permissions

Table of Contents ⬆️

Linux Commands for Networking

CommandDescription
SSH username@ip_address or hostnameEnables SSH login or sign-in to a remote Linux machine by specifying the machine's username and IP address.
ping hostnameUseful in analyzing the responsiveness of a hostname connected to a network.
dir Helps you view files under a remote computer's active directory.
cd "dir_name"Access a directory called dir_name on a remote computer.
put filenameIt will upload a file called filename from your local computer to a target remote computer.
get filenameIt will download a file called filename from a specified remote computer to your local machine.
quitLogout from a remote connection.
ping -c hostnameIt will print a routing packet trace associated with the targeted hostname or IP address
ssh -iGrants user access to openSSH client.
tcpdumpUsed to dump network traffic.
Table of Contents ⬆️

Linux Commands for Archives and File Compression

CommandDescription
tar xvfzUsed for creating or extracting files with .tar or .tgz extensions.
gzip, gunzip, zcat filenameUsed in creating, extracting. or viewing files with .gz extension
uuencode, uudecodeUsed in creating or extracting files with .Z extension.
zip, unzip -vUsed in creating or extracting files with .Zip extension.
rpmUsed in creating or extracting files with .rpm extension.
bzip2, bunzip2Used in creating or extracting files with .bz2 extension.
rarUsed in creating or extracting files with .rar extension.
tar cf [compressed_filename.tar] [file_name]This command creates an tar archive called compressed_filename for the file_name file.
tar xf [compressed_filename.tar]This command extracts the tar archive called compressed_filename.
tar czf [compressed_filename.tar.gz]This command compresses a tar file into a gzip archive.
Table of Contents ⬆️

Linux Commands for Installing Packages

CommandDescription
yum search [keyword]Trace a package installation based on specific keywords.
yum install package.rpmThe use of a YUM package manager to install and configure a package.
yum info packageThe use of the YUM package manager to find more information about a package before optionally proceeding with its installation.
rpm -i package.rpmUsing the RPM package manager to install a downloaded package.
yum remove packageUsing the YUM package manager to uninstall or remove a package from your system.
tar zxvf sourcecode.tar.gz
cd sourcecode
./configure
make
make installCommand sequence to install a package software that comes as a source code.
dnf install package.rpmUsing the DNF package manager to install a package software.
apt install packageUsing the APT package manager to install a package software.
rpm -e package.rpmUsing the RPM package manager to remove or uninstall an rpm package
Table of Contents ⬆️

Linux Search Commands

CommandDescription
grep [pattern] [file]Employs a specific pattern to search the contents of a file.
grep -r [pattern] [directory_name]Recursively or repeatedly searches within a specified directory for a defined pattern match.
locate [name]It will track and locate directories and files based on a name identity.
find [/path/to/folder/location] -name [a]Traces a specific system directory location then queries the names of its files and folders based on a character match. In this case [a]
find [/path/to/folder/location] -size [+100M]It will trace a system directory location and list the files within it that are bigger than 100 M in size.
find /home/FossLinux -name 'prefix*'Traces a specified system path for files with a matching prefix.

Table of Contents ⬆️

Linux Commands for SSH Logins

CommandDescription
ssh username@hostnameConnects you to a remote machine or server based on a valid username and hostname or IP address.
ssh hostConnects you to a specified host through the default port 22.
ssh -p [port] username@hostnameConnects you to a remote machine or server through a specified port.
telnet hostnameUses telnet's default port 23 to connect you to a target hostname, remote machine, or server.
Table of Contents ⬆️

Linux Commands for File Transfers and Management

CommandDescription
findThis command is useful when you need to find a specific file
ls -a -C -hLists or displays the content of an active directory.
rm -r -fUsed to remove or delete active files and directories instances.
locate -iUses updateddb(8) to track and find files on your system.
cp -a -R -iUsed to copy directories or files from an active directory instance.
du -sGive important information regarding the disk usage (storage space) on your Linux system. It is efficient when combined with the Sudo command.
file -b -iHelps identify the type of file on your system.
mv -f -iUsed for moving directories or files to a different system path or location.
grep, egrep, fgrep -i -vUseful in printing lines with a matching pattern.
scp file.txt server:/tmpUses secure copy command to copy file to a remote server. In this case the tmp folder

Table of Contents ⬆️

Linux File and Directory Commands

CommandDescription
lsIt will list the files and directories under an active director path.
ls -aLists only active files, but in a long and detailed list.
pwdreveals the present directory you are working under.
mkdir [dir_name]It will create a directory with the specified name.
rm [filename]It will delete a file with the specified name.
rm -r [directory_name]It will delete a directory with the specified name.
rm -rf [directory_name]It will recursively and forcefully delete a directory with the specified name.
cp filename1 filename2It will copy the contents of filename1 to filename2
cp -r [directory1] [directory2]It will recursively copy the content of directory1 to directory2
mv filename1 filename2It will rename filename1 to filename2

Linux Commands for Disk Utilities and Usage

CommandDescription
df -hWorks with mounted systems to reveal their free and used space.
df -iWorks with mounted systems to reveal their free inodes. These inodes hold disk block location data an other attributes.
fdisk -lReveals disk information like types, sizes, and partitions.
du -ahOutputs files and directories disk usage.
du -shOutputs your currently active directory's disk usage.
findmntOutputs the target mount point associated with all your file system.
mount device_path mount_pointUse this command syntax to mount a device.
mkfs -t -VUse this command to create a new file system.
resize2fsUsed for updating a file system, especially after lvextend*
fsck -A -NUsed to check and repair a file system.

Table of Contents ⬆️

Linux Commands for Environment Variables

CommandDescription
echo$Variable_name Outputs the value of a defined variable on the terminal.
envOutputs data related to all environment variables.
variable_name = variable_valueUse this command syntax to assign a variable name with a variable value.
UnsetUsed to remove or get rid of a variable.
export Variable = valueUsed to assign an environment variable a new value.

Set Environment Variables in Linux and Mac

Environment variables, are designed to be accessible to scripts or child processes and differ from shell variables by use of the export command.

Common environment variables in Linux and Mac

The following environment variables are not an exhaustive list, but are the most common between Linux and Mac:

  • $PATH: A colon-separated list of directories for looking up binaries and scripts that don't need the full path in order to execute them, e.g. echo instead of /bin/echo as /bin is in $PATH
  • $HOME: The user's home directory for the current shell
  • $PWD: The directory location in the current shell
  • $SHELL: The executable for the current shell, e.g. /bin/bash
  • $HOSTNAME: The hostname of the current machine as it may appear in your local network

How to change and set environment variables

Changing an environment variable is no different from changing a shell variable:

export FAV_JEDI="Obi-Wan Kenobi"
echo "My favorite Jedi is \$FAV_JEDI"
# >> My fav jedi is Obi-Wan Kenobi

FAV_JEDI="Rey Skywalker"
echo "My favorite Jedi is now \$FAV_JEDI"
# >> My favorite Jedi is now Rey Skywalker

How to delete an environment variable

The unset command is used to remove a variable:

export FAV_DROID="R2-D2"
echo "My favorite droid is \$FAV_DROID"
# >> My favorite droid is R2-D2

unset FAV_DROID
echo "My favorite droid is \$FAV_DROID"
# >> My favorite droid is

How to list environment variables

The printenv command does exactly as it describes, printing out all environment variables available to the current shell.

printenv

You can combine printenv with grep to filter the list environment variables:

printenv | grep GIT

To output the value of a specific environment variable, you should use also printenv:

printenv HOME

Table of Contents ⬆️

Linux Commands for System Processes Management

CommandDescription
bgSends a running process to execute in the background.
fgSends a stopped or halted process to keep executing on the foreground.
topOutputs all active processes details.
htopIt is an interactive process viewer command.
psOutputs a running process's status to a system user.
ps -efOutputs all active or executing processes on the Linux system.
ps -efgrep process_name
ps PIDOutputs a running processes's status in reference to its process ID.
pidofReveals the process ID of a specified process.
kill PIDKills a running process based on its process ID.

Table of Contents ⬆️

Linux Commands for VI Editing

CommandDescription
vilaunch the VIM editor.
:q!quit the editor without saving the implemented changes.
:wqquit the editor and also make changes to the open file.
iTakes the editor to insert mode and initiates a cursor.
aAn insert mode that enables the system user to write their input after a cursor.
AAn insert mode for writing at the end of the line.
ESC keyUsed to exit the insert mode.
uUndo Vim command for the last change.
UUndo Vim command for an entire line.
oInsert mode for opening a new line.

Table of Contents ⬆️

Linux Commands for File Editors

CommandDescription
exLaunches a basic editor.
viLaunches visual editor.
nanoLaunches the GNU nano editor.
viewLaunches an editor in view or read-only mode.
emacsLaunches emacs, an extensible and customizable system editor.
sublimeLaunches sublime text editor.
sedlaunches stream editor.
picolaunches pico, a simple editor.

Table of Contents ⬆️

Linux Commands for File Utilities

CommandDescription
tr -dCommand for translating or deleting a character.
uniq -c -uCommand for reporting or omitting repeated lines.
split -lCommand for splitting a file into segments.
wc -wThis command prints the number of bytes, words, and newlines on a specified file.
head -nOutputs the first few contents of a specified file.
cut -sremoves or deletes a file's section.
diff -qCommand to compare files line by line.
join -iGiven that two files have a common field, this command will join the lines of these files.
more, lessEnables a system user to view the contents of a file, a single file page view at a time.
sort -nCommand to sort the lines within a text file.

Linux Commands for Scripting

Once your skillset on using the Linux operating system improves with time, you might be inspired to scribble some scripts from your OS environment. These commands apply both to the scripting languages you might be familiar with and the scripting environment setup to make your Linux OS life as comfortable as possible. [table “49” not found /]

Table of Contents ⬆️

Linux Commands for Hardware Information

CommandDescription
dmesgOutputs the kernel ring buffer messages.
cat /proc/cpuinfoOutputs CPU information of your machine.
cat /proc/meminfoOutputs Memory information of your machine.
free -hOutputs both free and used machine memory info.
lshwOutputs the system's hardware configuration information.
lsblkOutputs the system's block devices information.
lspci -tvOutputs the system's PCI devices.
lsusb -tvOutputs the system's USB devices.
dmidecodeOutputs the system's hardware information on DMI/SMBIOS related to the BIOS.
hdparm -i /dev/diskOutputs a specified disk data information.

Table of Contents ⬆️

Linux Commands for Performance Monitoring and Statistics

CommandDescription
mpstat 1Outputs processor-related statistics.
vmstat 1Outputs statistical data related to the system's virtual memory.
iostat 1Outputs I/O (Input/Output) statistics.
lsofOutputs all the system's open files.
lsof -u userOutputs a list of all the files opened by a system user.
watch df -hA command to output periodic system updates.
tail 100 /var/log/messagesCommand to output the most recent 100 syslog messages. The Debian systems equivalent of this command is /var/log/syslog
tcpdump -i eth0An interface eth0-related command for outputting captured packets.
tcpdump -i eth0 'port 80'Command for monitoring traffic through port 80

Table of Contents ⬆️

Linux Misc Commands

CommandDescription
date -dCommand to print formatted date.
'[command_name] A backquote to execute a specified command.
exportA command for assigning or removing an environment variable.
calCommand to print a calendar.
exprA command used to evaluate an expression.
bcLaunches a high-precision calculator shell.
pwd -PReveals the current system directory you are under.

Table of Contents ⬆️