Posts in the “linux-unix” category

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.

Unix/Linux shell script reference page

Linux shell script test syntax

All of the shell script tests that follow should be performed between the bracket characters [ and ], like this:

if [ true ]
then
  # do something here
fi

Very important: Make sure you leave spaces around the bracket characters.

I'll show more detailed tests as we go along.

Linux shell file-related tests

To perform tests on files use the following comparison operators:

[toc hidden:1]

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.

Linux: Recursive file searching with `grep -r` (like grep + find)

Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Solution: find + grep

For years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a 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.

How to undo and redo changes in vi/vim

vi/vim editor FAQ: How do I undo and redo changes in the vi and vim editor?

Here are a couple of quick examples of how to undo and redo (“re-do”) recent changes in your vi/vim editor sessions.

vim undo (how to undo a change in vi/vim)

You “undo” changes in vi and vim with the undo command, which is the u key when you are in vim command mode. For instance, if you start with this text in your editor:

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.

How to use the Linux 'lsof' command to list open files

Linux “open files” FAQ: Can you share some examples of how to show “open files” on a Linux system (i.e., how to use the lsof command)?

The Linux lsof command lists information about files that are open by processes running on the system. (The lsof command itself stands for “list of open files.”) In this brief article I’ll just share some lsof command examples. If you have any questions, just let me know.

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.

Unix/Linux: Find all files that contain multiple strings/patterns

When using Unix or Linux, if you ever need to find all files that contain multiple strings/patterns, — such as finding all Scala files that contain 'try', 'catch', and 'finally' — this find/awk command seems to do the trick:

find . -type f -name *scala -exec awk 'BEGIN {RS=""; FS="\n"} /try/ && /catch/ && /finally/ {print FILENAME}' {} \;

As shown in the image below, all of the matching filenames are printed out. As Monk says, you’ll thank me later. :)

(I should mention that I got part of the solution from this gnu.org page.)

Update: My File Find utility

For a potentially better solution, see my File Find utility, which lets you search for multiple regex patterns in files.

Use zgrep to grep a gzip (gz) file

Linux zgrep FAQ: How do I use the Linux zgrep command? (Or, How do I grep a GZ file?)

Linux users quickly learn how to use the Linux grep command on plain text files, but it takes a little longer to really you can grep gzip (gz) files as well. Here's how.

Ubuntu ‘apt-get’ list of commands (list, update, upgrade, cheatsheet)

I have a couple of Ubuntu Linux systems, including Raspberry Pi systems, test servers, and production servers. It seems like every time I have to use an apt-get or other apt command, I always have to search for the command I need. To put an end to that, I’m creating this “apt-get reference page.” It’s very terse, as I’ve just written it for myself, but I hope it’s also helpful for others.

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: