trevordmiller

Shell reference

My reference sheet for system work with Unix-like shells.

View documentation

man some_thing

Interrupt a running process

<ctrl c>

Run an executable

./some_path<enter>

Print working directory

pwd

Display own currently running processes

ps

Display all currently running processes

ps -e

Stop a process and allow it to finish (SIGTERM)

kill -15 some_pid

Force a process to quit with potential data loss or corruption (SIGKILL)

kill -9 some_pid

Quit all spawned processes with a specific name

killall some_process_name

List directory contents

ls

List all directory contents

ls -a

Change directory

cd some_path

Change to last directory

cd -

Create directory

mkdir some_directory

Create file

touch some_file

Remove directory

rm -rf some_directory

Remove file

rm some_file

Move directory

mv some_directory another_directory

Move file

mv some_directory another_directory

Copy directory

cp -R some_directory another_directory

Copy file

cp some_file another_file

Re-run commands

<ctrl r>
Some search
# <up/down> to move through list of items
# <enter> to run

See where an executable is installed

which some_executable

Pipe output from one command as input to another

some_command | another_command

Redirect output from one command to a file

some_command > some_file

Use file contents in a command

some_file < some_command

Run a command if another is successful

some_command && another_command

File contents

cat some_file

Page contents

less some_file

Search for a pattern

grep some_pattern

Examples:

ps -ef | grep systemd

Search for a pattern while ignoring the casing

grep -i some_pattern

Search for a pattern in a path

grep -R some_pattern some_path

Substitute a pattern in a path

sed 's/some_pattern/some_replacement/g' some_path

Send HTTP requests

curl some_url

HTTP response status codes:

Common status codes include:

Headers with Name: value like:

Cache-Control: max-age=600
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
Age: 24

Run executable files

./some_executable

Create a shell script

touch some_executable
chmod +x some_executable
# '#!/bin/sh' at top of file and shell commands inside

Work on a remote server

ssh some_remote_server
# Run other unix commands
exit

Run a command on a remote server

ssh some_remote_server "some_command"

Show who is logged in

who

Use text

echo "Some text"

Examples:

echo "Some text to append to file" >> some_file

Check environment variables

echo $SOME_VARIABLE

Display processes using the most resources

top

Use package management

On Linux:

# Search for a package
snap find "some search"

# View details about a package
snap info some_package

# Install packages
sudo snap install some_package

# List installed packages
snap list

# Uninstall packages
sudo snap remove some_package

On macOS:

# Search for a package
brew search some_search

# View details about a package
brew info some_package

# Install packages
brew install some_package

# List installed packages
brew leaves

# Uninstall packages
brew uninstall some_package

# Install apps
brew cask install some_app

# List installed apps
brew cask list

# Uninstall apps
brew uninstall some_app

# Upgrade all package management
brew update && brew upgrade ```

Use the clipboard

On macOS:

# Copy to the clipboard
pbcopy

# Paste from the clipboard
pbpaste

Examples:

echo "Some thing" | pbcopy
pbpaste > ~/Downloads/somefile.txt