Posts in the “linux-unix” category

vim search and replace

vi/vim search and replace FAQ: How do I search and replace in vim?

A lot of people ask me how to perform a global search and replace operation in the vi (vim) editor. The vim editor is anything but intuitive, but for some reason I can remember this global search and replace syntax pretty easily.

A vim pattern search and delete example

vim FAQ: How do I perform a vim “search and delete” using a vim regular expression pattern (regex)?

Every once in a while when I’m using the vi editor (or vim) I find myself in a situation where I need to delete a bunch of lines in the file that match a particular pattern. In my younger days I used to get out of vi and then use sed or grep to get rid of those lines, but it turns out there’s a real easy way to do this in vi.

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:

Linux sed command: How to modify the content of many files

Linux sed FAQ: How can I use the sed command to modify many files at one time?

Here's a Linux/Unix sed command file that I just used to modify a bunch of files:

s/^M//g
1,2d
$d

I put those three lines in a text file named sed.cmds.

After that, I call that sed script from a simple shell script. Here are the contents of the shell script:

Linux ‘find’ example: How to copy one file to many directories

Did you ever need to take one file on a Linux or Unix system and copy it to a whole bunch of other directories? I had this problem recently when I changed some of the header files on this website. I had a file named header.html, and I needed to copy it to a bunch of subdirectories.

Using Unix, Linux, or Cygwin this turns out to be really easy. I used the Linux find command, in combination with the cp command. Once I figured out the right syntax, I was able to copy the file to nearly 500 directories in just a few seconds.

The Linux `chown` command

Unix/Linux file ownership 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:

How to show line numbers in the vi/vim editor

vim FAQ: How do I show line numbers in the vi/vim editor?

I’m frequently asked if there’s a way to show line numbers in vi (or vim). That is, can you take a normal vim display like this:

aaa
bbb
ccc

and then get vim to show line numbers before each line in the editor, like this:

1 aaa
2 bbb
3 ccc

The answer is a resounding yes.

How to show vim line numbers (vim set number)

You show vim line numbers by issuing this vim “set number” command:

Linux ‘find’ command recipes

Thinking about my own work when using Linux and Unix systems, a lot of the work is based around files, and when you're working with files, tools like the Linux find command are very helpful. So, I've decided to put together this list of find command examples/recipes that I'll update from time to time when I use the find command in different ways.

How to find all files beneath the current directory that end with the .jsp extension:

Linux ‘locate’ command examples

Linux FAQ: Can you share some examples of how to use the Linux locate command?

Background

Sure. The locate command is used to find files by their filename. The locate command is lightning fast because there is a background process that runs on your system that continuously finds new files and stores them in a database. When you use the locate command, it then searches that database for the filename instead of searching your filesystem while you wait (which is what the find command does).

A Linux mail command tip

If you're ever working on a Unix or Linux system, and need to email a file to someone else, it may be helpful to know that you can send your email message right from the Unix command line. You don't need to hop into mutt or any other Unix or Linux mail client to email the file.

Linux shell scripts: How to increment a counter in a shell script

Unix/Linux shell script FAQ: Can you share a simple Linux shell script that shows how to count, i.e., a shell script that increments a counter in a for loop or while loop?

Sure, I just needed to increment a while loop counter myself, so I thought I'd share my example shell script code here.

How to read and write from a file in a Linux bash shell script

I’m currently writing a complicated Linux bash shell script where I need to keep a counter in an external file, and to do so, I need to be able to write to a file and then read from that file.

In short, this is how I write my counter to that file:

# create a variable to represent the filename
COUNTER_FILE="counter.tmp"

# write to the file
echo "0" > $COUNTER_FILE

Later in the code I increment the counter and write it to the file like this:

Using the Linux ‘find’ command with multiple filename patterns

Someone asked me the other day how they could search for files with different names with one Linux find command. They wanted to create a list of all files that ended with the extensions .class and .sh.

Although this is actually very easy to do with the find command, the syntax is obscure and probably not well documented, so let's look at how to do this.

The Linux wc command (word count)

The Linux word count command is named wc. The wc command counts the number of characters, words, and lines that are contained in a text stream. If that sounds simple or boring, it's anything but; the wc command can be used in Linux command pipelines to do all sorts of interesting things.

Let's take a look at some Linux wc command examples to show the power of this terrific little command.

The Linux `chmod` command

Linux file permissions 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 examples, then add some more details as we go along.

Linux find: How to search multiple directories with find

Problem: You need to use the Unix/Linux find command to search multiple folders. Specifically, you'd like to search several folders beneath your current location, but not all folders.

For example, your current directory may have 20 subdirectories, and you don't want to search them all like this:

find . -name "*.java"

or this: