linux-unix

recent posts related to linux and unix

Use sed to modify files in place

sed command FAQ: How can I use the Unix/Linux sed command to edit (modify) files in place?

The short answer is that you just need to use the -i or --in-place sed arguments, as shown in the sed man page:

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if extension supplied)

Then, if you have a file named 'hello.txt' with contents like this:

jello, world
this is a test

you can then run a sed command like this to modify that file:

Apache RedirectMatch wildcard examples

Apache Redirect 301 FAQ: How can I redirect many old web pages using the Apache Redirect or RedirectMatch syntax and wildcard patterns (regex patterns)?

I'm currently trying to fix a lot of URLs that I more or less intentionally broke when I deleted the old "directory" portion of this website. In short, after removing the directory, no URL at "/Dir" work any more, so I have thousands of broken URLs (technically "URIs") that look like this:

Linux yum command reference (examples)

Summary: A CentOS Linux yum command cheat sheet (reference page).

I've been working with the CentOS Linux yum command a lot lately, so I thought I'd create a yum reference page with a lot of examples while it's still fresh in my mind. I've organized the yum commands below by task (i.e., list, install, update, information, remove).

A shell script to download a URL (and test website speed)

I've been having a problem with a GoDaddy website lately (see my GoDaddy 4GH performance problems page, and in an effort to get a better handle on both (a) GoDaddy website downtime and (b) GoDaddy 4GH performance, I wrote a Unix shell script to download a sample web page from my website.

To that end, I created the following shell script, and then ran it from my Mac every two minutes:

An egrep example with multiple regular expressions

Summary: How to use the Linux egrep command with multiple regular expressions (regex patterns).

As a quick note here today, I just used the Linux egrep command to perform a case-insensitive search on multiple regular expressions (regex patterns). Really, what I did was a little more complicated:

locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'

As you can see from that command, I did this:

Unix/Linux - Redirect STDOUT and STDERR to the same file/location

Unix/Linux redirection FAQ: How do I redirect Unix STDOUT and STDERR to the same file with one command?

To redirect both STDOUT and STDERR to the same file with one Unix/Linux command, use this syntax:

my-shell-script.sh > /dev/null 2>&1

As you can see in this command, I'm redirecting STDOUT to /dev/null as normal, and then the special 2>&1 syntax tells your Bash shell to redirect STDERR to STDOUT (which is already pointing to /dev/null).

Find command - Find files not matching a pattern

Unix/Linux find "patterns" FAQ: How do I find files or directories that don't match a specific pattern (files not matching a regex pattern, or filename pattern)?

In my case I just ran into a situation where I needed to find all files below the current subdirectory that are NOT named with the filename pattern "*.html" . Fortunately with the newer Unix/Linux find syntax this solution is pretty easy, you just include the "-not" argument, like this:

The Linux chmod command

Linux chmod FAQ: Can you share some examples of the Unix/Linux chmod command? (Also written as, "How do I change permissions on Unix files and directories?")

The chmod command name stands for "change mode", and as that name implies, the chmod command is used to change the mode of Unix/Linux files.

I'll start with some simple Unix chmod command examples, then add some more details as we go along.

chmod - Make a script executable

The chmod command is commonly used to make a file "executable", like this:

The Linux cut command

Linux cut command FAQ: Can you share some Linux cut command examples?

The Linux cut command is a really great command filter to know. You can use it to do all sorts of cool things when processing text files and text in command pipelines.

Using the cut command with /etc/passwd

For a first cut command example, I'll use the /etc/passwd file on my Unix system. I use this file because fields in the file are separated by the ":" character, which make it very easy to work with.

The Linux chown command

Unix/Linux chown FAQ: How do I use the chown command?

The chown command is most commonly used by Unix/Linux system administrators who need to fix a permissions problem with a file or directory, or many files and many directories.

For instance, suppose you want files to be owned by the user "nobody", but when you issue an ls command, you see that they're owned by the user "fred", like this:

Syndicate content