-1

To copy the contents of one directory into another, I can use the following:

cp -Rip source-dir/ ../destination-dir/

However, it seems that using the trailing slash at the end of source directory when using cp and mv commands is somewhat discouraged.

No trailing slash on source directory

You should not put a trailing slash on the source directory:

The point is relevant to cp - but also to mv, where it is much more important.

I'll cite the warning from the manual - note that it's not found in the man page, but in the info page info coreutils 'mv invocation':

Warning: Avoid specifying a source name with a trailing slash, when it might be a symlink to a directory. Otherwise, 'mv' may do something very surprising, since its behavior depends on the underlying rename system call. On a system with a modern Linux-based kernel, it fails with 'errno=ENOTDIR'. However, on other systems (at least FreeBSD 6.1 and Solaris 10) it silently renames not the symlink but rather the directory referenced by the symlink.

Is it really so? And if the answer is "yes", what is the recommended way to copy the contents of a directory?

jsx97
  • 117
  • 5
  • The workaround I recommend is to check if the source is a symbolic link or not first. – oguz ismail Jan 14 '23 at 16:39
  • In what way would you want a workaround? – konsolebox Jan 14 '23 at 18:10
  • @konsolebox Well, it would be more correct to call it just "a recommended way to copy the contents of a directory" rather than a "workaround". – jsx97 Jan 14 '23 at 18:42
  • 1
    [apple.se] is a better place for questions about using Apple-provided `mv` and `cp` commands. Those commands aren't included with the source of bash or zsh; it's 100% your OS vendor's decision how they behave, not your shell's. – Charles Duffy Jan 14 '23 at 18:45
  • That said, personally, I use `source-dir/. ../destination-dir` when I want `source-dir/A` to end up as `destination-dir/A` -- note the `/.` on the end of the source directory spec. – Charles Duffy Jan 14 '23 at 18:47
  • 1
    (Also consider [unix.se] or [Super User](https://superuser.com/) as available venues for questions on use of UNIX tools that aren't narrowly specific to software development). – Charles Duffy Jan 14 '23 at 18:51

1 Answers1

1

I think you would be better off using tar for content replication. It would give you finer control and flexibility over the actions being performed. Sometimes simple is just too simple.

To that end, I propose the following as an initial flow of logic to address your concerns. You can then tweak that as you see fit.

#!/bin/bash

STRT=`pwd`
OldRootDIR="${STRT}/Something"
NewRootDIR="${STRT}/Dupl"

mkdir  "${NewRootDIR}"
ls -ld "${OldRootDIR}"
ls -ld "${NewRootDIR}"

option=""

while [ $# -gt 0 ]
do
    case $1 in
        --symlink_contents )    option="--dereference" ; shift ;;
        --symlinks_only )   option="" ; shift ;;
        * ) echo -e "\n\t Invalid parameter identified on command line.  Only allowed:  [ --symlink_contents | --symlinks_only ] \n Bye!\n" ; exit 1 ;;
    esac
done

if [ ! -d "${OldRootDIR}" ]
then
    echo -e "\n\t Directory '${OldRootDIR}' does not exist.  Unable to proceed.\n Bye!\n" ; exit 1
fi

if [ ! -d "${NewRootDIR}" ]
then
    echo -e "\n\t Directory '${NewRootDIR}' does not exist.  Unable to proceed.\n Bye!\n" ; exit 1
fi

( cd "${OldRootDIR}" ; tar cf - --one-file-system ${option} . ) | ( cd "${NewRootDIR}" ; tar xvpf - )
Eric Marceau
  • 1,601
  • 1
  • 8
  • 11