Skip to content

Instantly share code, notes, and snippets.

@jahe
Last active December 4, 2022 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jahe/d6a3147bfd37b70b86bf to your computer and use it in GitHub Desktop.
Save jahe/d6a3147bfd37b70b86bf to your computer and use it in GitHub Desktop.
Bash Cheatsheet
# Create a file and insert some text with the ">" operator
echo "my new site" > index.html
# Show PATH
echo $PATH # /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
# /usr/local/bin is in the PATH by default
# Create symbolic link (symlink)
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
# > sublime <filename>
# Display text file in shell
cat config.json
# Display long text file in shell
less config.json
# Show hidden files
ls -a
# Download file and specifiy target file name
wget http://bit.ly/Hqv9aj -O jfrog-artifactory-oss-4.4.2.zip
# Execute programmB only if programmA exits with an exit code of 0
programmA && programmB
# Execute programmB only if programmA exits withn an exit code other than 0
programmA || programmB
# Use a pipe symbol to set the stdout of a programm to the stdin of another programm
programmA | programmB
# Set variable
name=value
# Use variable
$name
# OR:
${name}
# Display path of executable (e.g. npm)
which npm
# Clear console
clear
# List all installed apt packages
apt list --installed
# Place of sudo config
/etc/sudoers
# List files + Sort them + "less" them
ls <dir> | sort | less
# Print current working directory
pwd
# Create an alias for a command or a list of commands
alias home='cd ~'
# Show current bash version
bash --version
# Usable shells config
/etc/shells
# Change shell for current user
chsh -s /usr/local/Cellar/bash/4.4_1/bin/bash
# Show currently logged in users with login date
who
# Print to the console
echo Hey Ho Lets go
# OR
echo "Hey Ho Lets go"
# Append text to file via cat (=concatenate). End input with Ctrl+D
cat >> file.txt
Type in
random Text
# Show line/word/character count for a file
wc myfile.txt
# Show only line/word/character count for a file
wc [-l] [-w] [-c] myfile.txt
# List files with specific file extension
ls *.txt
# Remove multiple files
rm fileOne.txt fileTwo.txt
# Create multiple files
touch fileOne.txt fileTwo.txt
# Go to previous working directory
cd -
# Remove directory with files recursively (rmdir <dirname> can only remove empty directories)
rm -r <dirname>
# Show more info with ls -l (=long)
ls -l
# Example output ls -l
-rw-r--r-- 1 ekip staff 2133 30 Okt 19:56 bash-cheatsheet.sh
# Format
Type+Rights Links Owner Group Size Modified Name
# Value descriptions from left to right
# (x---------) Filetype
- normal file
d directory
p named pipe
c character oriented device file
b block oriented device file
s unix domainsocket
l symlink
# POSIX Rights
# (-rwx------) Owner rights
# (----rwx---) Group rights
# (-------rwx) All other users rights
r read right
w write right
x execute right
# Symlink count
1 if there is no reference to this file
# Owners username
ekip
# Groups name which is defined by the owner
staff
# Filesize in bytes
2133
# Last modified date + time
30 Okt 19:56
# Filename
bash-cheatsheet.sh
# Show current Date
date
# Show current user
whoami
# Show all logged in users
who
# Show hostname
hostname
# Make a file executable for the owner
chmod u+x myexecutablefile
# Show processes of current shell
ps -f
# Execute process without STDOUT on screen
./my-script 1>/dev/null
# Run process in background
./my-script &
# Kill process (send SIGTERM -> can be ignored by the process)
kill 123
# Force kill process (send SIGKILL -> cannot be ignored by the process)
kill -9 123
# Kill last started background process
kill $!
# Execute script with Bourne-Shell
sh my-script
# Execute script with Korn-Shell
ksh my-script
# Set executing Shell within the first line of the script (Shebang)
#!<absolute-path-of-shell>
# Set Korn-Shell as executing Shell
#!/bin/ksh
# OR
#!/usr/bin/ksh
# Set Bash-Shell as executing Shell
#!/bin/bash
# OR
#!/usr/bin/bash
# Set Bourne-Shell as executing Shell
#!/bin/sh
# OR
#!/usr/bin/sh
# Execute script without Sub-Shell
#
# As shellscripts are started in a Sub-Shell (not in the current Shell)
# there is no way to change the environment variables of the current Shell.
# To execute a script within the current Shell just add a ". " before the script name.
. ./my-script
# OR with Bash-Shell there is the "source" command as an alternative to the dot notation
source ./my-script
# Show current Shell within script
$SHELL
# Split long command to two lines with a "\" after the first line
echo "This is a long text\
separated with backslashes\
within the script"
# Exit current script with exit code number 0-255
# Where 0 stands for "Everything worked fine"
# And all other numbers for "Something went wrong"
exit <exit-code>
# Exit with the exit code of the last executed command in the script
exit
# Show exit code of last executed script
echo $?
# Debug a script with -x option to output executed line with a prefixed "+"
# just before the execution
bash -x ./my-script
# Replace the "+" with a more meaningful name by setting the PS4 environment variable
export PS4='[--- Line: $LINENO ---] '
# Show current process id
echo $$
# Show home directory of current user
echo $HOME
# Program which works like a task manager on the console
top
# Check if the current shell is a login shell (a "-" before the shell name)
> ps -f
... -bash
# Find lines in file(s) by a specific search value
grep <search-value> <filename> [<filename>, ...]
# Find lines recursively by a specific search value
grep -r <search-value> <directory>
# Find files/directories by name (case-sensitive)
find <search-root-directory> -name <search-value>
# Find files/directories by name (case-insensitive)
find <search-root-directory> -iname <search-value>
# Find directories by name
find <search-root-directory> -type d -name <search-value>
# Find files by name
find <search-root-directory> -type f -name <search-value>
# Find all files with .test.js in its filename
find . -name "*.test.js"
# Find lines within files found by the find command
find <directory> -name <filename> | xargs grep <search-value>
# Find lines within .test.js files with "describe(" in it
find . -name "*.test.js" | xargs grep "describe("
# Only find lines within files tracked by Git
git grep <search-value>
# Highlight search values red within the result list of grep
grep --color <search-value> .
# Substitute call within bash command (called command substitution)
cat $(which idea)
# Lookup process using a specific port
lsof -i :1337
# OR
lsof -nP -i4TCP:1337 | grep LISTEN
# Watch or "run the same command over and over again"
watch <command>
# e.g. Watch conteiners within you local machine
watch docker container ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment