Archive for the ‘Tips and Tricks’ Category

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=[email protected]

$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 →]

27
Dec

Change Terminal Behavior to be Similar to VI

Use the following command to setup your shell to have similar key behavior to vi.  If you’re already familiar with the keybindings of vi, then you may appreciate the ability to quickly search/edit your history, or navigate through your commands quickly.  One example I heard about was being able to change a single character in a massive 200 character command.

set -o vi

You could include this command within your ~/.bashrc or ~/.bash_profile files as well so you have it when you login to a shell.

22
Dec

Determine Which Terminal Shell You’re Running

It’s very simply to determine which shell you’re using while logged into a Linux terminal.  Simply run the following command:

ps -p $$ | tail -n1 | cut -d" " -f26

You’ll get in return, the shell command you’re using (plus the full path to it).

user@localhost$ ps -p $$ | tail -n1 | cut -d" " -f26
/usr/bin/bash

Another way to do it would be to use awk instead of cut:

ps -p $$ | tail -n1 | awk '{print $NF}'
« Previous PageNext Page »