How to clear the contents of a file from the command line?

When developing, I usually have a log file for tests and development for me to keep track of what’s happening. The thing is that logs become too large. Some editors or viewers can’t open the file. They are sometimes too large to be usable.

I just delete the contents and fill it up until the next time.

Here are a few ways to delete file contents from the command line.

> filename

> log/development.log

echo

echo "test" > log/test.log

cat

cat /dev/null > log/test.log

There are probably millions of ways to do this but the above are the quickest

cp

cp /dev/null log/development.log 

 MySQL: Backup Database with mysqldump from the Command Line

Backups are an essential part of development. You always want backups. mysqldump is the goto program to backup mysql databases

Backup a database

mysqldump database_name > database_dump.sql

Backup a database with username and password

mysqldump -u username -p password database_name > database_dump.sql

Backup a database with username and password and compressed

Gzip
mysqldump -u username -p password database_name | gzip > database_dump.sql.gz
Bzip
mysqldump -u username -p password database_name | bzip2 > database_dump.sql.bz2

 Nginx Essential Commands

NGINX is a free, open-source, high-performance HTTP server. NGINX usage has been growing in the past few years and you should at least learn some commands that you could use with NGINX.

First, Install NGINX

Debian/Ubuntu

apt-get install nginx

CentOS/RHEL

yum install nginx

Now, let’s get to the essential commands

Check NGINX status

$ service nginx status
$ service nginx status
nginx found running with processes:  15423 15422 15421 15420 15418 15417 15416 15414 15412

Stop NGINX

$ service nginx stop
$ service nginx stop
 * Stopping Nginx Server...                                                                                                                            [ OK ]

Start NGINX

$ service nginx start
$ service nginx start
 * Starting Nginx Server...                                                                                                                            [ OK ] 

Restart NGINX

This will do the above commands, stop and start

$ service nginx start
$ service nginx restart
 * Stopping Nginx Server...                                                                                                                            [ OK ] 
 * Starting Nginx Server...                                                                                                                            [ OK ] 

Reload NGINX Configuration

$ service nginx reload
$ service nginx reload
nginx: the configuration file /etc/conf/nginx.conf syntax is ok
nginx: configuration file /etc/conf/nginx.conf test is successful
 * Reloading (via HUP) Nginx Server...                                                                                                                        
Hangup

Come on and try these commands out on environment!

 How to Compress a Folder using Tar

How do you compress a folder using the Tar Command?

Every time I need to compress a file, I google the answer. I am quite tired of that and decided to write myself mine. Here’s a good tutorial about compressing files using the Tar Command.

1 Go to the location of the folder

$ cd /location

2 Use the tar command to compress the file

The important aspect of the command below is the -c which translates to compress

$ tar -zcvf a folder.tar.gz system
system/
system/popups/
system/popups/photos/
system/popups/photos/000/
system/popups/photos/000/000/
system/popups/photos/000/000/001/
system/popups/photos/000/000/001/photo_thumb/
system/popups/photos/000/000/001/photo_thumb/Free-Phyto.jpg
system/popups/photos/000/000/001/photo_main/
system/popups/photos/000/000/001/photo_main/Free-Phyto.jpg
system/popups/photos/000/000/001/original/
system/popups/photos/000/000/001/original/Free-Phyto.jpg

Voila! A compressed file!

$ ls
-rw-rw-r--  1 group group 190M Nov  4 13:25 folder.tar.gz

How to Extract a Tar Compressed File

In the last tutorial, we compressed a folder using the Tar command. Here’s another for extracting from a tar compressed file.

1 Go to the location of the Tar compressed file

cd /location

2 Use the Tar command

The important aspect of the command below is the -x which translates to extract

$ tar -zxvf folder.tar.gz 
system/
system/popups/
system/popups/photos/
system/popups/photos/000/
system/popups/photos/000/000/
system/popups/photos/000/000/001/
system/popups/photos/000/000/001/photo_thumb/
system/popups/photos/000/000/001/photo_thumb/Free-Phyto.jpg
system/popups/photos/000/000/001/photo_main/
system/popups/photos/000/000/001/photo_main/Free-Phyto.jpg
system/popups/photos/000/000/001/original/
system/popups/photos/000/000/001/original/Free-Phyto.jpg
ls -lah
drwxrwxr-x  9 group group 4.0K Nov  4 10:12 system

Bookmark this Tutorial for reading. Thanks!