Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

My big idea: Ancient Brain

Search:

CA216      CA249      CA318

CA400      CA651      CA668


Sample Shell programs



norm - set permissions as open as possible

chmod  u+rwx,go+rx-w  $*



hide - as hidden as possible

chmod  u+rwx,go-rwx  $*



semihide - just open enough as needed for Web

directories:

chmod  u+rwx,g-rwx,o+x-rw  $*
files (web pages, images, etc):
chmod  u+rwx,g-rwx,o+r-xw  $*
CGI scripts:
chmod  u+rwx,g-rwx,o+rx-w  $*
"norm" could of course replace all 3 if you don't mind granting more access than strictly necessary.



rmifexists - silent repeated delete

e.g. Want to be able to repeatedly run:
rmifexists *.bak
and not get error message if no *.bak files found.

for i in $*
do
        if test -f $i
        then
         rm $i
        fi
done


Question

Why not:

        if test -f $*
        then
         rm $*
        fi


Recursive rm




"Trying out some Deadly Linux Commands".
Includes typing rm -rf / and other scary commands, including the hilarious: mv / /dev/null
See more videos of people typing rm -rf /




wipe - clean up editor backup files

rmifexists *%
rmifexists .*% 

rmifexists  *~
rmifexists .*~

rmifexists  *.bak
rmifexists .*.bak

rmifexists  *.BAK
rmifexists .*.BAK
  1. Easier than having to point-and-click each one. Especially if do this for multiple directories.
  2. Safer than typing "rm *bak" every day. One day you will type "rm * bak"
  3. In general, if you regularly type some command that would be dangerous if you make a typo, it would be better to debug it once and put it in a script and never type it directly again.


Exercise

Write a recursive wipe.



myspace - which of my directories use the most space

cd $HOME
du | sort -n






Command-line image processing

We need libjpeg utilities.
Should be installed on DCU Linux.

To make 1/4 size versions of 10,000 JPEGs without ever opening an image editor (or doing any work):


  
for i in *jpg
do

 djpeg  -scale 1/4 -bmp $i  > temp.bmp

 cjpeg  temp.bmp            > small.$i

done


JPEG needs to be decoded to a BMP (bitmap), then be re-sized, then re-coded back to JPEG.
Can do this in one line, leaving out the temporary file: Pipe result of djpeg into input of cjpeg.

  


Extract images from PDFs

I was once given an archive of thousands of scanned historical images. They were all inside PDFs.

I automatically extracted all the JPEGs from the PDFs as follows:



for i in *pdf					# i = x.pdf
do

  x=`basename "$i" ".pdf"`		# get root filename x (without .pdf bit)

  pdfimages  -j  $i  $x  		# extracts images to x-nnn.jpg

done


  


Command-line movie processing

You can do command-line movie processing with ffmpeg.

Your Shell script can bulk convert, split, join, rotate and resize thousands of videos. Without opening any window.



Feeds      w2mind.org      ancientbrain.com

On the Internet since 1987.