linux-unix

recent posts related to linux and unix

Linux cut command example with field and delimiter

Should you ever run into a situation where you want to use the Linux cut command by specifying both a field number and field delimiter, I hope this example is helpful for you.

I was just working on a problem where I wanted to list all the fonts on a Mac OS X (Unix) system, and needed to use the cut command this way. A straight listing of all the filenames in the Mac font directory gave me a long list of names like this:

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:

Debug Linux shell scripts with -x

If you ever need to debug a Unix or Linux shell script, you can just add the -x option to the interpreter when you invoke it. You can add the shell's debug option on the command line like this:

sh -x misbehaving_script.sh

or at the end of the first line in your shell script (the she-bang line), like this:

#!/bin/sh -x

I was just working on a shell script related to fonts on a Mac OS X (Unix), system, and when I tried to run it normally, all it gave me was this error message:

Recursive grep searching with grep -r (like grep with find)

For years I've always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match my grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, "Search all files in all subdirectories of the current directory for the string 'alvin', and print the filenames that contain this pattern." It's an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify.

Linux "crontab every" examples

Linux crontab FAQ: How do I schedule Unix or Linux crontab jobs to run at intervals, like "Every five minutes", "Every ten minutes", "Every half hour", and so on?

Use sed to edit files in place (and make a backup copy)

Yesterday I ran into a situation where I had to edit 250,000 files, and of course I instantly thought of the Unix/Linux sed command. I knew what edit commands I wanted to run (simple swap/replace commands), but my bigger problem was how to edit the files in place.

A quick look at the sed man page showed that I needed to use the -i argument of the sed command:

How to gracefully restart Nginx

Nginx FAQ: How do I gracefully restart Nginx?

On a Unix or Linux system, you can restart Nginx without having to manually stop it and start it by sending a HUP signal (a "hangup" signal) to the main Nginx process. So, if the process id happened to be 1234, you would issue this command:

kill -HUP 1234

The easy way to get the actual Nginx process id is by looking at the nginx.pid, which is kept in the Nginx log directory by default. On my server, the nginx.pid file is located here:

How to view HTTP headers from the command line using curl

I've been working a lot on the next generation web server for the devdaily.com website lately, and as I'm looking at different HTTP accelerators, I keep wanting/needing to look at the headers returned by my web pages. A simple way to look at the HTTP headers from the command line is with the curl command, like this:

curl -I http://example.com/

Running this command against the main Google website, I see output like this:

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:

Unix/Linux grep command examples

Linux grep commands FAQ: Can you share some Linux/Unix grep command examples?

Sure. The name "grep" means "general regular expression parser", but you can think of the grep command as a "search" command for Unix and Linux systems: it's used to search for text strings and more-complicated "regular expressions" within one or more files.

I think it's easiest to learn something like the grep command by showing a collection of examples, so let's jump into some examples.

Syndicate content