UNIX/ copy directory to new directory - unix

I need to copy a directory and its content to a NEW directory.
Using: cp -r dir1/dir2 dir1/dir3
where dir3 is going to be created anew, it just copies the file(s) I have within dir2... It's like it copies dir2 naming it dir3.
Is there any way to copy dir2 into dir3?
I need to do it with a single command.

It's not really possible to do it in 1 command with cp. What you want is for dir3 to already exist when you do your copy. I'm not sure if there's a real reason why you have to do it in 1 command or not. You can certainly do it in one line.
mkdir dir/dir3;cp -r dir1/dir2 dir1/dir3

Related

How do I ls a directory to get it's data, but none of its files?

I wish to run ls -alh on a directory, so that I can get when the directory was last modified.
I do NOT want to get the ls information for the files in that directory, just that directory.
Is this possible?
Turn's out it's easy
ls -alhd
The -d flag reports on directories, not their files

MacOS: xargs cp does not copy subdirectories

I am on Mac OS.
I have a directory with round about 3000 files and several subdirectories (wordpress installation)
Now I have to find all the files in a similar directory (have to separate master and child installation) that are additional files and have to copy them away into another directory.
I use this command:
$ diff -rq dt-the7 dt-the7-master-from-Yana|grep 'Only in dt-the7'|awk {' print $3 $4 '}|sed 's/:/\//g'|xargs -J {} rsync -av {} neu/
but somehow a certain file 3d.png and a list of other that should be in a subdir of the destination dir are copied into the root dir of the destination.
Any idea why that might be?
It makes no difference whether I use cp, rsync or ditto
You need the -R relative option on your rsync command.
Without this rsync just copies the item referenced rather than the path referenced, so items at the root level are copied as you expected but items in sub-directories are also copied to the root, which is not what you wanted.
With the option rsync takes account of the relative path and recreates it at the destination.
An example with another command might help, consider:
cp A/B.txt C/
that will copy B.txt into C, it does not create a folder A in C which in turn contains the file B.txt. rsync without -R behaves like that cp command, with -R it creates the A directory in C.
HTH

How to diff all modifications of all checkout files under a directory recursively with Clearcase?

I'm using ClearCase. I checked out several files under a directory, some of files in its sub-directory, some in sub-sub-directory.
What I want is to list the diff of all my modifications on these checked out files under this certain directory recursively.
What I currently do is:
for file in $(cleartool lsco -recurse -me -cview -fmt "%n\n"); do
cleartool diff -serial_format -pred $file;
done
I use a bash for loop, but I perhaps it can be done with a simple ClearCase command.
The OP suggests using the list of checked out files, but there is no way to find the diff in one cleartool command.
An xargs (used here) might be easier
cleartool lsco -recurse -me -cview -fmt "%n\n" | xargs -n 1 cleartool diff -serial_format -pred

How to copy a entire directory which contains symlinks?

I want to copy a complete directory content from /home/private_html/userx/ into the /home/private_html/usery/, the problem is that the directory userx contains few symlinks, and when using the cp it just skip them (skip occurs, if symlinks directs into a file, in case if it points into the directory, it just copy WHOLE directory instead...).
The command I was using looks following:
# cp -iprv /home/private_html/userx/ /home/private_html/usery/
Has anyone a solution to copy the directory "just as it is" into other place?
On FreeBSD, cp doesn't have an -r option. It does have -R, which should do what you want:
-R If source_file designates a directory, cp copies the directory and
the entire subtree connected at that point. If the source_file
ends in a /, the contents of the directory are copied rather than
the directory itself. This option also causes symbolic links to be
copied, rather than indirected through, and for cp to create spe‐
cial files rather than copying them as normal files. Created
directories have the same mode as the corresponding source direc‐
tory, unmodified by the process' umask.
Roland is right about the -R flag. You could also use a pair of tar-processes, which would make your command a little bit more system-independent:
tar -C /home/private_html/userx/ -cpf - . | tar -C /home/private_html/usery/ -epf -

How to copy a directory in a Makefile?

I have a directory images/ that I want to copy to build/images/ from within a Makefile. The directory might contain multiple levels of subdirectories. What would be the most elegant way to do that? I want:
avoid a full directory copy on each make run (i.e. no cp -r)
guaranteed consistency (i.e. if a file changed in images/ it should be automatically updated in build/images/)
avoid to specify a rule for each image and each subdirectory in the Makefile
solve the issue within make, so no rsync or cp -u if possible
I am using GNU make, so GNU specific stuff is allowed.
Well, I'd just use rsync. Any make script you will create with these constraints will just replicate its functionality, and most probably will be slower and may contain bugs. An example rule might look:
build/images:
rsync -rupE images build/
.PHONY: build/images
(.PHONY to trigger the rule every time).
Maybe symlinks or hardlinks can be used instead?
build/images:
ln -s ../images build/images
If you really want to avoid rsync and links, this piece re-implements them somehow (not tested, needs find, mkdir, and plain cp):
image_files:=$(shell find images -type f)
build/images/%: images/%
mkdir -p $(#D)
cp $< $#
build: $(patsubst %,build/%,$(image_files))

Resources