Robocopy everything in subdirectory excluding the root files - directory

How do I use robocopy so the root contents are not copied?
I already have root files stored elsewhere and I just would like to copy the sub directories and their contents while the source folder still containing root directory contents.

This is not possible with native robocopy switches as far as I can tell. You will need to use a script to enumerate the subdirectories and run robocopy against them.
Here is a sample PowerShell command that will accomplish what you want, copying everything from C:\temp\source\ to c:\temp\target\, excluding the files that are in c:\temp\source:
get-childitem c:\temp\source\* |?{$_.PsIsContainer} | %{robocopy $_.FullName c:\temp\target\$($_.Name) /S}
Credit to powershell ignore files in root but robocopy folders and their contents for the basics of this.

I don't have a reputation but the answer Mr. Hinkle gave solved 2 days of effort and searching. My challenge was moving the files that were > 1hour of age. This combination of powershell and robocopy appears to work. Below is my final code.
# Clear screen
cls
# Disconnect the drive if it exist - don't know where it is pointing to
If (Test-path p:) {
net use p: /delete
}
#Map the destination
net use p: \\server\dir1\dir2\dir3 password /USER:domain\user /P:no
get-childitem -path 'D:\dir1\dir2\' |
where-object {$_.LastWriteTime -lt (get-date).Addhours(-1)} |
?{$_.PsIsContainer} |
%{robocopy $_.FullName p:\$($_.Name) /S /MOVE /r:3 /W:1}
net use p: /delete

Related

Navigating directories when you don't know the name of the directory

I have a directory that is created through an external process. The directory is named 2021-12-08_1345 (YYYY-MM-DD_HHMM) based on the date and time when the process is executed. While this is the only directory in the path, I won't know the precise name of the directory. Is there a way to navigate to this folder knowing that it's the first and only directory?
The solution is cd $(ls -d -1 */ |sed -n '1p') where 1p is the nth directory that you want to navigate to. I came across the solution on an Ubuntu StackExchange https://askubuntu.com/questions/454688/how-do-you-cd-into-the-first-available-folder-without-typing-out-the-name#comment1800653_455113
I verified that this works on macOS 11.5.2+
If you are sure it is the only directory you can use
cd */
or
cd /path/to/*/
but this will fail if there is more than one directory.
Otherwise I suggest to use the solution from Ed Knittel's answer.
If you know that there is only one directory and no files you can even omit the trailing / from these commands, e.g. cd *.

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

Seemingly invalid No such file or directory error

I'm attempting to open a directory in Unix. If I enter the command
ls
I see the directory listed in my current directory but if I endter
cd [directory_name]
I get the error
No such file or directory
I'm also not able to auto complete the directory name using the 'tab' key. Does anyone know what may be causing this?
Check whether you are using the right capitalization? It's case sensitive. Add this to your ~/.inputrc if you want bash to not care about the case of the file.
set completion-ignore-case on
This is example:
user#stackoverflow:~$ ls
users questions file.txt
user#stackoverflow:~$ cd /questions
user#stackoverflow:~/questions$
Make sure that you're trying to access a valid folder and not a file.
To further explain:
List the current directory's contents (either one):
ls .
ls
List the home directory's contents (wherever you are):
ls ~
List the root directory's contents (wherever you are):
ls /

Unix- Copying the same file from multiple directories into a new directory while renaming the files

I have 36 subdirectories in the same directory named 10,11,12,....45 and a subdirectory logs
in each subdirectory (except for the directory logs) there is the same file called log.lammps
i was wondering if there was a way where i could copy each log.lammps file from each subdirectory 10-45 and put it in the sub directory logs while also adding the number of the directory that it originated from to the end of the filename
so i am looking for a code that copies the file log.lammps one by one from each subdirectory and every time the file gets copied into the directory logs, the filename gets changed from log.lammps to log.lammps10 if it came from the subdirectory 10 and when the file log.laamps from subdirectory 11 is copied into logs its name changes to log.lammps11 etc.
any help would be appreciated since right now i am only dealing with 30-40 files and in time i will be working with hundreds of files
Something along this line should work:
for f in [0-9][0-9]/log.lammps; do
d=$(dirname ${f})
b=$(basename ${f})
cp ${f} logs/${b}.${d}
done
That's easy-peasy with the magic of shell scripting. I'm assuming you have bash available. Create a new file in the directory that contains these subdirectories; name it something like copy_logs.sh. Copy-paste the following text into it:
#!/bin/bash
# copy_logs.sh
# Copies all files named log.lammps from all subdirectories of this
# directory, except logs/, into subdirectory logs/, while appending the name
# of the originating directory. For example, if this directory includes
# subdirectories 1/, 2/, foo/, and logs/, and each of those directories
# (except for logs/) contains a file named log.lammps, then after the
# execution of this script, the new file log.lammps.1, log.lammps.2, and
# log.lammps.foo will have been added to logs/. NOTE: any existing files
# with those names in will be overwritten.
DIRNAMES=$( find . -type d | grep -v logs | sed 's/\.//g' | sed 's/\///g' | sort )
for dirname in $( echo $DIRNAMES )
do
cp -f $dirname/foo.txt logs/foo$dirname
echo "Copied file $dirname/foo.txt to logs/foo.$dirname"
done
See the script's comments for what it does. After you've saved the file, you need to make it executable by commanding chmod a+x copy_logs.sh on the command line. After this, you can execute it by typing ./copy_logs.sh on the command line while your working directory is the directory that contains the script and the subdirectories. If you add that directory to your $PATH variable, you can command copy_logs.sh no matter what your working directory is.
(I tested the script with GNU bash v4.2.24, so it should work.)
For more on bash shell scripting, see any number of books or internet sites; you might start with the Advanced Bash-Scripting Guide.

Resources