Copy directory withOUT recursion

Hi,

I cannot find a way to copy a directory to another location with all attributes (mode, ownership, timestamps) but withOUT recursion (after so many years of working with Linux).

Say I want to create /home/jail/tmp exactly like /tmp but with nothing in it. Here is what I tried:

santiago@debian:~$ cp -p /tmp /home/jail/tmp
cp: -r not specified; omitting directory '/tmp'
santiago@debian:~$ cp -p -T /tmp /home/jail/tmp
cp: -r not specified; omitting directory '/tmp'

And if i add -r, it copies the content of the directory which is NOT what I want:

Any idea?
Regards

If you just want to copy regular files from /tmp to /home/jail/tmp , one could try something like:

for path in /tmp/*
do	[ -f "$path" ] && cp -p "$path" /home/jail/tmp
done

assuming that you're using a shell based on Bourne shell syntax.

# a root user 
mkdir /home/jail/
mkdir /home/jail/tmp
chmod 7777 /home/jail/tmp # set sticky bit

Try this?

Hi Don Cragun and thanks for your concern,
No I only want to copy the directory and its attributes, not the files nor the other folders in it.

------ Post updated at 15:15 ------

Hi jim mcnamara and thanks for your interest,
That technic would fail to copy any folder with nod other than 7777.
It would fail to copy /etc/sudoers.d to /home/jail/etc/sudoers.d and also fail to copy /var/run to /home/jail/var/run
What I want is to copy a folder and its attributes and nothing else.
I'm looking for a command that does what this script does:

#!/bin/bash
mkdir -p "$2"
chmod --reference="$1" "$2"
chown --reference="$1" "$2"
touch --reference="$1" "$2"

Does it exist at all?

Sorry I misunderstood what you were trying to do. Try:

pax -dLrw -p e /tmp /home/jail

if you're trying to create /home/jail/tmp with the same characteristics as /tmp or:

pax -dLrw -p e /tmp /home/jail/tmp

if you're trying to create /home/jail/tmp/tmp with the same characteristics as /tmp .

Note that on my system, /tmp is a symbolic link pointing to private/tmp . If /tmp can never be a symbolic link anywhere you might want to try this, you can leave out the pax -L option.

Note, however, that the destination directory ( /home/jail or /home/jail/tmp in the above two examples) must exist before you invoke pax .

1 Like

Didn't know the command pax .
I had to install the package pax .
It does exactly what I want.
Thanks

Another archiver, cpio

(cd / && echo tmp | cpio -pd /home/jail)

should create (or modify) /home/jail/tmp with the attributes of /tmp

1 Like

Even simpler, cpio makes an absolute path relative

echo /tmp | cpio -pd /home/jail