Copy directory withOUT recursion


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Copy directory withOUT recursion
# 1  
Old 07-25-2018
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:

Code:
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
# 2  
Old 07-25-2018
If you just want to copy regular files from /tmp to /home/jail/tmp, one could try something like:
Code:
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.
# 3  
Old 07-25-2018
Code:
# a root user 
mkdir /home/jail/
mkdir /home/jail/tmp
chmod 7777 /home/jail/tmp # set sticky bit

Try this?
# 4  
Old 07-25-2018
Quote:
Originally Posted by Don Cragun
If you just want to copy regular files from /tmp to /home/jail/tmp, one could try something like:
Code:
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.
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 ------

Quote:
Originally Posted by jim mcnamara
Code:
# a root user 
mkdir /home/jail/
mkdir /home/jail/tmp
chmod 7777 /home/jail/tmp # set sticky bit

Try this?
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:
Code:
#!/bin/bash
mkdir -p "$2"
chmod --reference="$1" "$2"
chown --reference="$1" "$2"
touch --reference="$1" "$2"

Does it exist at all?
# 5  
Old 07-25-2018
Quote:
Originally Posted by chebarbudo
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.
Sorry I misunderstood what you were trying to do. Try:
Code:
pax -dLrw -p e /tmp /home/jail

if you're trying to create /home/jail/tmp with the same characteristics as /tmp or:
Code:
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.

Last edited by Don Cragun; 07-25-2018 at 10:58 AM.. Reason: Add second note.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-25-2018
Didn't know the command pax.
I had to install the package pax.
It does exactly what I want.
Thanks
# 7  
Old 07-25-2018
Another archiver, cpio
Code:
(cd / && echo tmp | cpio -pd /home/jail)

should create (or modify) /home/jail/tmp with the attributes of /tmp
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory Tree/Recursion Issue . . .

Hope the title is close enough; didn't quite know how to put this... OK. We have a hypothetical script, script.a, with one simplistic line:bash ./nextDir/script.bNow script.b has a bit more going for it; and does file moves, copies, and renames, calling all the targets with respect to itself.... (7 Replies)
Discussion started by: LinQ
7 Replies

2. Shell Programming and Scripting

Copy directory without using cp-r

Hi guys, I need a solution to copy files and folders without using cp-r , cp-R or any variants. So i need to code it manually. I allready have a loop #!bin/bash IFS=$'\n' if test "$1" = "" then wd=pwd else wd=$1 fi for file in $(find $1) do if... (1 Reply)
Discussion started by: Wolverine89
1 Replies

3. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

4. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

5. UNIX for Dummies Questions & Answers

Copy everything in a directory

I use (cp new folder directory), but it just makes a new folder, i want to copy everything in a directory not the actual directory. (10 Replies)
Discussion started by: wiinick
10 Replies

6. UNIX for Dummies Questions & Answers

How to copy all files into the same directory

Dear All, Again I have another simple question. :confused: I want to write a csh which can copy all files of a current directory with a new name in the same directory, I mean: If I have tree bird apple as files in a directory I want to give ,say number 007 as argument to my csh and it copies... (3 Replies)
Discussion started by: dreamer0085
3 Replies

7. Shell Programming and Scripting

Copy Last modified directory to another directory

Hi, I have to copy last modified Directory to another directory.How can i Do using pipe or redirect cp -R 'ls -lrt|tail -1' test is not working. please help. (3 Replies)
Discussion started by: shaveta
3 Replies

8. Shell Programming and Scripting

Help with directory copy please

Hi, I have a folder that has about 100 directories. They all have different names. I need to do a find and copy of only directories (and their files) that start with ABC. There are directory names as ABCDE,ABCDG,ABCHT and so on. I tried using the AB* but that doesn't work. Can someone... (4 Replies)
Discussion started by: bbbngowc
4 Replies

9. SuSE

cant copy from one directory to another

I faced a problem when I tried to copy files from one directory to another using certain user although this user has r/w/x permission on source and destination directory ?? the OS is Linux-Suse 9 any help?? (2 Replies)
Discussion started by: mm00123
2 Replies

10. Shell Programming and Scripting

Recursion to Copy a Directory structure

I have the below code to recurse through a given file path. I am able to go to the leaf level once recursively but traversing out is not possible as the required varaibles don't have the values on return. Both typeset and local don't work for my variable definitions. I have the o/p of the sample... (2 Replies)
Discussion started by: avrkiran
2 Replies
Login or Register to Ask a Question