Linux, Unix, /etc

Danger Will Robinson! You are now entering a condescending Unix user zone!
Sponsored links (requires javascript):

Scripts: File Utilities

random-sound.sh

A little toy to pick a sound file at random and play it. The sort of thing you run from cron every often, to give others in the room, and sometimes yourself, a bit of a surprise!

 
#!/bin/sh
#random-sound.sh: play a random file from the sounds directory
cd $HOME/sounds
filename=$(find -type f |
awk 'BEGIN{
		srand()
	}
	{
		names[NR]=$0
	}
	END{
		i=1+int(rand()*NR)
		print names[i]
	}
')
echo "$filename"
wavplay "$filename"


showatq

Display the content of jobs in the atq. Warning: requires mod. of perms. on /var/spool/atjobs/ to non-safe values for a shared machine.

 
#!/bin/sh
for i in /var/spool/atjobs/*
do
    file=`basename $i`
    case $file in
	'*') exit 1 ;;
    esac
    echo $file
    sed -n '/cd \/usr\/home\/paul/,/^$/p' < $i | sed '/cd \/usr\/home\/paul/d'
done


overwrite

Overwrite one file with another. From "The Unix Programming Environment ".

 
#!/bin/sh
#overwrite: overwrite file with output of command
#note: must use redirection of input from file in command if required

opath=$PATH
PATH=/bin:/usr/bin:/usr/local/bin

case $# in
    0|1) echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
esac

file=$1; shift
new=/tmp/overwr1.$$; old=/tmp/overwr2.$$
trap 'rm -f $new $old; exit 1' 1 2 15

export PATH=$opath; 
if "$@" > $new
then
    cp $file $old
    trap '' 1 2 15
    cp $new $file
else
    echo "overwrite: $1 failed, $file unchanged" 1>&2
    rm -f $new $old
    exit 1
fi
rm -f $new $old


dotfiles

List your dotfiles, and just your dotfiles.

 
#!/bin/sh
#dotfiles: list all files beginning with .

ls -a $1 | grep ^[.].*


fixjpg

convert "Progressive" jpg files, as often found on websites, to normal jpg, that can be displayed with xv. Why xv can't display these files to start with, I couldn't tell you.

 
#!/bin/sh
#fixjpg: convert "Progressive" jpg files to normal jpg
for i in "$@"
do
    if djpeg < "$i" | cjpeg > "$i.new"
    then
	mv "$i.new" "$i"
    else
	echo "fixjpg: failed on $i"
	rm "$i.new"
    fi
done


select-files

Pick out files last modified on "MMM DD" eg. "Sep 16".)

 
ls -ltr|
grep 'Sep 16'|
sed 's/  */	/g'|
cut -f 9-|
sed 's/	/ /g'|
sed -e 's/^/"/' -e 's/$/"/'


editlots

grep for a string and edit the files containing it. I think this is from Unix Power Tools.

 
#!/bin/sh
# editlots: grep for a string and edit the files containing it
exec vi `grep $1 *|cut -f 1 -d :|sort -u`


prman

Print man page on 65-line device (default is 66))

 
#!/bin/sh
#prman: print man page on 65-line device (default is 66)
for i in $*
Do
    file=`find /usr/man /usr/local/man -name $i.[0-9]`
    sed '1i\
    .pl 65
    ' < $file | groff -man
done


a simple one-liner

Visually examine all the shell scripts in a directory.

less $(file *|grep script|sed 's/^\([^:]*\):.*/\1/')


rcs.review

Sometimes, I want to "put away" a piece of writing, not to have it hanging around in my working directory any more. On the other hand, I don't want to get rid of it entirely with rm. Well, since I use the RCS revision control system, this is simply done: just a ci <filename>, and I'm right. But then, on occasion I want to look through stuff that's been "retired" in this way. I can't do a co unless I know the filename, which is usually not the case. What I really want is to read through all the old stuff, on the off-chance I find something worth using, This script does the trick. Note the -p option to co, sending the file to STDOUT rather than creating a working file.

#!/bin/sh
#rcs.review: have a look at files that have been "retired"
if [ ! -d RCS ]
then
    echo "No revision control here! Bailing out"; exit 1 
fi
cd RCS
for i in *
do
    name=$(echo $i | sed 's/,v$//')
    if [ ! -f ../"$name" ]
    then
	(cd ..; (echo $name; co -p RCS/"$name",v) | less) 
    fi
done 


[back to Scripts index] [back to Linux, Unix, etc] [Main Site] [Weblog]



Contents licensed under the GPL