UNIX ksh Copy Files Script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers UNIX ksh Copy Files Script
# 1  
Old 01-06-2014
UNIX ksh Copy Files Script

I need a UNIX ksh script that counts the number of files in directory, if the files exceed 20 files, then email results. I want the script to run every hour.. I don't have access to cron.. I'm some what new to UNIX. Windows guy all my career.. this is what I have so far..
Code:
#!/bin/ksh
# count.sh

while :
do
 for dir in /directory1/
 do
  echo "$dir `du $dir |wc -l`" 
 done > ./message
 mailx -s 'Dir Count' me@johndoe.com < ./message
 sleep 3600
done


Any help is greatly appreciated...

Last edited by Franklin52; 01-06-2014 at 09:40 AM.. Reason: Please use code tags
# 2  
Old 01-06-2014
How new are you to UNIX?
Because giving you the code will maybe sort you out, but if you get in trouble with the script then you are doomed, so I believe if you came here, its to learn...
Looking at given code puzzles me because I have doubts, have you tried it? what is it doing?
Do you really need a loop?
What do you call file ( a directory in your directory is counted? a link? )
# 3  
Old 01-06-2014
When I run the code given, it gives me the count of the files in the directory and sends me the email of the file count, but what I need the script to do is.. only email me the count, if its over 20 files in the directory.. No hidden files... Finally, I want the script to run every hour, if the directory exceeds 20 files, then email.. We cant use cron here..
# 4  
Old 01-06-2014
If you had subdirectories it would list them and their content also, the same for hidden stuff...
Compare in your directory
Code:
du . |wc -l     and   ls . |wc -l

and
Code:
du . |more      and    ls  .|more

then try the same in another directory where you have subdirs...


The code you gave is a loop that reads whatever it finds in directory1 and displays its disk usage... and counting how many lines are sent to output

If you want to count file in one directory, then you dont need a loop
Code:
ls -1 <directory> |wc -l

Should do the job: You have the count

Last edited by vbe; 01-06-2014 at 11:58 AM.. Reason: completed comparison...
# 5  
Old 01-06-2014
I forgot to say: what your script sends you is the content of last occurence of the loop since it overwrites itself at each new execution...
# 6  
Old 01-06-2014
Relational operators:
-lt
-gt
-eq
etc...
are used like this:
Code:
if [ $A -gt $B ]
then
   echo "A: $A is greater than B: $B "
else
   ...
fi

In your case
Code:
B=20
A=$(ls -1 <directory> | wc -l )

You now should be able to get the work done...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use find with cp and sed in ksh to copy files to a slightly different location

Hello there wonderful people, I am running on Solaris 10 and with the following ksh version: strings /bin/ksh | grep Version | tail -2 @(#)Version M-11/16/88i Suppose I want to copy files that end in _v2 from underneath /dir1/dir2/save directory to /dir1/dir2. Basically, what I’m... (12 Replies)
Discussion started by: ejianu
12 Replies

2. Shell Programming and Scripting

Creating a ksh script to copy a folder

Hi Folks, I need help in creating a ksh script to copy a folder from one server to multiple other servers using ssh and sftp. The thing is the folder name changes daily. So is there a way we could specify the folder name during the execution of script.? I would appreciate a quicker help.... (3 Replies)
Discussion started by: lena07
3 Replies

3. Shell Programming and Scripting

UNIX command to copy files from Windows to UNIX box

Hi Folks, I have a file name abc.xml in my windows machine at the location c:\ytr\abc.xml which I want to place at the unix box machine inside cde directory.. at the following location that is /opt/app/cde/ now the credentials of unix box are abc345 -->(dummyid) ftyiu88--->(dummy passwd) ... (4 Replies)
Discussion started by: punpun66
4 Replies

4. Shell Programming and Scripting

Need Shell Script to copy files from hp UNIX to windows server 2008

Need Shell Script to copy files from hp unix to windows server 2008 I tried to google and found some options but nothing worked I want a script to copy a file in unix to windows server so I can schedule the script on daily basis.Please help me its more needful for me. Let me know if any... (4 Replies)
Discussion started by: Lucky2Bv
4 Replies

5. UNIX for Dummies Questions & Answers

Help with ksh script to list files, cp it to another UNIX server

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (7 Replies)
Discussion started by: chococrunch6
7 Replies

6. Shell Programming and Scripting

Unix shell script to Copy files from one Windows server to another Windows server.

Can anybody please help me on how to code for the below requirement: I need to write a shell script (on different unix server) to copy files from multiple folders (ex. BRN-000001) from one windows server (\\boldls-mwe-dev4)to a different windows server(\\rrwin-ewhd04.ecomad.int). This shell... (4 Replies)
Discussion started by: SravsJaya
4 Replies

7. Shell Programming and Scripting

KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post. Software we run creates files... (2 Replies)
Discussion started by: hurtzdonut
2 Replies

8. Shell Programming and Scripting

ksh to copy multiple files

Guys, I've got a list of about 200 files I need to copy from /tmp to /data. I can't use wildcards because the filenames are all very different. What I want to do is cut and paste them into a file and read that as the input to a copy command (line by line). I tried using find and -exec... (4 Replies)
Discussion started by: Grueben
4 Replies

9. Shell Programming and Scripting

Solaris KSH shell script to copy all lines from one file to another

Hello, more of a windows wscript guy. However I took a new position that requires me to support some solaris servers. So... issue is that I need to copy all lines from a file to a temporary file and then copy them back into the original file starting at line 1. Reason I need to do this is... (5 Replies)
Discussion started by: ZigZaggin
5 Replies

10. Shell Programming and Scripting

Sample ksh script for copy the data from excel to database table ?

Hi All, I need to convert the data from excel to database table in sybase. Please provide some sample script.. thanks, Royal. (1 Reply)
Discussion started by: royal9482
1 Replies
Login or Register to Ask a Question