Quickzi: How To Remove Blank Lines from a File

If you want to remove all blank lines from a file, a quick way of doing it from the Linux command line is to use sed.

So, lets say that your file looks like this:

# vi example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.

sed is a stream editor for filtering and transforming text

There are a lot of powerful things you can do with sed.

:wq

Now, to remove the blank lines in this file, we’ll use sed.

# sed -i '/^$/d' example_file

# cat example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.
sed is a stream editor for filtering and transforming text
There are a lot of powerful things you can do with sed.

If you want to preserve the original file, then you can pipe the changes to a new file:

sed '/^$/d' example_file > example_file_new

Cheers!

Quickzi: How To List Files with a Certain Date Stamp

Here is a quick tip on how to list files in a directory with a certain date stamp using awk.

Let’s say that you want to list all files stamped with 2008-04-11.

# ls -l | awk '{if($6=="2008-04-11") print $N }'

-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file1
-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file2
-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file3

If you want to list only the file names, add an F switch.

# ls -l | awk '{if($6=="2008-04-11") print $NF }'
file1
file2
file3

Quickzi: How To Add a Welcome Message for SSH Users

Here is a quick tip on how to add a welcome message for your SSH users.

If you want users to see a banner welcome message when connecting to your SSH server, you need to turn on the banner configuration of SSHd and then create a banner file.

Step 1:

Create a banner file that contains text you want people to see when connecting to your SSH server.

Create and open the banner file:

# vi /home/adam/banner

Add your text:

This is the banner file for Foogazi.com! Welcome!

Write the file and quit:

:wq

Step 2:

Edit sshd_config to set a default banner path.

# vi /etc/ssh/sshd_config

then add the following to the config file:

Banner /path/to/banner

Write the file and quit:

:wq

Step 3:

Restart the sshd server.

# /etc/init.d/sshd restart

Step 4:

SSH to your server and test to see if the banner is working:

# ssh adam@foogazi.com
This is the banner file for Foogazi.com! Welcome!

adam@foogazi.com's password:
#

It’s working!

Quickzi: How To Set Cron to Run Every 5 Minutes

Here is a quick tip on how to run crontab every 5 minutes.

*/5 * * * * /home/adam/script.sh will execute script.sh every 5 minutes.

Also, here’s a quick guide to understaning the layout of cron:
# MIN HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
5 * * * * echo 'Hello'

Also, the Crontab Man page

For further reading on Crontab check out Understand Cron Jobs in 5 Minutes

Quickzi: How To Change PostgreSQL Root Password

Here is how to change the PostgreSQL root password from the Linux command line. Replace “root” with any user name to change any password. It is important to note that PostgreSQL does not have an actual “root” user name. The “postgres” user is the super-user for PostgreSQL.

# su postgres
# psql -d template1
template1=# ALTER USER postgres WITH PASSWORD '${POSTGRESQL_POSTGRES_PASSWORD}';

You can replace “postgres” with any user name.

Quickzi: Find out when a user account expires

The chage command changes the number of days between password changes and the date of the last password change. This information is used by the system to determine when a user must change his/her password.

Example:

adam@desktop:~$ sudo chage -l adam
Last password change : Mar 02, 2008
Password expires : Apr 11, 2008
Password inactive : never
Account expires : Mar 30, 2009
Minimum number of days between password change : 20
Maximum number of days between password change : 40
Number of days of warning before password expires : 7
adam@desktop:~$

Read the man page of chage here.