Difference between cp and mv linux command


 
Thread Tools Search this Thread
Top Forums Programming Difference between cp and mv linux command
# 1  
Old 05-26-2010
MySQL Difference between cp and mv linux command

Hi,

I am facing one problem only with mv command not with cp command. I have a test program

Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <fcntl.h>
#include <errno.h>

int sync_file(char *file)
{
FILE *fp=NULL;
int fd;

printf("file is %s\n",file);
fp = fopen(file, "r");
if(!fp)
return -1;

fd = fileno(fp);
fflush(fp);
fsync(fd);
ioctl (fd, BLKFLSBUF, 0);
fclose(fp);
return 0;

}

int main()
{
int len=0;
FILE *fp = NULL;
char buf[1024];
char *fname = "/etc/test.conf";
char fname_tmp[129] = "";


len = sprintf(buf, "%s\n", "Newly added Line is there");

snprintf(fname_tmp, 128, "%s.tmp", fname);

if( (fp = fopen(fname_tmp,"a")) == NULL )
printf(" ERROR: open(), error - %s\n",strerror(errno));

fprintf(fp,"%s",buf);
fflush(fp);

fsync(fileno(fp));
fclose(fp);
system("cp -f /etc/test.conf.tmp /etc/test.conf");
// system("mv -f /etc/test.conf.tmp /etc/test.conf");
sync_file(fname);
return 0;
}

Here i am opening a tmp file for writing. Then i am copying/moving for original file. Then i do a fflush, fsync(), ioctl() to the original file. Then i run this binary in linux machine(ext2 file system, 2.6.23.5 kernel) after that immediately power off the machine. Then power on machine, the file is disappeared or written data lost or file gets corrupted if i move the tmp file to the original file. And there is a no problem if i copy the tmp file to original file. So i want to know the difference between the cp and mv command. Can you please give me suggestion on it?

Thanks,
Indira

Moderator's Comments:
Mod Comment Moved post to its own thread - please no "threadjacking"; removed colour formatting; added code tags
# 2  
Old 05-26-2010
mv takes the file and places it in a another directory, the entry in the old directory is removed. if you mv from filesyem1 to filesystem2, mv behaves like cp + rm old file.
The rename() C call does what mv does.

cp duplicates the file - you have two entries in directories, old and new.

Is that what you mean?
# 3  
Old 05-27-2010
thanks for your replay. I know this. Actually I want to know file system data structure difference between cp and mv command. Because if copy the tmp file to original file then fsync() icotl()(seen in my source code) then power off machine there is no data loss. If i move the tmp file to original then there is a data loss. So i want to know what makes the difference between these.

Thanks,
Indira.
# 4  
Old 05-27-2010
The mv command first deletes the target file if it exists. The cp command does not.
# 5  
Old 05-27-2010
well, I suggest you read the source code for cp and mv

well, for a start, cp copies the file, so opens, reads and copies.
mv just moves or renames it. So the actual inode is not accessed,
only the Directory entries.

---------- Post updated at 12:16 PM ---------- Previous update was at 12:10 PM ----------

Quote:
Originally Posted by achenle
The mv command first deletes the target file if it exists. The cp command does not.
no that is wrong.

observe, if I mv (rename) a file the inode
does not change:

Code:
 
cat:$ls -i 1
82309 1
cat:$mv 1 2
cat:$ls -i 2
82309 2

if I do a cp the inodes actually change.

Code:
cat:$ls > 1
cat:$cp 1 2
cat:$ls -i 1 2
82964 1  82967 2

the inode is the physical disk block(s) where the file lives.
you have only updated the directory with a mv
# 6  
Old 05-27-2010
cp creates another copy of the file on disk. On the other hand mv renames the file. The original file gets a new name.

mv also used to move a file or set of files to a directory.
ex: mv *.c dirname

command mv oldname newname just gives the new name to file oldname

This is the diff between cp and mv
# 7  
Old 05-28-2010
As for your question. I believe calling fsync has nothing to do with the directory structure, which is entirely up to the file system modules in the kernel. So, I would guess when you mv and power off, the file system has not had a chance to flush its changes. As such, you lose the modifications it made to the directory structure. When you cp the file, it may flush the changes more immediately as it is creating a new file.... This is just a guess.... However, I do not believe your fix lies in the program you are writing, because I am not sure you can programatically control when the file system changes get flushed to disk.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

AIX to Linux command difference

Moving from AIX 6.1 to RHEL 6.6, I have noticed a few command differences. One that has been causing issue is a simple echo command when I have to use it this way -> "echo -e" On the AIX it outputs to "-e" but since RHEL has "-e" as an option for echo and hence it outputs to blank here. All... (3 Replies)
Discussion started by: aster007
3 Replies

2. Red Hat

Difference Redhat Linux/RH Enterprise Linux

what is the difference between Redhat Linux and Redhat Enterprise Linux. whereas Redhat linux have Server installation options too. (2 Replies)
Discussion started by: hananabbas
2 Replies

3. UNIX for Dummies Questions & Answers

Difference between UNIX and Linux

hi experts please tell me the real difference between unix and linux at kernel structure (1 Reply)
Discussion started by: linurag
1 Replies

4. Linux

What is the difference between Linux and Windows?

Hi, What is the difference between Linux and Windows? Thanks. (5 Replies)
Discussion started by: billcrosby
5 Replies

5. Linux

Difference between Windows and Linux

Hi, What is the difference between Linux and Windows? Thanks. (1 Reply)
Discussion started by: billcrosby
1 Replies

6. UNIX for Dummies Questions & Answers

difference between unix and linux

Hi I am new to linux I have dout waht is the difference between UNIX and LINUX Is there any soft for insatallation for UNIX OS Thanks (0 Replies)
Discussion started by: sanjaya
0 Replies

7. UNIX for Dummies Questions & Answers

su command difference between unix and linux

Hello all the su with -l option is running normal with linux but when i try to run it on unix AIX 5.2.7 it's not working with -l option any help (6 Replies)
Discussion started by: islam.said
6 Replies

8. UNIX for Dummies Questions & Answers

Difference between UNIX and Linux

OK, I've used various versions of UNIX(Solaris, HPUX, etc..) over the years. Now the organization I work for is leaning towards more Linux based systems(Redhat, Suse, etc..) I do see differences in in comands and how to accomplish basic adminstration, but nothing mind blowing. So, what is it... (5 Replies)
Discussion started by: pbonilla
5 Replies

9. Programming

Difference in LINUX and SOLARIS

/* * one-way-pipe.c - example of using a pipe to communicate data between a * process and its child process. The parent reads input * from the user, and sends it to the child via a pipe. * The child prints the received data to the... (1 Reply)
Discussion started by: SQ4LIFE
1 Replies
Login or Register to Ask a Question