Table of Contents
Create a sudo user
# usermod -aG sudo username
Compress a directory
tar -zcvf file-name.tar.gz directory-name or zip -r file-name.zip directory-name
Uncompress a tar.gz file
tar -zxvf compress-file.tar.gz
Uncompress .gz file
gunzip compress-file.gz
or
gzip -d compress-file.gz
List the last modified files in a specific directory recursively
Find the last 10 modified files from a certain directory recursively:
find . -type f -printf '%T@ %p\n' | sort -k1,1nr | head -10
List all files older than x days recursively
Find the files older than 15 days in the current directory and subdirectories:
find . -type f -mtime +15
If you want to delete the files:
find . -type f -mtime +15 -delete
List all files newer than x days recursively
Find the files newer than 15 days in the current directory and subdirectories:
find . -type f -mtime -15
Recursively Search All Files For A String
Find all the files that contain the “foo” string in the current directory and in its subdirectories:
grep -ri -l "foo" .
Recursively renaming part of a filename
Rename recursively a part of a file name (ABC) with the XYZ text.
$ find . -name '*ABC*' -exec rename 's/ABC/XYZ/' {} \;
Print a file, excluding comments and blank lines
grep "^[^#;]" file grep -v '^$\|^\s*\#' file
To see the crontab
grep -v '^$\|^\s*\#' temp
To remove the time configuration in each line (to copy and paste the crontab commands to the console)
crontab -l | grep -v '^$\|^\s*\#' | cut -d" " -f6-
Restore a MySQL database with different name
First I have to create the backup from the old_database_name
mysqldump -uroot -p --routines --triggers old_database_name > database_backup.sql
Then I have to restore the backup in a new database: new_database_name
mysql -uroot -p -A -Dnew_database_name < database_backup.sql
Create a compressed MySQL dump and restore it
mysqldump -uroot -p databaseName | gzip > databaseName.sql.gz
gunzip databaseName.sql.gz | mysql -u root -p databaseName
Update the user password in a MySQL database
ALTER USER 'user'@'hostname' IDENTIFIED BY 'new-and-strong-password';
Delete commented or empty lines in a file
sed -i '/^[@#]/ d' file.txt sed -i -rn '/\S/p' file.txt
Execute a command each X seconds
Execute “ls | wc -l” each 5 seconds
watch -n 5 "ls | wc -l"
Symbolic link
ln -s origin destination ln -s /etc/nginx/sites-available/www.jesusamieiro.com /etc/nginx/sites-enabled/www.jesusamieiro.com
Get The Process Start Time And Date
ps -eo pid,lstart,cmd
Iptables
Add an IP to the whitelist
sudo iptables -A INPUT -s IPv4 -j ACCEPT
Add IPs from a file
sudo for IP in $(cat ip-file.txt); do echo "Enabling $IP"; iptables -A INPUT -s $IP/32 -d 0/0 -j ACCEPT; done
Add IPs from a remote URL
sudo for IP in $(wget -qO - https://uptimerobot.com/inc/files/ips/IPv4.txt); do echo "Enabling $IP"; iptables -A INPUT -s $IP/32 -d 0/0 -j ACCEPT; done
Show all the domains in Apache
apache2ctl -S | grep namevhost | grep 80 | awk -F ' ' ' { print $4 }' | sort
Show all the domains in NGINX
grep server_name /etc/nginx/sites-enabled/* -RiI | grep -v "#" | awk -F ' ' ' { print $3 "\n" $4 "\n" $5 }' | sort | uniq
Create a MySQL database and a user with permissions for that database
CREATE DATABASE my_dtabase CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'my_user'@'localhost' identified by 'my_password';
GRANT ALL PRIVILEGES ON my_database.* TO my_user@localhost;
FLUSH PRIVILEGES;
Create a random password
openssl rand --base64 64
Install a WordPress from the WP-CLI
$ wp core download --path=./wordpress --allow-root
$ wp config create --dbname=my_database --dbuser=my_db_user --dbpass="my_db_password" --locale=es_ES --path=./wordpress --allow-root
$ wp core install --url=my_url --title="Mi Site" --admin_user=my_admin_user --admin_password="my_admin_password" --admin_email=example@example.com --path=./wordpress --allow-root
Improve a WordPress installation
Update the core, plugins and themes. Delete the inactive plugins and themes. Set plugins’ auto-update.
wp core update --allow-root
wp theme delete $(wp theme list --status=inactive --field=name --allow-root) --allow-root
wp plugin delete $(wp plugin list --status=inactive --field=name --allow-root) --allow-root
wp plugin auto-updates enable --all --allow-root
wp theme update --all --allow-root
wp plugin update --all --allow-root
wp language core update --allow-root
wp language theme update --all --allow-root
wp language plugin update --all --allow-root
Measuring the download time
$ curl -w "\nStart: %{time_starttransfer} Total: %{time_total}\n" -o /dev/null -s -H "Accept-Encoding: gzip,deflate" $URL
$ curl -w "\nStart: %{time_starttransfer} Total: %{time_total}\n" -o /dev/null -s -H "Accept-Encoding: gzip,deflate" http://ipv4.download.thinkbroadband.com/1GB.zip
Change the default PHP CLI version
To make PHP7.4 the default CLI version.
$ which php7.4
which php7.4
sudo update-alternatives --set php /usr/bin/php7.4
Display the largest folders
Shows the 20 largest folders in a Linux machine. Beware, this could take a while.
$ find / -type d -exec du -Sh {} + | sort -rh | head -n 20
Display the largest files
Shows the 20 largest files in a Linux machine. Beware, this could take a while.
$ find / -type f -exec du -Sh {} + | sort -rh | head -n 20
:wq