Copy folder structure (without files) from one location to another
You could do something like:
find . -type d > dirs.txt
to create the list of directories, then
xargs mkdir -p < dirs.txt
to create the directories on the destination.
copy directory structure without files
If the target is a relative path your command will get into an infinite loop. Below one doesn't have that problem.
find "$1/." -type d -exec bash -c '
mkdir -p "${@/*\/.\//"$0"/}"' "$2" {} +
- Uses
$1/.
as the starting point so that/./
marks where source path ends in each path. - Relies on bash's pattern substitution PE;
"${@/*\/.\//"$0"/}"
replaces the longest match of*/./
with the target path in each member of$@
(i.e paths selected) and expands to the resultant list. - Abuses
$0
a bit but it won't cause any harm.
Handling of positional parameters is left to OP.
how to copy directory structure without actually copying the content into another directory
This worked for me:
import java.io.File;
public class StartCloneFolderOnly {
/**
* @param args
*/
public static void main(String[] args) {
cloneFolder("C:/source",
"C:/target");
}
public static void cloneFolder(String source, String target) {
File targetFile = new File(target);
if (!targetFile.exists()) {
targetFile.mkdir();
}
for (File f : new File(source).listFiles()) {
if (f.isDirectory()) {
String append = "/" + f.getName();
System.out.println("Creating '" + target + append + "': "
+ new File(target + append).mkdir());
cloneFolder(source + append, target + append);
}
}
}
}
How to copy folder structure under another directory?
For me the following works fine:
Iterate over existing folders
Build the structure for the new folders based on existing ones
- Check, if the new folder structure does not exist
- If so, create new folder without files
Code:
import os
inputpath = 'D:/f/'
outputpath = 'D:/g/'
for dirpath, dirnames, filenames in os.walk(inputpath):
structure = os.path.join(outputpath, dirpath[len(inputpath):])
if not os.path.isdir(structure):
os.mkdir(structure)
else:
print("Folder does already exits!")
Documentation:
os.walk
os.mkdir
os.path.isdir
Create folder structure and copy file inside without specific path
It wasn't absolutely clear to me which directories you wanted the source file, in this case C:\Users\Giovani\Pictures\Portrait.jpg
, to be copied to, So I'm offering two complete batch-file options, where the final directories will be placed along side the batch file itself.
The first will copy to each 'new' directory, including 1.Capture
:
@Echo Off
For %%G In (
"1.Capture"
"1.Capture\Selected"
"1.Capture\Discard"
"2.Projects"
"3.Masters"
"4.Web"
"5.Instagram"
) Do %SystemRoot%\System32\xcopy.exe "C:\Users\Giovani\Pictures\Portrait.jpg" "%~dp0%%~G\" /CHIKQRY 1>NUL
The second will do the same, creating the 1.Capture
directory, but not copying to it:
@Echo Off
SetLocal EnableExtensions
For %%G In (
"1.Capture\Selected"
"1.Capture\Discard"
"2.Projects"
"3.Masters"
"4.Web"
"5.Instagram"
) Do %SystemRoot%\System32\xcopy.exe "C:\Users\Giovani\Pictures\Portrait.jpg" "%~dp0%%~G\" /CHIKQRY 1>NUL
Copy folders without files, files without folders, or everything using PowerShell
To copy everything in a folder hierarchie
Copy-Item $source $dest -Recurse -Force
To copy the hierarchie you can try:
$source = "C:\ProdData"
$dest = "C:\TestData"
Copy-Item $source $dest -Filter {PSIsContainer} -Recurse -Force
To flatten a file structure you can try:
$source = "C:\ProdData"
$dest = "C:\TestData"
New-Item $dest -type directory
Get-ChildItem $source -Recurse | `
Where-Object { $_.PSIsContainer -eq $False } | `
ForEach-Object {Copy-Item -Path $_.Fullname -Destination $dest -Force}
Good luck!
copy a directory structure with file names without content
You can use find:
find src/ -type d -exec mkdir -p dest/{} \; \
-o -type f -exec touch dest/{} \;
Find directory (-d
) under (src/
) and create (mkdir -p
) them under dest/
or (-o
) find files (-f
) and touch
them under dest/
.
This will result in:
dest/src/<file-structre>
You can user mv
creatively to resolve this issue.
Other (partial) solution can be achieved with rsync:
rsync -a --filter="-! */" sorce_dir/ target_dir/
The trick here is the --filter=RULE
option that excludes (-
) everything that is not (!
) a directory (*/
)
How to copy a directory structure but only include certain files (using windows batch files)
You don't mention if it has to be batch only, but if you can use ROBOCOPY
, try this:
ROBOCOPY C:\Source C:\Destination data.zip info.txt /E
EDIT: Changed the /S
parameter to /E
to include empty folders.
Related Topics
How to Add Deploy Key for 2 Repo with 1 User on Github
Why Does Arm-Linux-Gnueabi-G++-4.4 Always Build a "7-A" Binary
Why Is Dd with the 'Direct' (O_Direct) Flag So Dramatically Faster
Bypass Rsync Prompt "Are You Sure You Want to Continue Connecting"
Bash: Add String to the End of the File Without Line Break
How to Make Sure the Numpy Blas Libraries Are Available as Dynamically-Loadable Libraries
Ctrl-P and Ctrl-N Behaving Unexpectedly Under Docker
Stripping Single and Double Quotes in a String Using Bash/Standard Linux Commands Only
Easy Way of Installing Eclipse Plugins on Ubuntu
Getting Disconnection Notification Using Tcp Keep-Alive on Write Blocked Socket
Node Server Crashes After Few Hours
What Does Double-Dash Do When Following a Command
Why Does %Rbp Point to Nothing
How to Send Data to Local Clipboard from a Remote Ssh Session
How to List All Users in a Linux Group
Fastest Way to Tell If Two Files Have the Same Contents in Unix/Linux