External Command Examples

awk

# Print the name and size of all files in a directory
ls -l | awk '{print $9, $5}'


#Print the files that contain unix in their name
ls -l | awk '/unix/ {print $9, $5}'


#Print the name and owner of files with a size greater than 100,000 bytes
ls -l | awk '{if ($5 > 100000) print $9, $3}'


#Print lines 35 through 50 of a file named listofstuff
awk '/35/,/50/' listofstuff


#Print the lines in a file named file1 that begin with DEBUG
awk '/^DEBUG/

cat

#Catenate 2 files together
cat file1 file2 > file3

cmp

# Show where two files are different

cmp -l test test1


# The exit status is 0 is files are identical, 1 if different and > 1 
# for an error.  Use this to decide what to do.

#! /bin/bash
 
if cmp -s junk junk.s; then
   echo "the same"
else
   echo "different"
fi

cut


echo "abc/def/ghi" | cut -d/ -f2 # prints def 


# put columns 5-10 of a file into another file

cut -b 5-10 < file > columns5-10

date

date

echo

echo "The value of count is " $COUNT


echo -n "The sum is "
echo $[$X + $Y]

expr

# If COUNT is not zero, print the value, otherwise print a message

expr $COUNT \| "COUNT not set"   # COUNT or if COUNT



# Return the larger of two values

function max (x, y)
{
   if expr $x > $y then
      return $x
   else
      return $y
   fi
}


# Get a substring from a string starting at 8 and 10 characters long
# and output it

expr substr $NAME 8 10



# Determine the number of characters in a string variable

expr "$NAME" : ".*"

# or

expr length $NAME


file

# Determine, if possible, the types of files

file xyz

file $3


# Find text files and compress

if test `file -b $filename | cut -f1 -d ' '` = "ASCII" 
then
   compress $filename
fi


find

# find a file with a specific name

find /usr/local -name run.jar


# find files over 100 days since last access

find $DIR -atime +100


# find files owned by a particular user

find / -user $USER


# find all files with size greater than 1 megabyte and delete them

find ~ -size +1024b -exec rm \{\} \;


fmt

# Format file1 to have 20 volumns with standard spacing
# and output to file2

fmt -20 -u < file1 > file2

grep

# Find all occurrences of the string "mp3" in files in /usr/users/$name

grep mp3 /usr/users/$name/*


# Get the filenames of files containing the string "mp3"  or MP3 in files in 
# /usr/users/$name

grep -l -i mp3 /usr/users/$name


# Get the filenames of all files that DON'T contain the string
# th?r*.bin (any case) in the current directory and all directories below.

grep -l -i -r "th?r*.bin  *


#

head

# Print the first 10 lines of the specified file and look for the 
# script command

head $1 | grep bash

# As before, but print only the first 20 bytes

head -c 20 $1 | grep "[sh bash csh]"

join


# Join two files that match join fields line for line.

join file1 file2

# Join and insert empty fields for missing data

join -e EMPTY file1 file2

# Join with the join field being field 2 in file1 and
# field 4 in file2

join -1 2 -2 4 file1 file2

paste


# Create a new file consisting of lines from file1 and file2

paste file1 file2 > file3

# Paste three files together side-by-side using semicolons to 
# separate the lines

paste -d; file1 file2 file3 > file4


ps


# List at all processes 

ps ax


# List at all processes from user jimbob

ps -u jimbob

# List all processes, listing % cpu time, % memory usage, user, command and pid

ps ax -o %cpu, %mem, user, cmd, pid


sed



test

# The syntax is the same as the internal shell command
#
# Find out if a file exists and print an appropriate message

if test -f menu.html; then echo "yes"; else echo "no"; fi



tr

# Convert [ in a file to {

tr '[' '{' < file1 > file2

# Exchange a and z and p and g in a file

tr "apzg" "zgap" < file1 > file2

# Remove extraneous carriage returns from a file 

tr -d '\r' < file1 ? file2

uniq

# Remove duplicate lines

uniq < file1 > file2

# Do so ignoring case 

uniq -i < file1 > file2

# Print the duplicated lines in a file

uniq -d < file1

which

# Get the full path of the netscape

NETSCAPEPATH=`which netscape`


xargs

# Delete all jobs belonging the the user cpuhog

ps ax | grep cpuhog | awk '{print $1}" | xargs kill -KILL