Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

in linux terminal, I have this command: mkdir -p sourcedestination/ ; find res/ -type f…

  1. User

    in linux terminal, I have this command: mkdir -p source_destination/ ; find res/ -type f -exec cp u {} destination_folder/ ';' it works but only copy the files of the source destination instead of files and folders recursively, how can I improve the copy command?

  2. Assistant

    To recursively copy both files and folders using the cp command in the Linux terminal, you can modify your command as follows:

    mkdir -p source_destination/ && find res/ -exec cp -ur {} destination_folder/ ';'
    

    The modification includes replacing the -type f option with -exec cp -ur {} destination_folder/, where:

    • -u checks if the destination file has a newer timestamp or not before copying. This prevents overwriting newer files.
    • -r copies directories recursively along with all their contents.

    With these modifications, the cp command will now copy both files and folders from res/ to destination_folder/ recursively.

  3. User

    it copy everything but doesn't respect the folders, so every files is at the same level, how can I copy everything folders included

  4. Assistant

    Apologies for the confusion. To preserve the folder structure while copying files recursively, you can use the rsync command instead of cp. Here's the modified command:

    mkdir -p source_destination/ && rsync -av --exclude='*' --include='*/' --include='*.*' res/ destination_folder/
    

    In this command:

    • rsync is used instead of cp.
    • -a preserves the file permissions, timestamps, and other attributes.
    • -v enables verbose output.
    • --exclude='*' excludes files from being copied.
    • --include='*/' includes directories.
    • --include='*.*' includes files matching any extension.
    • res/ is the source directory.
    • destination_folder/ is the destination directory.

    By using rsync with these options, you can copy files and folders recursively, preserving the folder structure in the destination folder.

  5. User

    i don't like rsync command, is there another alternative?

  6. Assistant

    If you prefer not to use rsync, you can achieve the same effect using the cp command along with the find command to handle recursive copying. Here's an alternative command:

    mkdir -p source_destination/ && find res/ -type d -exec mkdir -p destination_folder/{} \; -o -type f -exec cp {} destination_folder/{} \;
    

    In this command:

    • mkdir -p source_destination/ creates the destination directory if it doesn't exist.
    • find res/ -type d -exec mkdir -p destination_folder/{} \; finds directories in the source directory (res/) and creates them in the destination directory (destination_folder/) using mkdir -p.
    • -o acts as the logical OR operator.
    • -type f -exec cp {} destination_folder/{} \; finds files in the source directory and copies them to the corresponding directory in the destination directory using cp.

    Using this alternative command, both files and folders will be copied recursively while preserving the directory structure in the destination folder.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.