Archive for the ‘Tips and Tricks’ Category

14
Nov

Find/Highlight Lines Longer Than 80 or ‘n’ Characters in VIM

Here’s a quickie that will help you find lines longer than 80 characters (or any number of characters) within VIM. Open vim, and copy/paste the following:

:/.{80,}

You can use the regex anywhere, actually, but this is a good one to store in the useful command list. Or, create an alias for it in your .vimrc file for quick access.

2
Sep

Using Arrays and For Loops

Here’s how to setup an array and use it in a for loop. Open up the terminal and let’s get started…

Step 1: Create an array

This is easily done by surround space separated items in parenthesis. For example:

files=( dir1/file1.php dir2/file2.php dir1/subdir/file3.php )

Note: Be aware of any spaces within the array items. If you have this, you’ll need to surround it in quotes. See appendix at the end of this post.

Step 2: Figure out what you want to do to each of the items in the array

The next part is figuring out what you want to do to each of the items in the array. Keep in mind, these items can be variables, filenames, text, numbers, whatever. For example, I want to see how large each of these files are along with other information. For this, I would run ls -lh filename.php

Unix~ ls -lh dir1/file1.php
-rw-r--r-- 1 user group 2.7K May  4 16:47 dir1/file1.php

Step 3: Iterate through the list of array items

Finally, let’s perform the action from step 2 on all of the items in our array. For this, the key is to use ${array[@]}, which says run through all (@ll) of the items in the array called “array”. Going along with the contrived, simple example set in the previous steps, the final command is:

for file in ${files[@]}; do ls -lh $file; done

The output from the above command is:

Unix~ for file in ${files[@]}; do ls -lh $file; done
-rw-r--r-- 1 user group 2.7K May  4 16:47 dir1/file1.php
-rw-rw-r-- 1 user group 2.8K Aug 25 15:23 dir2/file2.php
-rw-r--r-- 1 user group 2.4K Mar  7 19:57 dir1/subdir/file3.php

APPENDIX: Working with spaces in array items

If you have spaces within your array items, be sure to surround them with quotes like such:

test=( one "two 2" "three 3 tree" )

On top of that, you’ll need to surround ${test[@]} in quotes like such:

for i in "${test[@]}"; do echo $i; done

Here’s what you’ll get:

Unix~ for i in "${test[@]}"; do echo "==> $i"; done
==> one
==> two 2
==> three 3 tree

If you don’t use quotes, you’ll get this instead (BAD!):

for i in ${test[@]}; do echo "==> $i"; done
==> one
==> two
==> 2
==> three
==> 3
==> tree
26
Aug

Remove a File / Directory from a Tarball Without Extracting First

If you have an extra folder or file within a tarball (.tar) file, you can remove it without extracting the entire tarball first. This can be really handy when you have a massive file that you don’t want to spend a lot of time extracting and re-archiving. Open up a terminal shell and follow along with the example below.

We’ll first see what’s in the compressed tarball named sandbox.tar.gz by using the --list option of tar:

thelinuxdaily$ tar --list --file=sandbox.tar.gz
sandbox/
sandbox/delete_me/
sandbox/delete_me/hello.txt
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt

Let’s try to delete the folder called sandbox/delete_me from the compressed tarball:

thelinuxdaily$ tar --delete --file=sandbox.tar.gz sandbox/delete_me
tar: Cannot update compressed archives
tar: Error is not recoverable: exiting now

See what happened? That means we need to uncompress it. In this case, it’s a .gz compression type, so we’ll use gunzip (if you were using bz2, you’d use bunzip2:

thelinuxdaily$ gunzip sandbox.tar.gz

Let’s try to delete the folder sandbox/delete_me again:

thelinuxdaily$ tar --delete --file=sandbox.tar sandbox/delete_me

We didn’t get an error message, so that’s good. Let’s see if it’s gone by using --list again:

thelinuxdaily$ tar --list --file=sandbox.tar
sandbox/
sandbox/hello.txt
sandbox/hello2.txt
sandbox/hello3.txt
sandbox/save_me/
sandbox/save_me/hello.txt
thelinuxdaily$ gzip sandbox.tar

Excellent! That’s what we needed to see. If you have any other tips, feel free to use the comments below.

17
May

Give Normal User Write Access to /var/www

Here’s a quick way to allow a normal user write access to the /var/www directory so that they can work with web server files without the need to login as root first. Open the terminal and issue the commands:

sudo chown -R `id --user` /var/www/
sudo chgrp -R `id --user` /var/www/

If you don’t have sudo enabled on your distro, remove ‘sudo’ from the commands and login as root first before executing them with ‘su’.

You should now be able to write to all the files within /var/www/. My only other suggestion is that you can be a bit more selective about what directories and files are allowed write access by removing the recursive option “-R” from the command.

6
Apr

Article: Playing Audio with Linux 2.4 Kernel

The 2.4 kernel seems dead to most, but it has it’s place in embedded systems. I dealt with a customer a while ago using an embedded system running the 2.4 kernel and he needed to play audio. Here’s an article that got me through the process: http://tldp.org/HOWTO/Sound-HOWTO/x504.html

30
Mar

A Quick Usage Example of the Split Command

Have a file that needs to be split into pieces? Here’s a quick example of splitting the text file “test.txt” containing an arbitrary number of lines into several files each with 10 lines a piece and named “split_aa, split_ab, split_ac, …”.

cat test.txt | split -l 10 - split_

If you’d like to use numeric suffixes instead of alphabetic, use the ‘-d’ option in split. There’s more tips and tricks where that came from in the split man page, so be sure to check it out.

9
Feb

Validate Email Using POSIX Regular Expressions

You can validate an email address using POSIX Regular Expressions like the following examples:

^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$

This is two different ways of doing the same thing. If you want tighter control on your validity, I would recommend specifying com, edu, gov or other domain names you consider valid like shown in the second example above.

A quick PHP script to demonstrate:

<?php

// hit this in a browser and specify the email in the URL:
// http://www.mywebsite.com/email_validate.php?email=foo@bar.com

$email=$_GET['email'];

//if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', $email)) {
if (!eregi('^[a-zA-Z0-9_-.]+@[a-zA-Z0-9-]+.[com|edu|gov]+$', $email)) {
        echo "<p>That is not a valid email address.</p>".
             "<p>Please return to the previous page and try again.</p>";
        exit;
}
else
        echo "<p>That is one sexy email address!</p>";
?>
2
Feb

Cute ‘sed’ Tricks to Modify Specific Lines Within File

Here’s a cute trick to change or insert a line in a file given a line number using sed. You can pair it with grep to make it even more powerful with pattern matching.

#!/bin/sh

if [ $# -lt 1 ]; then
   echo "Usage: $0 <filename>"
   exit
fi

# Variables
#file=testfile.txt
file=$1

# First, change line number $linenum, then insert
# two spaces at the beginning of that same line
linenum=1
sed -i "${linenum}c This line was changed" $file
sed -i "${linenum}s/^/   /" $file

# Insert a line at $linenum, then insert text at
# the end of that same line.
linenum=2
sed -i "${linenum}i This line was inserted" $file
sed -i "${linenum}s/$/ end of line/" $file

# Match a particular pattern in the file and insert a line
# directly above the first match (will need massaging for
# multiple lines, perhaps using an array)
linenum=`grep -n pattern $file | head -n1 | cut -d: -f1`
linenum_above=`echo "$linenum - 1" | bc`
sed -i "${linenum_above}i This line was inserted" $file

echo "Resulting File:"
cat $file

As usual, take a look at the man pages for sed for more information.

31
Jan

Delete Lines Within File Containing Matching Pattern/Text

Here’s a quick one-liner command that will remove any lines matching a text patter from a file:

sed -i '/pattern/d' filename

For example, say I want to remove all lines in the file garbage.txt that contain the text “foobar”. Here is my console output.

user@localhost $ cat garbage.txt
This is a plain text file that
contains lots of text
but most of it is foobar
and it's unnecessary
to include foobar in text.

user@localhost$ sed -i '/foobar/d' garbage.txt
user@localhost$ cat garbage.txt
This is a plain text file that
contains lots of text
and it's unnecessary

user@localhost$
29
Dec

Creating a “Fluff” File Filled with Specified or Random Text

Described below are two different methods for quickly generating a certain size of fluff, or filler, to be inserted into a text file for testing purposes. The first method is to use a single line command which will very quickly fill a file with random text (must have base64 installed). The second method is to use a script that will quickly fill a file with text that the user specifies (there are some limitations).
[Read more →]

Next Page »