Hello all I’m going to add a few scripts, not all at once, so please check back if interested.

 

First one is sort of a followup on my last Post  . This allows you to find the filenames and total file size used on the particular, in this case /mnt/Camera directory, but I have made it sort of general.

#!/bin/sh
# Arguments are setup as follows: $0 is the script (program)  name,
# $1 is the first ARG, $2,$3,$4,$5,$6,$7;
du –all –human-readable –apparent-size $1 $2 $3 $4 $5 $6 $7;
printf “Total number of files: “;  find $1 $2 $3 $4 $5 $6 $7-type f | wc -l;

Now let us go through this line by line and perhaps section by section.

#!/bin/sh

This allows us to create a temporary shell that will run the following code.

# Arguments are setup as follows: $0 is the script (program)  name,
# $1 is the first ARG, $2,$3,$4,$5,$6,$7;
.

Remember the “#” by itself is for a comment with a few exceptions, but no exception here, just letting you know that there is this thing called ARG or arguments in out case where their are more than one. ARG is something you will pass, like what directory you will want this shell script to execute on. In this case I have allowed up to 7 ARG’s or separate directory’s or later when you do your own scripting pass things you want acted upon. Now the actual argument that is passed is “$1, $2, $3…$7″ all separated by a ” “ (SPACE) character.

du –all –human-readable –apparent-size $1 $2 $3 $4 $5 $6 $7;

OK a little explanation, do a “du –help” in your Ubuntu Terminal window and it will show many options, and we are just using a few of them. “du” is a Disk Usage program. It tells you how much space is used and where it is used, try a du -h, This will show you how much each file on the current directory uses. the “-all” tells du that we want a count of all files, not just directories. The “–human-readable” tells us that we want a human readable format IE 1K, 200M or even 1.2G all depending on file-size. the “–human-readable” could just as easily been “-h”, but for the explanation here, i used the extended version…LOL… Try “du -H” and see some minor differences. The “–apparent-size” tells the program we want the size of the program, rounded off, not the particulars. and we have already covered the arguments “$1,$2,…,etc”.

 

The second line starts off with a printf, this is a shell command it tells the program to print the following text, in our case “Total number of files: “ and not to print a carriage-return and line-feed combination. This way we can append something to the end of the line. Then there is the “;” which basically terminates a command in that line. So out printf prints out the text and leaves the cursor at the end of the line we just printed.

This continuation of the line uses the find command. Again try find –help so see how many things it can actually do. the following “-type f” actually tells find that we are looking at file types of the filenames only.  Then we are passing the directory in this case /mnt/Camera (it can be any directory you choose) and through what is called a “|” pipe. Pipe has a lot of uses, I use it often in this small example of where I have already done a:

sudo apt list > apt.list

asks the “apt” program to “list” all its packages then appends “>” to a file called “apt.list”. Then I use:

cat apt.list | grep text

where “cat” is concatenate a file in this case “apt.list” to standard output. Then the “text” is what I am looking for, but I digress and back to the program at hand.

Now using the nifty little single character”|” called pipe we can pass the entire results from the program  “find $1 $2 $3 $4 $5 $6 $7-type f” to the program “wc” or word count. As you may be able to tell from previous examples and perhaps your attempt try attaching a “–help” to the program for a little help, not everything has a help section, but most of them do. Use the internet to search, I use the phrase “Ubuntu <what I am looking for>” a lot. “wc” has several options, but we are looking for something very specific. We just requested “find” to find each filename in the “/mnt/Camera” and the “-l” in “wc” tells the word count program to report the number of lines from the results of what was passed to it and this case it was the results of the “find” command.

Several things were accomplished here, First I took three actual lines of code and made about 6 paragraphs out of it. Secondly I started a sentence with lower case and it is acceptable (to me anyway) (hint “wc” has several options) OK enough for junk. Most importantly I hope that this helps or spurs you to look further what scripts can do for you.

QED

 

 

Advertisements