TinyURL widget - shorten your URL's for free!

Enter a long URL to make tiny:

Saturday, April 4, 2015

BASH Shell scripting: read all files in directory, read files of .suffix, remove the nth file, and output count

BASH is hard for beginners and even pros; unless you were taught all the contexts of data and variables, then it's hard to remember which circumstance makes sense.

The biggest problem with Linux is the powerful tools matched with skimpy documentation.  It resides in the brains of people that forget to pass it on.

Or, perversely, the people that want to hoard the information and make you beg them to reveal it.  I really don't like that attitude from the older SYSV crowd, they seem to be helping by answering questions but it would be better to take the time and write out complete methodical examples. And to explain step by step.

The unix community has a penchant for Munchausen syndrome. It has a need to be asked for help rather than just being helpful.

I would recommend if you are trying to learn to make simple examples with the dangerous parts left out and build up functionality from simple parts.

For these examples, I started making a shell script that could read files, then read certain files, then read and delete certain files.  I made lots of mistakes and the directory was placed in /tmp in case I toasted it.

This script would be good for someone trying to make a GIF from a series of image files. Sometimes people that want to do the functionality but don't want to learn a language this is a good post for that.

I made this really bad animation removing every third file over and over unto a nonsensical mess but it made an interesting GIF.





This shellscript reads all files of any type (not hidden) in the present working directory and echos the file name and increments a counter.

At the end, it outputs the count of files:


#!/bin/bash
x=0
FILES=./*        # BASH resolves variable for you
for  f in $FILES
do
    echo $f        # iterates through and echoes
    ((x++))         # postincrements counter for all type file
done
echo "$x"

You can find this file here

If you want read and count files of a certain .suffix then  send it as a command line argument in this case $1 first arg. NOTE: don't include the '.' !

#!/bin/bash
# this version reads files of a certain .suffix passed as arg $1
x=0
FILES=./*.$1        # BASH resolves variable for you
for  f in $FILES
do
    echo $f        # iterates through and echoes
    ((x++))         # postincrements counter for all type file
done
echo "$x"
You can find this file here

For a good tutorial of if then constructs go here:

If you want to iterate through and remove every nth file in that file list with the suffix, then execute this shellscript:

#!/bin/bash
# DRE 2015 - Thinking, Realizing, Reacting
# this version reads files of a certain .suffix passed as arg $1
x=0
y=0
n=$2
echo $n
FILES=./*.$1        # BASH resolves variable for you
for  f in $FILES
do
    echo $f        # iterates through and echoes
    ((x++))         # postincrements counter for all type file
    if ((x%n == 0))  ;  then  # test for file increment and execute
        rm -f "$f"   # you need to quote filename variable to get name in proper format to rm command
        ((y++))    
    else
        :   # this is the BASH no operation (NOOP) command
    fi
done
echo files count: "$x"
echo files removed : "$y"
You can find this file here


TO RUN THIS COMMAND, Open a SHELL and type:

sh ~/shellscripts/remove_n_file.sh png 3

( I put all my shell scripts into a common folder shellscripts. Your placement will vary command. )

inside the directory with a series of PNG image sequence files.  This will look for all .png files and removes every 3rd one. mod 3 for this example.

So if you have 2100 files, the first run through of the script would remove about 700 files.  Note that the contents would still be in sequence.  Then, if you wanted to reduce them further just do this again and again and it will remove from the same sequence over and over. You can compensate for varying the loop delay time with Image Magick. Of course if you thin out too many images the sequence will go really fast and may not work right.  This is the art of the process. 

To make a good motion animated GIF you should be looking at a sequence that's a few second in video length.

This shell script takes 2 variables, the suffix first and then the number file to delete.

Note for sake of simplicity there is no error checking here so you can destroy the contents you are working on. Use against a copy folder!  This is a tutorial not a production shell script.

The tricks are you use the (()) C argument mode to evaluate math properly, so you don't need $variable names but the actual variables.

You need to quote the $f filename to send it resolved to remove command.


NOTE:

This shellscript DOES work on ubuntu 14.10 with:

 bash --version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

If you change the downloaded file permission to executable

chmod u+x remove_n_file.sh

to allow it to run and run without sh

~/shellscripts/remove_n_file.sh png 3

and it does work on Fedora 20 with:

 bash --version
GNU bash, version 4.2.53(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


No comments:

Post a Comment