Posts in the “linux-unix” category

Unix: How to find files with multiple filename extensions

As I mentioned in my How to find multiple filenames with Linux find tutorial, you can use find command syntax like this to find files with multiple filename extensions:

find iTunes \( -name "*.mp3" -o -name "*.m4a" \)

As that command shows, I ran this find command to find all of my music files under my iTunes directory, including .mp3 and .m4a filename extensions.

While I’m in the neighborhood, this is the full find command I use to backup all of my iTunes files that have changed or been added in the last 180 days:

find iTunes \( -name "*.mp3" -o -name "*.m4a" \) -type f -mtime -180 -print0 | xargs -0 tar rvf NewMusic.tar

There’s probably an easier way to do this, but that backup command works for me.

Unix/Linux: How to use sudo and curl to download and install a command

If you ever need a Linux/Unix shell command to download and install another command using curl and sudo privileges, I just used this command to install a command-line tool named Ammonite:

sudo sh -c '(echo "#!/usr/bin/env sh" && \
curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.0.4/2.13-2.0.4) \
> /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm

I show the command on multiple lines, but you can also make it a one-line command, as it is on the Ammonite page. As shown, this runs the sudo command, which requires your admin password, and then it runs the curl command inside a Unix/Linux shell environment, writing its output to /usr/local/bin/amm.

How to use one version number variable in many Linux shell scripts

As a brief note to self, I currently have a situation where I have about ten shell scripts, and each script uses the same jar file, but I keep changing the version number of that jar file each time I have a new release of my Static Drupal application. For example, my current jar file has this name:

Static-Drupal-assembly-1.2.jar

So to keep all of those shell scripts using the correct, current version of my jar file, I created a file named VERSION that has this version variable:

version=1.2

Then in my other shell script files, the first thing I do is “source” that file to get the version variable into those other shell scripts. One of the first lines in my ten shell scripts looks like this:

. ./VERSION

By doing this, I now only have to change the version number in that first file, and it’s automatically picked up in all of my other shell scripts.

A Linux shell script that shows find and tar with multiple image filenames

As a brief “note to self,” this is the Bourne shell script I use to copy images from my Drupal 8 sites directory to the same directory on my new “Static Drupal” website:

tarFile=newImagesFromDrupalSitesDir.tar
drupalHtmlDir=/drupal8/html
staticHtmlDir=/staticDrupal/html

cd $drupalHtmlDir
rm $tarFile 2> /dev/null

# create a tar file containing all new images
find sites -type f \( -name "*.jpg" -o -name "*.jpeg"  -o -name "*.png" -o -name "*.gif"  \) -mtime -2 -print0 | xargs -0 tar rvf $tarFile

# TODO make sure the tar file exists
if [ -e $tarFile ]
then
    echo "tar file exists, moving it to $staticHtmlDir"
    mv $tarFile $staticHtmlDir
    cd $staticHtmlDir
    tar xvf $tarFile
    rm $tarFile
else
    echo "POSSIBLE ERROR: the tar file DOES NOT exist"
fi

I changed the three initial variable names, but the rest of the script shows one possible way to copy all of the images in the original sites directory into the new Static Drupal directory. If you wanted to see things such as how to use multiple filenames with the Linux find command, or how to use the find command to create a tar file, I hope this example is helpful.

An Apache name based virtual host (NameVirtualHost) tutorial

Apache virtual hosting FAQ: How do I configure Apache to run multiple virtual hosts (name-based virtual hosts)?

An Apache name-based virtual host (NameVirtualHost) tutorial: As a quick reminder to myself for something I just did using MAMP on my Mac OS X system, here's how I configured the MAMP Apache server to use name based virtual hosting for two websites that I'm developing at the same time.

Unix/Linux ‘cut’ command examples

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.

A "tar extract multiple" tip - How to extract multiple files from a tar archive

tar extract FAQ: Can you demonstrate how to extract (un-tar) multiple files from a tar archive, without extracting all files from the archive?

Sure, here are a couple of examples of how to extract multiple files from a tar archive (un-tar them), without extracting all the files in the archive.

First, if you just need to extract a couple of files from a tar archive, you can usually extract them like this, listing the filenames after the tar archive:

Linux backups: Using find, xargs, and tar to create a huge archive

I did something wrong in a previous blog entry that led me to use the pax command to create a large backup/archive. There’s nothing wrong with using pax — other than the fact that it’s not available for Cygwin — and I really needed to created a huge archive.

What wasn’t working

In my earlier blog post I stated that something like this did not work for me when trying to create a large backup using find, xargs, and tar:

find . -type f -name "*.java" | xargs tar cvf myfile.tar

What was happening was that as xargs was managing the input to the tar command, tar kept re-writing the archive. That is, each time xargs passed a new block of input files to tar, tar perceived it as a new command, and went on to re-create the file named myfile.tar. So, instead of the huge myfile.tar that I expected, I ended up with only a few files in the archive.

How to use multiple filename search patterns with Linux find

Linux find FAQ: How do I use the Linux find command to find multiple filename extensions (patterns) with one find command?

Problem

You want to use the Unix/Linux find command to search for multiple filename types (or patterns). You know you can run the find command several times, one for each filename extension you're looking for, but there must be a way to search for multiple filenames at one time.

Linux: How to find files open by a Linux process (lsof)

Linux process question: How can I determine which files are open by a process on a Unix/Linux system?

It seems like a couple of times a year I run into a situation where I have a zombie process that I need to get rid of, but before killing it off, I want to make sure I'm killing the right thing. Some times when a Linux process is in a zombie state, you can't see the information you need with the ps command, so you need something else.

Unix/Linux ‘alias’ command examples

Unix and Linux aliases are a really nice way of customizing the command line to work the way you want it to work. With alias commands, you're essentially doing a little programming, and creating new Unix and Linux commands custom-tailored to the way you work. You can create aliases to use instead of existing commands, and you can also create aliases as Linux command pipelines.

Complete backup scripts for my websites (Drupal, MySQL)

I’m spending a little time today trying to automate the process of backing up my websites, and in doing so I thought I would share the Linux shell scripts that I use to generate the backup files, including backups of my MySQL databases and Drupal website directories. If you are comfortable with shell programming in Linux, I think you’ll be able to follow the code in the following scripts.

MySQL database backup script

First, this is a backup script I use to backup a MySQL database:

Linux pipe command examples (command mashups)

One of my favorite things about Unix, Linux, and Mac OS X systems is that you can create your own commands by merging other commands. There isn't any formal name for these command combinations, other than to say that you're "piping" commands together, so I recently started referring to these as "command mashups".

Here's a simple pipeline command I use all the time, creating a long list of files and piping the output into the Linux more command:

Using find and grep to print lines before and after what you’re searching for

A cool thing about the Unix/Linux grep command is that you can show lines before and after a pattern match with the -B and -A options. As an example, I just used this combination of find and grep to search for all Scala files under the current directory that contain the string null. This command prints five lines before and after each null line in each file:

$ find . -type f -name "*.scala" -exec grep -B5 -A5 null {} \;

That’s good stuff, but it prints a really long list of lines, and I can’t tell the output of one file from another. To fix this, I put the following code in a file named helper.sh, and made it executable:

Linux `mkdir`: How to create multiple directory levels at one time

Linux mkdir question: I'm about to go to work on a new server, and I want to create subdirectories named docs, bin, and lib, and the docs directory has two subdirectories named personal and business. How can I create these directories and subdirectories with one command?

Answer: Use the "-p" option of the Unix/Linux mkdir command. The answer is shown below:

A collection of 75+ free Linux tutorials

Free Unix and Linux tutorials: Wow, this blog post makes me feel a little old. As I've been working on reorganizing the website lately, I found that I've written more than seventy-five Unix and Linux tutorials. To try to make them easier to find, I created this page to link most of them up.

So, to that end, here is a list of at least 75 free Unix and Linux tutorials I've written. I hope you enjoy them, and I hope they're helpful.

The Unix/Linux `htop` command

One new thing I learned in the last week is the `htop` command. It’s a nicely-improved version of the standard top command. This image provides some hints as to its features. If you have a Mac, you can install it with Homebrew.

vi and vim `set` command examples

If you work with the vi editor a lot, you'll find yourself tinkering with various vi configuration settings from time to time. Many times you'll want to modify the configuration of your current vi session, and to do that you'll use one of many available vi set commands. In this vi (and vim) tutorial, I'll share the vi set commands I use most often.

How to build a dynamic Java classpath in a shell script

Here's a Bourne Shell (sh) script I use to run a Java anti-spam program I wrote. The program I'm running isn't important, but what is worth sharing about this shell script is how I dynamically build the Java CLASSPATH by including all of the jar files in the lib directory.

Other parts of the shell script (showing a shell script for loop) may be worth sharing as well, but I think that building the Java classpath dynamically in the shell script is probably the most important part.

With that being said, here's the shell script:

How to use the Linux cut command with a 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: