Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy directory content

Tags:

c++

c

linux

I want to copy the content o directory(tmp1) to another directory(tmp2). tmp1 may contain files and others directories. I want to copy the content of tmp1 (including the mode) using C/C++. If tmp1 contains a tree of directories I want to copy them recursively.

What is the simplest solution?

I found a solution to open the directory and read every entry and copy it with cp command. Any simpler solutions?

like image 350
Tandura Avatar asked May 19 '16 14:05

Tandura


People also ask

How do you copy the contents of a directory to another?

Answer: Use the cp Command You can use the cp command to copy files locally from one directory to another. The -a option copy files recursively, while preserving the file attributes such as timestamp. The period symbol ( . ) at end of the source path allows to copy all files and folders, including hidden ones.

How do I copy a directory content in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied.

How do I copy a directory and its contents in Unix?

To copy files or directories in Unix-based operating systems (Linux and MacOS), you use the cp command. The cp command is a relatively simple command, but its behavior changes slightly depending on the inputs (files vs directories) and the options you pass to it.


2 Answers

I recommend using std::filesystem (merged to ISO C++ as of C++17!)

Shamelessly copied from http://en.cppreference.com/w/cpp/filesystem/copy:

std::filesystem::copy("/dir1", "/dir3", std::filesystem::copy_options::recursive);

Read more about it:

https://gcc.gnu.org/onlinedocs/gcc-6.1.0/libstdc++/api/a01832.html

experimental::filesystem linker error

like image 176
cshu Avatar answered Sep 22 '22 20:09

cshu


Recently I had the same need, so I have developed the next chunk of code in order to solve the problem. I hope it helps to another people in the same situation.

#include <iostream>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <windows.h>

using namespace std;

bool is_dir(const char* path);
void copyFile_(string inDir, string outDir);
void copyDir_(const char *inputDir, string outDir);

int main()
{

    string srcDir = "C:\\testDirectory";
    string destDir = "C:\\destDir";

    copyDir_(srcDir.c_str(),  destDir);

    return 0;



}
void copyDir_(const char *inputDir, string outDir)
{

    DIR *pDIR;
    struct dirent *entry;
    string tmpStr, tmpStrPath, outStrPath, inputDir_str = inputDir;

    if (is_dir(inputDir) == false)
    {
        cout << "This is not a folder " << endl;
        return;
    }


    if( pDIR = opendir(inputDir_str.c_str()) )
    {

        while(entry = readdir(pDIR)) // get folders and files names
        {

            tmpStr = entry->d_name;
            if( strcmp(entry->d_name, ".")  != 0 && strcmp(entry->d_name, "..") != 0 )
            {
                tmpStrPath = inputDir_str;
                tmpStrPath.append( "\\" );
                tmpStrPath.append( tmpStr );

                cout << entry->d_name;
                if (is_dir(tmpStrPath.c_str()))
                {
                    cout << "--> It's a folder" << "\n";
                    // Create Folder on the destination path
                    outStrPath = outDir;
                    outStrPath.append( "\\" );
                    outStrPath.append( tmpStr );
                    mkdir(outStrPath.c_str());

                    copyDir_(tmpStrPath.c_str(), outStrPath);
                }
                else
                {
                    cout << "--> It's a file"   << "\n";
                    // copy file on the destination path
                    outStrPath = outDir;
                    outStrPath.append( "\\" );
                    outStrPath.append( tmpStr );
                    copyFile_(tmpStrPath.c_str(), outStrPath.c_str());
                }
            }
        }
        closedir(pDIR);
    }
}

bool is_dir(const char* path)
{
    struct stat buf;
    stat(path, &buf);
    return S_ISDIR(buf.st_mode);
}

void copyFile_(string inDir, string outDir)
{
    CopyFile(inDir.c_str(), outDir.c_str(), 1);
    DWORD Error = GetLastError();
}
like image 24
Jose M. Jimenez Avatar answered Sep 24 '22 20:09

Jose M. Jimenez