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 (*/
)
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 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.
Replicate a directory file structure with empty files
From windows vista, robocopy
is included in the OS (for previous versions there is a download from Microsoft)
robocopy \mydir \mydir_structure /create /e
/e
indicates a recursive copy including empty folders.
/create
creates the empty files in the target hierarchy.
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 whole directory but exclude all folders and subfolders with certain name
Instead of cp
, you can use tar
with option --exclude
to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
- So any path that contains the "outdir" pattern will be excluded.
- Without the
--exclude
option, it will copy the entire structure of your current directory under TARGET_DIRECTORY. - You can replace the
.
in the first tar by your desired source directory.
Related Topics
Packaging Proprietary Software for Linux
File Size in Human Readable Format
Adding a New System Call in Linux Kernel 3.3
Coreos - Get Docker Container Name by Pid
Shell Script Change Directory with Variable
How to Tail -F the Latest Log File with a Given Pattern
How to Force Node.Js Require to Be Case Sensitive
Setting Up a Workspace Using Team Explorer Everywhere on Linux
List Files Recursively in Linux Cli With Path Relative to the Current Directory
How to List the Files in a Zip Archive Without Decompressing It
Option to Display Control Characters in Gedit
Difference Between Completion Variables and Semaphores
How to Delete Everything in a String After a Specific Character
How to Add JSON Object to JSON File Using Shell Script
What Is the Best Tool to Convert Common Video Formats to Flv on a Linux Cli