Linux Tip of the Day - Moving files around

November 28, 2008 by Adrian · Leave a Comment
Filed under: Linux 

Here is another way to move files around.

 

Sometimes you find yourself wanting to move a bunch of files into a new directory that is to be created in the current working directory.

 

Rather than using a temporary directory, or grep and variable substitution; use a subshell:

 

$ ( \ls; echo dir; mkdir dir ) | xargs mv

 

The trick is in using a subshell to run a number of commands _before_ the output is piped to xargs. This idiom is also useful for plenty of other applications.

Linux Tip of the Day - changing perms recursively

November 22, 2008 by Adrian · Leave a Comment
Filed under: Linux 

To change permissions recursively for all files in a directory

 

find dirname -exec chmod xxx {} \; -print

 

where dirname is the directory you want to change permissions.

Linux Tip of the Day - protocols with netstat

November 21, 2008 by Adrian · Leave a Comment
Filed under: Linux 

Use the command:

 

% netstat -an

 

It will show you what ports are in use on the local and foreign machines as well as the protocol running over that port for that connection and IP address information. It also displays the state of the socket being used.

 

Using the above tip user can identify the port to which he wants to send data is busy or free.

Linux Tip of the Day - numbering a file

November 20, 2008 by Adrian · Leave a Comment
Filed under: Linux 

Ever want line numbers appended to a file. Here is a short script.

 

awk ‘{no=no+1; printf”%d : %s”, no, $0}’ filename > filename.out

 

Another shorter but not necessary elegant method is:

 

grep -n . filename > filename.out

 

filename is the original file filenmae.out is the file with line numbers appended at the beginning of each line

Linux Tip of the Day - be alarmed

November 19, 2008 by Adrian · Leave a Comment
Filed under: Linux 

Name the following script alarm. Make it executable and them invoke it as:

 

% alarm 10 “Time for tea”

 

After 10 seconds machine will echo Time for Tea and beep five times…..

 

—————

 

#! /usr/bin/ksh
# alarm program
# displays a message at
if [ $# -ne 2 ]
then
echo “USAGE : $0 seconds message”
exit 1
fi

 

time=$1
mesg=$2
signal_alarm()
{
sleep $time
banner $mesg
for i in 1 2 3 4 5 6 7 8 9
do
if [ $i -eq 2 -o $i -eq 4 -o $i -eq 6 -o $i -eq 8 ]
then
sleep 1
else
tput bel
fi
done
}

Linux Tip of the Day - the next uid

November 18, 2008 by Adrian · Leave a Comment
Filed under: Linux 

Here is one way to find out the next available UID on a system.

 

It takes both the password and group files and finds the next available UID and displays it.

 

(No cats were harmed in the making of this script)

 

#!/bin/bash
awk -F”:” ‘{ print $3 }’ /etc/passwd > number_list
awk -F”:” ‘{ print $3 }’ /etc/group >> number_list
A=` sort -g number_list | tail -1`
A=`expr $A + 1`
echo “New Available UID is $A”

Linux Tip of the Day - manipulate multiple files

November 17, 2008 by Adrian · Leave a Comment
Filed under: Linux 

If you have a directory with large number of files and you want to rename or copy or process it, here is a simple way to do it;

 

# for temp in *
> do
> echo cp $temp $temp.org >> t.txt
> done

 

You can change the copy command to what ever that you need to do. This will create a file tmp.txt with all the files listed in the current working directory and insert a line into the file tmp.txt as;

 

cp filename filename.org

Linux Tip of the Day - inserting lines within scripts

November 16, 2008 by Adrian · Leave a Comment
Filed under: Linux 

If you want to insert a line at the top (or anywhere for that matter) of a file within a shell script, use the ed editor.

 

EXAMPLE:

 

string=”hello”
ed << EOF
e any_file
1i
${string}

Linux Tip of the Day - connecting database through shell

November 15, 2008 by Adrian · Leave a Comment
Filed under: Linux 

isql utility of Sybase can be efficiently used through shell scripts as follows:

—————————————————
#!/bin/sh
cat /dev/null>/tmp/qry.sql
cat << SQL >> /tmp/qry.sql
select name from sysobjects where type = ‘T’
SQL

isql -U -P -S -D -i
/tmp/qry.sql
#remove the query file
rm /tmp/qry.sql
#end of script
——————————————————

Obviously you can use update/delete queries of any complicacy in the query.

Linux Tip of the Day - comment out multiple lines

November 14, 2008 by Adrian · 1 Comment
Filed under: Linux 

Ever wanted to comment out multiple lines of code while writing shell scripts in vi, but didn’t want to pound your keyboard for half an hour doing it?

 

(I#Escape j.j.j.j. = carpal tunnel)

 

Here’s the nerdy way to do it:

 

:.,+N-1 s/^/#/g

 

Where N-1 is the number of lines minus one that you want to comment out, and s/^/#/g is the regular expression (the pattern between the first two slashes is what you want to replace, in this case the beginning of the line, and the pattern between the last two slashes is what you want to replace it with).

« Previous PageNext Page »