Linux useful commands

Download a file from internet


# if you want to save it with a different name
wget -O
# please not that -O is not zero but capital O

# Form more details about wget, see the manual of the command:
man wget

Working with zip archives:

# create an archive from a file
zip /path/to/file/dump.sql

# create an archive from a directory
zip -r path/to/directory/

# Extract/Unarchive a zip file

# For more details about zip and unzip see the manual
man zip
man unzip

GNU Zip archivator

# Create a gzip archive
gzip file.sql # this command will create file.sql.gz and will delete file.sql

# Extract a gzip archive
gunzip file.sql.gz # this command will extract archive into file.sql and will delete the archive file

Tar archivator

# Create a tar.gz archive
tar -czvf file.tar.gz /path/to/directory/

# Extract a tar.gz archive
tar -xzvf file.tar.gz

# for more details see the command manual
man tar

See first lines of a file

# See the first 10 lines of the file
head file.log

# See the first 50 lines of the file
head -50 file.log

See the last lines of a file

# See the last 10 lines of a file
tail file.log

# See the last 50 lines of a file
tail -50 file.log

# Watch the file for changes
tail -f file.log # press CTRL + C to exit

Working with files and directories

Create files

# Create an empty file
touch filename.txt
# or using an editor
vim filename.txt # type something and then save ":x"

# Create a file from another file (duplicate)
cp source_file.txt copied_file.txt
# or
cat source_file.txt > copied_file.txt

# Concatenate content of more files into one single file
cat file1.txt file2.txt file3.txt > all.txt

# Show content of both files
cat file1.txt file2.txt

# Create a file with content
echo "My content" > my-file.txt

# Write more lines to a file
cat << EOF > /etc/docker/daemon.json
	"exec-opts": ["native.cgroupdriver=systemd"]
# the above example will create a json file `/etc/docker/daemon.json` with the json content provided


Create directories

# Create a directory in current location
mkdir my_directory

# create a directory including parent directories
mkdir -p /path/to/my/directory/

# create a directory as root
sudo mkdir -p /path/to/directory/

Operations with files and directories

# Move a file to a directory
mv a.html path/to/dir/

# Rename a file
mv orinal_name.txt new_name.txt

# Delete a regular file
rm file.txt

# Delete a directory and all it's content
rm -rf path/to/dir/

# view directory content (list of files and directories)
ls -la

# view directory content with size displayed in MB, GB
ls -lah

# list files and directories in a directory
ls -la /path/to/directory

# list files and directories from home directory
ls -la ~

# Change user and group or a file or directory
chown -R <user>:<group> path/to/file_or_directory
# -R means recursive
# Example:
chown -R ubuntu:ubuntu /var/logs/my_directory

# Set file or directory permissions to my user and group
sudo chown $(id -u):$(id -g) path/to/dir_or_file

Working with databases from terminal

# If you are root you can connect with the following command:
mysql -uroot

# Connect to mysql server
mysql -hhostname -uusername -p 

# or specifying the database name too
mysql -hhostname -uusername -p database_name

# or specifying the database name and password
mysql -hhostname -uusername -ppassword database_name

# Import sql file into mysql
mysql -hhostname -P3306 -uusername -p database_name < database_dump.sql
# Example
mysql -h10.10.0.1 -P3306 -udemo_user -p database_name < database_dump.sql

# Exporting database to file
mysqldump -uusername -ppassword dbname -hhost > dump.sql
If you use the default values for database connection (hostname=localhost, port=3306) you don't need to add them.

Mysql queries in terminal

# Show databases on server available to current user

# Show users and hosts on server
SELECT User, Host FROM mysql.user;

# Select and use database
USE my_database;

# Show tables from selected database

# or
SHOW TABLES FROM other_database;

# Show table structure
DESC table_name;

Running internal php server locally

php -S localhost:8000 # instead of 8000 you can specify other port

# make php server deliver content to any ip 
php -S -t path_to_dir # -t path_to_dir is optional