copy files from multiple sub-directories to the same destination directory

723

Solution 1

If the files have non-unique names, then

cp "Main Directory"/*/Subsubdirectory1/* destdir

would overwrite some of the files at the destination. This would also fail if there are thousands of matching pathnames.

To get around this, using GNU cp:

for pathname in "Main Directory"/*/Subsubdirectory1/*; do
    cp --backup=numbered "$pathname" destdir
done

This would create numbered backups of the files that would otherwise have been overwritten.

The same thing but using non-GNU cp:

for pathname in "Main Directory"/*/Subsubdirectory1/*; do

    # create first stab at destination pathname
    dest="destdir/${pathname##*/}"

    i=0
    while [ -e "$dest" ]; do
        # destination name exists, remove backup number from end of
        # pathname and replace with next one in the sequence
        i=$(( i + 1 ))
        dest="${dest%.~*~}.~$i~"
    done

    cp "$pathname" "$dest"
done

Solution 2

Try this:

cp mainDir/*/SubsubDirectory1/* <destination_path>

Note: file names must be different.

Share:
723

Related videos on Youtube

Rui
Author by

Rui

Updated on September 18, 2022

Comments

  • Rui
    Rui almost 2 years

    In my app, I need to spawn a debugger process given a dump file to debug, say "example.dmp". If the dump file is not found or not it is not a dump file. The process will be successfully spawned but it will exit immediately. I would like to throw an error message in an async function which can be try catched later.

        const spawnDebuggerProcess = (debuggerConfigs) => {
            let debuggerProcess = spawn(config.debuggerName, debuggerConfigs)
    
            debuggerProcess.on('exit', (code, signal) => {
                console.log('pid ' + `${debuggerProcess.pid}`)
                console.log('child process exited with ' + `code ${code} and signal ${signal}`)
            })
    
            debuggerProcess.on('error', (error) => {})
    
            if (debuggerProcess.pid !== undefined) {
                return debuggerProcess
            }
    
            throw externalError.createError({
                name: externalError.SPAWN_DEBUGGER_PROCESS_ERROR,
                description: 'Failed to spawn debugging process'
            })
        }
    

    One of my thought is to time give this function a time window before returning. If the process exited before the time window I should throw an error. But since node.js is async. I don't have an idea of how can this be realized. Thanks!

    ==== EDIT =====

       const spawnDebuggerProcess = async (debuggerConfigs) => {
            let debuggerProcess = spawn(config.debuggerProcess.debuggerName, debuggerConfigs)
    
            /** added a flag */
            let exited = false
    
            debuggerProcess.on('exit', (code, signal) => {
                console.log('pid ' + `${debuggerProcess.pid}`)
                console.log('child process exited with ' + `code ${code} and signal ${signal}`)
    
                /** set flag to true if exited */
                exited = true
            })
    
            debuggerProcess.on('error', (error) => {})
    
            if (debuggerProcess.pid !== undefined) {
    
                /** delay this promise for a certain amount of time, act as the time window */
                await delay(config.debuggerProcess.immediateExitDelay)
                /** check the flag */
                if (exited) {
                    throw externalError.createError({
                        name: externalError.SPAWN_DEBUGGER_PROCESS_ERROR,
                        description: 'Process exited immediately'
                    })
                }
    
                return debuggerProcess
            }
    
            throw externalError.createError({
                name: externalError.SPAWN_DEBUGGER_PROCESS_ERROR,
                description: 'Failed to spawn debugging process'
            })
        }
    

    It seems to be working, but I am not sure if it is a good practice.

  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    I will give it a try and give you feedback
  • Mostafa Hussein
    Mostafa Hussein almost 6 years
    Thanks a lot, the files have unique names, so I will try the first solution, and tell you if it works