Posts in the “linux-unix” category

How to sort the Linux 'ps' command output by RAM

With this site hosted on a virtual server, I'm fighting quite a battle over memory use with the new LAMP architecture in place. As I try to learn more about which applications are using the most memory, I ran into this cool ps command last night that sorts the ps output by memory use, specifically by the rss field:

ps aux --sort:rss

Here's the important output from that command:

The vim cut and paste commands

Vim FAQ: What are the vim cut and paste commands?

In vim you can cut and paste content fairly easily, but being an older text editor, these commands aren't obvious. Let's take a look at how to use cut and paste in vim.

The vim cut command

In short, the vim cut command is dd -- the letter 'd', typed twice. This command deletes the current line, but places it in a buffer, so you can easily paste it somewhere else. You use this command while in the vim command mode.

Scary thought of the day

A scary thought for me is that as many as 138,664 people may have learned about the Unix/Linux vi editor by watching my old vi/vim tutorial on YouTube. That was one of the first video tutorials I ever created, and what I should have done is (a) never publish it, and (b) keep re-recording it until I got a lot better.

(I was reminded of this when YouTube sent me an email last night to congratulate me on having over 1,000 followers.)

A Linux Bash if/or example

Here’s a quick Linux Bash if/else example featuring an or clause:

#!/bin/bash

day_of_week=`date +"%a"`

if [ "$day_of_week" == "Sat" ] || [ "$day_of_week" == "Sat" ]; then
  echo 'Weekend!'
else
  echo 'Not weekend!'
fi

The if/or clause is similar to other if/or expressions, and uses the || syntax shown.

RPI FAQ: How can I see what services are configured to run at startup on a Raspberry Pi? (Raspbian)

To see which services are configured to run at startup on a Rasperry Pi (Raspian) — and also see their current startup status — issue this Linux service command:

sudo service --status-all

The service man page describes what this command does:

service --status-all runs all init scripts, in alphabetical order, with the status command

On my Raspberry Pi (RPI) that command produces a long list of output that looks like this:

iptables restart tip - How to make your Linux iptables firewall automatically restart after a reboot

iptables restart FAQ: How do I make my iptables firewall start/restart after I reboot my Linux system?

I was going to write a tutorial about configuring a firewall on a Linux system using iptables, but then I found this great CentOS iptables tutorial, and I think they really nailed it.

The only thing I think they didn't fully cover is how you get your Linux iptables firewall to start up again properly after your Linux system is rebooted (which is kind of a huge deal). To that end, I thought I'd describe the process of getting your iptables firewall to restart after a system reboot.

And -- as an added bonus -- this same process will help you get any Linux service to automatically start after a reboot.

How to get the IP address of a Linux system from the command line

One way to to get the IP address of a Linux system from the Linux command line is this:

$ hostname -I

That’s the hostname command, followed by a capital letter i as a command line parameter. On my Raspberry Pi system, this command returns its IP address — and only its IP address — like this:

10.0.1.9

It’s nice that this command returns only the IP address, because that means I don’t have to pipe together several commands to get what I need.

How to configure an iptables firewall on CentOS 6

As a quick note to self, here’s how I configured the firewall rules on a new CentOS 6 Linux server recently.

First, I created an “undo” script at /root/undo-iptables with these contents:

#!/bin/sh

OUT=/tmp/undo-iptables.out

echo "running UNDO at `date`"      > $OUT
unalias mv                        >> $OUT 2>&1
mv /etc/sysconfig/iptables /tmp   >> $OUT 2>&1
/etc/init.d/iptables restart      >> $OUT 2>&1

I then made that file executable.

How to show the CentOS Linux version

CentOS Linux FAQ: What commands can I use to show what version of CentOS Linux I'm using?

There are at least two different ways to show what version of CentOS Linux you're using. First, you can use the Linux cat command on the /etc/redhat-release file. Here's the command, and sample output:

# cat /etc/redhat-release

CentOS release 5.6 (Final)

You can also use the lsb_release command with the -a option. Here's that command, and its output:

Show unique list elements with the Linux sort -u command

If you're ever working on a Unix or Linux system, and have a list with duplicated items in it, and want a smaller list of only the unique items in the list, the sort command is your friend.

I just ran into a situation where I generated a list of fonts on a Mac OS X (Unix) system, and my list ended up with a bunch of duplicated names, like this:

New, free Introduction to Unix and Linux tutorial

After having this on my "wish list" for many years, I finally took the time tonight to regenerate the pages for my "Free Introduction to Unix and Linux tutorial". The old format was just horrible, spread out among 285 small pages, and while this one still needs some work, it's about 1,000 times better than the old format, and comes in weighing only 13 large tutorial pages.

Some of the tutorial material is a little dated now, but these sections are still very relevant to today's Unix/Linux world:

Use vimdiff to see the difference between multiple files

I was just reminded of the Unix/Linux vimdiff command that lets you visually see the differences between multiple files. I normally just use vimdiff to look at the differences between two files, but I know it can also work with three files.

A quick vimdiff example with two files

To demonstrate how vimdiff works, here's a quick example. Suppose you have one file named getty1 that has these contents:

vim backup files - how to move

vim FAQ: How do I move vim backup files to another directory? (Those files that end with the "~" character.)

To move vim backup files to another directory, just use commands like these in your "vimrc" configuration file:

vi/vim arrow keys

vi/vim FAQ: Help, I'm on an older Unix system; what are the vi/vim arrow keys?

On older Unix systems, including HP-UX and other systems I've run across recently, you may not have access to the vim editor, and may just have access to the older vi editor. In that case the arrow keys on your keyboard may not work, and you'll have to use the "simulated" vi arrow keys on your keyboard to move up, down, left, and right. These vi arrow keys are:

vi/vim backspace key

vi/vim editor FAQ: What is the vi/vim backspace key?

In Vim, you delete the character under the cursor using the 'x' key while in command mode (which is the equivalent of a [delete] key), and to delete characters to the left of the cursor -- which is the equivalent of a vim backspace key -- use the capital letter 'X'. This lets you backspace over characters in vim, at least all the way back to the beginning of the current line.

The vim copy and paste commands

Vim FAQ: What are the vim copy and paste commands?

In vim you can copy and paste content fairly easily, but being an older text editor, these commands aren't obvious. Let's take a look at how to use copy and paste in vim.