Need UNIX script to check checksum and dummy file creation.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need UNIX script to check checksum and dummy file creation.
# 1  
Old 02-19-2019
Need UNIX script to check checksum and dummy file creation.

Hi Folks,
I need a UNIX script which will copy files(Table wise) from source directory to destination directory (Under table directory) and also creates 2 additional files after getting copied to destination directory with extension .pdy and .ldy , . pdy file will be zero byte file should get created with table name (i.e. Table : xyz should get xyz.pdy file ) and .ldy file should get table_name, checksum of that file, and checksum method used (where comma(,) is field separator.
any help is greatly appreciated.
# 2  
Old 02-19-2019
Welcome to the forum.


We're glad to help you if you're stuck, but please show your own efforts first, accompanied with OS, shell, and tools' versions, careful, detailed decription of the problem, meaningful sample data, and error messages if they exist (or description of malbehaviour otherwise).
If what you posted is homework / classwork, please post in the adequate forum filling in the entire form.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-19-2019
Hi RudiC,

i'm executing this command in linux x86-64 bit server on korn shell.
i tried using below command but it doesn't help me completely.

Code:
for file in *.txt
do
  count=$(wc -l < $file)
  echo $file `date +%Y/%m/%d`, $count, cksum $file, checksum method use 
done

(comma(,) is field separator)

source path:
Code:
/data/files_to_transfer/xyz.txt

destination path with required output (files):

Code:
/data/final_files/
xyz.txt
xyz.pdy
xyz.ldy

content of xyz.ldy file should be as below:
Code:
row_count (xyz.txt),timestamp of coping the file,checksum value of file, checksum method used

where comma (,) is field seperator




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-20-2019 at 10:55 AM.. Reason: Added CODE tags.
# 4  
Old 02-20-2019
I'm having a hard time to reconcile your specification in post #1 with what you show in post #3 - no cp command, no empty .pdy file creation, no cksum method determination, and the fields echoed don't match the ones given in spec nor within post #3

How would you get along with this as a starting point:
Code:
SRC="/data/files_to_transfer"
TGT="/data/final_files"


(
 cd "$SRC" 

for FN in *.txt
   do    read CKS REST <<< $(cksum $FN)
        > "$TGT/${FN%.*}.pdy"
        echo "$FN", $(date +%Y/%m/%d), $(wc -l < $FN), $CKS, "checksum method used" > "$TGT/${FN%.*}.ldy"
        cp "$FN" "$TGT/$FN"
  done
)

This is executed in a subshell so the cd command won't change your parent shell's CWD. And, yes, you're reading correctly - the redirection without a command will just create an output file but won't put anything in.

Last edited by RudiC; 02-25-2019 at 08:29 AM.. Reason: Found the opening parenthesis an cd $SRC missing
# 5  
Old 02-25-2019
Do you have sha1sum available? That is very good at generating/checking checksums. Expanding on the suggestion from RudiC:-
Code:
SRC="/data/files_to_transfer"
TGT="/data/final_files"
OWN=myappuser                                                                                      # Define file owner
GRP=myappgroup                                                                                     # Define file group
PRM=440                                                                                            # Define permissions
SUMS="${TGT}/sha1sums"

exec 50> "${SUMS}.new"                                                                             # Define the output file for checksums

cd "${SRC}"                                                                                        # Change to the source directory
for FN in *.txt
  do
     CKS=$(sha1sum "${FN}")                                                                        # Generate a checksum
     echo "${CKS}"  >&55                                                                           # Write the sha1sum record to intermediate file descriptor 55
     > "${TGT}/${FN%.*}.pdy"                                                                       # Create en empty file
     echo "${FN}", $(date +%Y/%m/%d), $(wc -l < ${FN}), ${CKS}, "sha1sum" > "${TGT}/${FN%.*}.ldy"  # Still creating this as before if you need it
     cp "${FN}" "${TGT}/${FN}"
     chown "${OWN}:${GRP}" "${TGT}/${FN}"                                                          # Set desired ownership
     chmod "${PRM}" "${TGT}/${FN}"                                                                 # Set desired permissions
  done  55>&50                                                                                     # Catch all the sha1sum output into the new file in one IO operation

grep -vf - "${SUMS}" < <(cut -f3 -d" "  "${SUMS}.new") > "${SUMS}.tmp"                             # Ignore previous checksums of the same filename
sort "${SUMS}.tmp" "${SUMS}.new" > "${TGT}/sha1sums"                                               # Put the older & new file checksums together neatly

This will also give you the file /data/final_files/sha1sums that your can use as input to sha1sum very simply like this:-
Code:
sha1sum -c /data/final_files/sha1sums

This command will read the checksum file and then try to confirm the files listed. Note that it will be a relative path in the checksum file, so you will have to be in the correct directory.

Would just collecting the sha1sum output be easier than trying to write your own and build your own checking process? (presumably some similar counting etc., but in reverse)



I hope that this helps,
Robin

Last edited by rbatte1; 02-25-2019 at 08:31 AM.. Reason: Corrections to various bits, including using field 3 in the cut!
These 3 Users Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. UNIX for Dummies Questions & Answers

File creation time in UNIX

Hi All, Can any one help how to long list today's files in unix.i.e files which are have been created today should be able to "ls -ltr" .I should be able to apply the "ls -ltr" command on today's files(should not include all the files which were there in the directory). Thanks in advance!!! (3 Replies)
Discussion started by: Balasankar
3 Replies

3. Shell Programming and Scripting

Needed script to FTP a File and generate a quality checksum file

hi all i want a script to FTP a file and should generate a quality checksum file means when I FTP a file from one server to another server it should generate a QC file which should contain timestamp,no.of records in that file Thanks in advance saikumar (3 Replies)
Discussion started by: hemanthsaikumar
3 Replies

4. IP Networking

Wireshark UDP checksum bad checksum

Hello I am communicating with two devices using my computer over UDP protocol. The application is running fine. When I monitored the UDP traffic using Wireshark software, I found that there were too many Checksum errors. Please find attached the png file showing this error. I am about to... (0 Replies)
Discussion started by: AustinCann
0 Replies

5. UNIX for Dummies Questions & Answers

About Unix File creation time

Hello, I registered and recreated this thread because everywhere we can see "It's not possible to get the file creation time in UNIX fs". This is not true any more with Ext4! Unfortunately, there is not user-level tools that allow you to read those information. You have to use a low level tool... (4 Replies)
Discussion started by: gaellafond
4 Replies

6. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

7. Shell Programming and Scripting

UNIX script to check file and start the informatica server

Hi Rockers, I hope u r dng good one. I have a one question is in unix with informatica . I need a unix script to check whether particular file exists in the folder , If it means we have a informatica server , so we can start the informatica server by accessing that file. Every week we have... (0 Replies)
Discussion started by: gurukrishnan
0 Replies

8. Shell Programming and Scripting

unix script to check whether particular file exists and to find its size

I want to find the size of particular file exists in a particular directory and i wnt to zip it. In the below mentioned code it should check the MQ.log in the particular directory.Please correct my code so that it will check for particular MQ.log but i could not able to check whether the... (9 Replies)
Discussion started by: Balachandar
9 Replies

9. Shell Programming and Scripting

Unix File creation time

IS there any command to find the file creation time in Unix. (2 Replies)
Discussion started by: tinivt
2 Replies

10. Shell Programming and Scripting

Creation and Transfer of TAR file from one machine to another Using UNIX script

Hi, I want to create unix script such that it should run on machine A, it should run TAR commands on machine B and copy that TAR to machine C. Is it possible? Thanks Rahul (2 Replies)
Discussion started by: rahuljadhav
2 Replies
Login or Register to Ask a Question