How to check a file exist and do a copy of other files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check a file exist and do a copy of other files
# 1  
Old 12-19-2007
How to check a file exist and do a copy of other files

Hi,

I would like to perform bash which would check the file A.txt to be size 0 or not. If the size is 0, I would copy file B.txt to replace A.txt.

Please help.

Thanks.


-Jason
# 2  
Old 12-19-2007
look into 'man test'
# 3  
Old 12-19-2007
Thanks.

But if i would like to add in a third field with 0 of the intended copied file; how could I do so without creating another files.

Means :-

in MM folder I have A.txt(size which is 0) and in NN folder I have similar A.txt (size more than 0)

if [-s MM/A.txt] then

else cat NN/A.txt <perform the addition of all 0s in third field of A.txt>
copy MM/A.txt to NN/A.txt

I would like to perform the substitution if the size is 0 before copied to the new path.


Thanks.

-Jason

Last edited by ahjiefreak; 12-19-2007 at 10:31 PM..
# 4  
Old 12-19-2007
It's easier to read if you use 'CODE' tages around code:
Quote:
Originally Posted by ahjiefreak
But if i would like to add in a third field with 0 of the intended copied file; how could I do so without creating another files.

Means :-

in MM folder I have A.txt(size which is 0) and in NN folder I have similar A.txt (size more than 0)
Code:
if [-s MM/A.txt] then

else cat NN/A.txt <perform the addition of all 0s in third field of A.txt> 
copy MM/A.txt to NN/A.txt

I would like to perform the substitution if the size is 0 before copied to the new path.
Last edited by ahjiefreak : 3 Minutes Ago at 03:31 PM.
So NN/A.txt contains some 0's in the third field, and you want to add then up? <confused>
Or do you want to add them to MM/A.txt? Even though it's currently blank? <still confused>

Please show an example of what you are trying to do.

Also, consider using [ ! -s ] so you don't need the 'else' section in your code.
# 5  
Old 12-19-2007
Hi,

Please disregard the previous said problem as I think I was quite confused by that time.

More clearer picture is below:-

Let us consider we have two files; A.txt and B.txt.

First, I would check the A.txt; if the A.txt is zero size; i would like to copy B.txt content to A.txt but changing all the second field of B.txt content to 0.

Given B.txt input is

1 2
2 3
3 4
1 5
1 6

And output of A.txt would be

1 0
2 0
3 0
1 0
1 0

Currently, I am thinking to use the

if [!- s A.txt]
paste B.txt |awk '{$2=0}' >> A.txt

But it does not work. Please advise. THanks.

-Jason
# 6  
Old 12-19-2007
Remember to use 'CODE' tags Smilie

You need a 'then' clause in your if statement and to end it with a 'fi'

I don't see a reason to use paste,
just use awk to grab the first column:
Code:
awk '{ print $1,"0" }' < B.txt > A.txt

# 7  
Old 12-20-2007
Code:
if [ ! -s "file" ];then
    while read one two
    do
        echo "$one 0" >> A.txt
    done < B.txt
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if 10 files exist

Hi All, Whenever i get 10 files(file names like sales*) then another file need to create. May i know how to implement this in KSH. (4 Replies)
Discussion started by: siddireddy
4 Replies

2. UNIX for Dummies Questions & Answers

Does rsync check and ignore files that already exist?

Hi, We have two (2) servers named primary and standby. There is a directory named /db01/archive that we need to keep in-sync. Files get transferred from primary and standby. Sometimes when we do a failover or when there is a network issue, some files fail to get transferred. I want to use... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. UNIX for Dummies Questions & Answers

Rsync copy files if dont exist

I have a setup where I have two drives. TV TVbackup For what ever reason, I have a lot of content on my TVbackup drive which isn't on my TV drive. I want to copy all the files across which are on TVbackup but are not currently on TV. If there is a file with the same name but a... (2 Replies)
Discussion started by: Spadez
2 Replies

4. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. UNIX for Dummies Questions & Answers

copy files as space exist in file name..

Hi, i am having a directory in which files are having space in the name . $ls -1 aa b.txt my file.pdf lost file.csv foo_file.txti want to copy those file to some where with date +%F as extension . But it failed for the file having space. #!/bin/sh ls -1 >tt for var in `cat tt` do b=$var... (2 Replies)
Discussion started by: posix
2 Replies

7. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

8. HP-UX

Copy only files whiich do not exist on Target Directory

Hi, I am using HP-UX B 11.23 I want to copy files from one directory into another directory in such a way that, only the nonexistent files in target directory are copied from source directory e.g. dir1 has file1 file2 file3 dir2 has file1 file3 now I want that a command... (7 Replies)
Discussion started by: Chetanaz
7 Replies

9. Shell Programming and Scripting

check/wait for files to exist before continuing

I'm attempting to write a pretty simple script. It opens a Filemaker file successfully. That Filemaker file takes around 30-90 seconds to finish. When it's done, it writes a few .xml files into the same directory where my shell script and the Filemaker script reside. In my script, how can I... (2 Replies)
Discussion started by: alternapop
2 Replies

10. Shell Programming and Scripting

Check if certain files exist in a directory, if not add name to a textfile

We recieve some logs on our windows box via FTP on a daily basis, in the same directory. I would like to check for missing logs files and add their name to a text file. Something like... Check if C:\logs\file1_currentdate exists (if not, add file1_currentdate to... (1 Reply)
Discussion started by: SunnyK
1 Replies
Login or Register to Ask a Question