Question

In my linux machine if i try to copy /proc/stat it is creating 0 byte file. but if i do cat /proc/stat it has data. but the size always shows as 0.

 cp /proc/stat statfile

is creating zero byte file. If i write a program to copy then it worked. why is it so ?

  int main() 
  {
    std::ifstream procFile("/proc/stat");
    std::ofstream outfile("statfile");
    char buf[1024];
    while (!procFile.eof() && procFile.is_open())
    {
            procFile.getline(buf, 1024);
            outfile << buf<<endl;
    }

    procFile.close();
    outfile.close();
    return 0;
  }
Was it helpful?

Solution

/proc is a pseudo-filesystem. It indicates a size of 0 for the file /proc/stat.

This is why copying doesn't work (cp does first look at the size of the file it has to copy), but you can easily read the information and write it back to a file.

$> cat /proc/stat > statfile

This was fixed in GNU coreutils 7.3.

OTHER TIPS

Use find to do a cp being as root user as shown,

find /proc/cpuinfo -type f -exec cp -iv '{}' /home/dir/ \;


Here is my attempt,

~/cpuinfo$ ls
~/cpuinfo$ sudo find /proc/cpuinfo -type f -exec cp -iv '{}' /home/esunboj/
  stackoverflow/cpuinfo/ \;
  `/proc/cpuinfo' -> `/home/esunboj/stackoverflow/cpuinfo/cpuinfo'
~/cpuinfo$ ls   
  cpuinfo

Also, as said /proc is a pseudo file system meaning the files are not stored on disk but have hooks to kernel. Probably you can read and/or write each entry from user space like a normal file operations. Usually different monitoring programs when they fopen a file, its now kernel job to find the appropriate driver which handles the task and assign it to that driver. Where in case of /proc its proc system in the kernel which will read the memory entities with the cost of system load involved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top