Post

Bash Common Commands

Files

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




Folders

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




File / Folder Permissions

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




Environmental Variables

1
printenv
set variable
1
export VAR_NAME="hello"
1
echo $VAR_NAME
remove variable
1
unset VAR_NAME




Scripting

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




Networking

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

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

nmap -sP <ip_addr>/<ip_mask>
list all available hosts, within an ip range, on specific ports
1
2
3
4
5
6
7
# 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
1
ssh <user_name>@<host_name>
connect, on specific port
1
ssh -p <port_number> <user_name>@<host_name>
connect, using keyfile
1
ssh -i <identity_filename> <user_name>@<host_name>
remove ssh key
1
ssh-keygen -R <server_dns> > /dev/null 2>&1;
copy remote file to local
1
scp <user_name>@<server_dns>:<remote_location> <local_location>
This post is licensed under CC BY 4.0 by the author.