Last active
April 20, 2022 07:58
-
-
Save luukverhoeven/db4a14c236d60272ccf678a9913451a9 to your computer and use it in GitHub Desktop.
A list of handy bash commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bash Notes | |
# Silver search | |
ag -G "/settings.php" courses | |
ag -G "/version.php" format_grid /home/ | |
# Search by type | |
cd /; sudo find . -depth -type d -name .gnupg | |
# Search plugin Moodle the fast way | |
ag block_coursespoints --file-search-regex version.php --silent --ignore-case --skip-vcs-ignores --ignore={vendor,moodledata,public_old,node_modules} /home/ | |
# FPM stats | |
top -bn1 | grep php-fpm | |
# Remove all except latest | |
ls -t | tail -n +2 | xargs -I {} rm {} | |
ls -t | tail -n +4 | xargs -I {} rm {} | |
# remove files older than a month in current directory | |
find . -mtime +40 -exec rm {} \; | |
#remove files older then a week | |
find . -mtime +7 -exec rm {} ; | |
# monitoring cpu and processes | |
ps -eo pcpu,pid,user,args | sort -k 1 -r | head -20 | |
netstat -tanp | |
ps -efH | |
#dump all traffic to a file | |
tcpdump -i eth0 -s 65535 -w dump1.tcpdump | |
#remove files older then a week | |
find . -mtime +7 -exec rm {} \; | |
#remove files older than a month in current directory | |
find . -mtime +40 -exec rm {} \; | |
#dump all traffic to a file | |
tcpdump -i eth0 -s 65535 -w dump1.tcpdump | |
# Import mysql | |
time mysql --defaults-file=/etc/mysql/debian.cnf DATABASENAME < FILENAME.sql | |
# Snmp check | |
snmpwalk -v 2c -c public 192.168.1.1:163 | |
snmpwalk -v 2c -c public 192.168.1.1:164 | |
# replace all in directory. | |
find /home/www/ -type f -exec sed -i 's/SEARCH/REPLACE/g' {} \; | |
# search in web files. | |
grep --include=\*.{css,php,js} -rnw $(pwd) -e "YourSearchText" | |
# find in content. | |
find . -name "*.php" -print | xargs grep "YourSearchText" | |
echo "content of mail body" | mail -s "Mail Server Tester" example@example.net | |
# Mysql import sql. | |
mysql --defaults-file=/etc/mysql/debian.cnf DB < FILE.sql | |
# Check cron errors | |
cat /var/log/syslog | grep cron | |
# Check if cron is working you will see a number | |
pgrep cron | |
# Check disk reads | |
sudo hdparm -tT /dev/sda | |
# Write | |
dd if=/dev/zero of=/tmp/output bs=8k count=10k; rm -f /tmp/output | |
# Interval a command | |
watch -n 60 php admin/cli/purge_caches.php | |
Reverse DNS
dig +noall +answer -x THEIP
Set new date on server for testing
date -s '2014-12-25 12:34:56'
grep -Rhi --with-filename "SSLCipherSuite" .
sed -i -- 's/php_admin_flag/#/g' *
Nameserver check
dig @8.8.8.8 NS domain.com
dig domain.com +noall +answer
System
uname -a # shows info about the system (kernel, architecture...)
sudo lshw # (only Linux) list all the hardware components
sudo lshw -short # (only Linux) list a summary table with the hardware components
lscpu # (only Linux) show information about the CPU
lsusb # (only Linux) list the plugged USB devices
date # print the current date and time
Processes
ps # shows the processes running right now on this terminal session
ps aux # shows all the processes running on the computer
htop # display an activity monitor in the terminal
Change users
su # change current user to root
su - # change current user and local variables to root
su maria # change current user to "maria"
sudo nano # runs command "nano" as root user
Filter the output
cat verylongfile.txt | more # shows the content of verylongfile.txt split in pages
cat verylongfile.txt | less # shows the content of verylongfile.txt split in pages, with support for scrolling and backwards navigation
cat verylongfile.txt | grep "I love you" # shows the lines where the string "I love you" is found
Variables and Path
echo "hello" # prints hello in the console
echo $USER # prints the value of the variable USER in the console
env # prints all the environment variables
set # prints all the local variables
MY_FRIENDS=William # create a local variable called MY_FRIENDS
MY_FRIENDS=$MY_FRIENDS:John:Anna # concatenate values 'John' and 'Anna' to MY_FRIENDS
export MY_FRIENDS # makes local variable MY_FRIENDS an environment one
unset MY_FRIENDS # deletes the variable MY_FRIENDS
export PATH=$PATH:/home/john/myprogram # add the directory /home/john/myprogram to the path
which nano # shows where is the executable of "nano" located
whereis nano # shows where are the executable, manuals, source code, etc. of "nano"
Directories
pwd # get current directory
cd # change current directory to home
cd /bin # change current directory to /bin
cd .. # go to parent directory
Directories
rmdir movies # remove the empty directory movies
rm -rf movies # remove the directory movies and its files
rm file1.txt # remove the file file1.txt
mv /home/michael/myfile.txt /home/john/important.txt # move a file from /home/michael to /home/john
cp /home/michael/myfile.txt /home/john/important.txt # copy a file from /home/michael to /home/john
cp -R letters/ memories/ # copy the contents of the folder "letters" inside the folder "memories"
cp -R letters ~/Desktop # copy the folder "letters" to the Desktop
df -k . # free space in the current directory
df -k # free space in all the mounted volumes
Search / replace
find . -name hello.txt # finds a file called "hello.txt" in the current directory
find /home/joe/Downloads -name *.pdf # finds PDF files in the Downloads folder
find / -executable -atime -10 # find an executable file in the root directory that was accessed less than 10 minutes ago
find ~ -newer reference.txt # find a file in the home directory edited after the file "reference.txt"
sed -i "s|-Lsd|-LS4d|" /etc/default/snmpd
Compress and decompress TAR/ZIP files
# Tar / untar
tar -cvzf myfile.tar myfolder # compress the folder "my_folder" in the file "myfile.tar"
tar -xvzf myfile.tar # decompress the file myfile.tar in the current directory
tar -C /opt/abc -xvzf myfile.tar # decompress the file myfile.tar in /opt/abc
# Zip / unzip
zip -r myfile.zip myfolder # compress the folder "my_folder" in the file "myfile.zip"
unzip myfile.zip # decompress the file myfile.zip in the current directory
Reading Files
# grep: filter by lines
grep 455 sales.txt # print the lines where there is a 455
# cut: show some columns
cut -f 3 sales.txt # print only the third column
cut -f 2-3 sales.txt # print only the 2nd and 3rd columns
cut -c 9-19 sales.txt # for each line, print only from the 9th to the 19th character
cut -f 1 -d ';' clients.txt # the file clients.txt uses ';' as delimiter, instead a tab
# awk: the same than cut, but more flexible with delimiters, with more filters, and more computacionally complex
awk '{print $3}' sales.txt # print only the 3rd column
awk '{print $2 ", " $3}' sales.txt # print the 2nd and 3rd columns, separated with a comma
awk '{print $2 + $3}' sales.txt # print the sum of the 2nd and 3rd columns
awk 'length($0) > 20' sales.txt # print those lines whose length is longer than 20 characters
awk '$2 > 100' sales.txt # print those lines where the value of the second column is greater than 100
# wc
wc sales.txt # print the number of lines, words, and characters in the text file
ls -1 | wc # print the number of lines, words, and characters for each of the files in the current directory
# sort: sort the lines of a file according an alphabetic criteria
sort sales.txt # sort the lines of the file
sort -r sales.txt # sort the lines in reverse order
sort -R sales.txt # sort the lines in random order
sort -t $'\t' -k 3n,3 sales.txt # sort the lines numerically according the 3rd column
# uniq: shows the consecutive non-repeated or duplicated lines of a file
uniq hello.txt # skips consecutive duplicated lines
uniq -d hello.txt # show only consecutive duplicated lines
uniq -u hello.txt # show only lines that are not duplicated
uniq -c hello.txt # for each line, show the number of times it is duplicated
Monitoring
free -h -w -s 3 #Free memory
iotop # writes/reads disk
htop # Improved top
iptraf # Wireshark alternative without GUI
journalctl -u snmpd --follow
SSL info
openssl s_client -showcerts -servername DOMAIN.nl -connect DOMAIN.nl:443
Speedtest
iperf3 -s
iperf3 -c 192.168.32.10
iperf3 -c 192.168.32.1 -p 5200
<?php
$input = file_get_contents("php://input");
error_log(print_r($_REQUEST, true). PHP_EOL . $input, 3, __DIR__ . '/dump.txt');
Find installed plugins between 2 moodle folders
diff <(find public_html/ -printf '%P\n' | sort) <(find public_html_old/ -printf '%P\n' | sort) | grep '^[>]' | grep 'version.php' | rev | cut -c12- | rev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
find . -type f -exec sed -i 's/192.168.86.91/192.168.32.21/g' {} \;