Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Last active November 23, 2022 20:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save apolloclark/1ba73c73b4e75a898ae8 to your computer and use it in GitHub Desktop.
Save apolloclark/1ba73c73b4e75a898ae8 to your computer and use it in GitHub Desktop.

Bash Cheatsheet

The following is a list of various Bash commands which I use on a regular basis. It's not exhaustive, just a simple "getting started", and collection of useful commands. Anything between the brackets "<>" should be replaced.

Collection of various Bash commands:

Files

list files
ls
list all files
ls -la
create file
touch <file_name>
rename file
mv <file_name> <new_file_name>
copy file
cp <file_name> <file_name>
delete file
rm -f <file_name>
print file contents
cat <file_name>
scroll long file
less <file_name>
print first N lines
head -<n_lines> <file_name>
print last N lines
tail -<n_lines> <file_name>
search for filename's location
locate <file_name>
search for program's location
which <program_name>
search for a file, containing a string
find . -type f -print0 | xargs -0 grep "<search_string>"




Folders

show current folder
pwd
list sub-folders
ls -d */
change directory
cd <directory_name>
change to home folder
cd ~
create folder
mkdir <folder_name>
rename folder
mv <folder_name> <new_folder_name>
delete folder
rmdir <folder_name>




File / Folder Permissions

list permissions
ls -la
set executable
chmod +x <file_name>
set ownership
chown -R <user_name> <folder_name>




Environmental Variables

print existing variables
printenv
set variable
export VAR_NAME="hello"
print variable
echo $VAR_NAME
remove variable
unset VAR_NAME




Scripting

set script to be executable
chmod +x ./script_name.sh
set the executable flag on all .sh files
find ./ -name "*.sh" -exec chmod +x {} \;
run script
./script_name.sh
run script, but keep environmental variables
. ./script_name.sh
source ./script_name.sh
associative arrays
@see http://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/




Networking

list network adapters (Debian, Ubuntu, Kali)
ifconfig -a
list network adapters (Redhat, Fedora, CentOS)
ip link show
flush dns cache (Debian, Ubuntu, Kali Linux)
sudo /etc/init.d/dns-clean
restart networking (Debian, Ubuntu, Kali Linux)
sudo service network-manager restart
list all open ports
sudo netstat -tunlp

nmap <127.0.0.1 | localhost | ip_addr>
find if specific port is open
sudo netstat -tunlp | grep <port_number>
list all available hosts, within an ip range
nmap -sP 192.168.1.1/24

nmap -sP <ip_addr>/<ip_mask>
list all available hosts, within an ip range, on specific ports
# custom TCP SYN scan
sudo nmap -sP -PS22,3389 192.168.1.1/24
sudo nmap -sP -PS<port_1>,<port_2> <ip_addr>/<ip_mask>

#custom UDP scan
sudo nmap -sP -PU161 192.168.1.1/24
sudo nmap -sP -PU<port_1> <ip_addr>/<ip_mask>




SSH commands

connect
ssh <user_name>@<host_name>
connect, on specific port
ssh -p <port_number> <user_name>@<host_name>
connect, using keyfile
ssh -i <identity_filename> <user_name>@<host_name>
remove ssh key
ssh-keygen -R <server_dns> > /dev/null 2>&1;
copy remote file to local
scp <user_name>@<server_dns>:<remote_location> <local_location>




Hardware

http://www.haifux.org/lectures/86-sil/kernel-modules-drivers/kernel-modules-drivers.html

list all PCI system devices
lspci

list all running kernel modules

lsmod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment