Let's say every day we run a cleanup script that removes old editor .bak files:
rmifexists *.bak
If there are none today, or in this directory, this will give error messages.
Can we make a silent rm that does not complain about non-existing files?
"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 /
A Steam for Linux
shell script
included a highly dangerous rm -rf line in 2015.
This led, depending how it was run,
to entire user filesystems being destroyed.
Easier than having to point-and-click each one.
Especially if do this for multiple directories.
Safer than typing "rm *bak" every day.
One day you will type "rm * bak"
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.
Exercise: shake
"shake" - detect if a word exists in Shakespeare
Usage like:
shake ireland
Script does a "cd" to the Shakespeare dir
grep (ignoring case) the argument in all the Shakespeare files
We need libjpeg utilities.
Last time I checked, at DCU this was:
Installed on PCs in labs.
Installed on student server.
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.
I was once given an archive of about 60,000 historical images.
They were all inside about 3,000 PDFs.
I automatically extracted all the images from the PDFs using
pdfimages
Last time I checked, at DCU pdfimages is:
Installed on PCs in labs.
Installed on student server.
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
# extract images and save as default PPM:
pdfimages file.pdf prefix
# extract images and save as JPEGs:
pdfimages -j file.pdf prefix
# sometimes JPEGs do not extract well, save all images as PNG:
pdfimages -png file.pdf prefix
# just list all images without extract:
pdfimages -list file.pdf
pdfimages -help
More on the archive and why Shell scripts were needed
First shell script:
The 3,000 PDFs had super complex, verbose naming.
Every single one of the 3,000 was in a collection called "P106 Papers of Sighle Humphreys".
For some reason, this got added to every single filename instead of just the folder name.
i.e. Files were called things like:
P106-138- Papers of Sighle Humphreys-.pdf
My first shell script went through all 3,000 renaming them to the simplest possible names:
138.pdf
Doing the renaming by hand would have taken endless hours of exhausting, error-prone work.
Second shell script:
Next, all 3,000 PDFs were in one directory.
If we extracted the images, we would have
60,000 images in one directory.
Impossible to browse or look at.
So my second shell script used a defined system to split the 3,000 files into 40 different sub-directories.
Splitting them by hand would have been long, exhausting and error-prone.
Third shell script:
Finally my third shell script is like the above. It extracts the images from each PDF in its directory.
Doing that through loading up each PDF into a GUI and clicking some menu item to extract images
would have taken endless hours of exhausting, error-prone work.
We end up with 40 directories, each with about 70 PDFs and 1,500 images.
One could divide it up differently, but the point is you absolutely never want to do such a large job by hand.
Without shell scripts, I have no idea how a "civilian" would manage this kind of unwieldy archive.