Linux copy only directory structure

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to copy the directory structure without the files in linux

Sometimes, you want to replicate just the structure of a directory to another location, without any of the files that reside inside the directory or the sub-directories. It is quite easy to copy over entire directory including the files by using the cp command. But the cp command does not provide an easy way to exclude files while copying or an option that will allow to just copy directory structure or folders.

There are still couple of command line options available to you, if you do not mind piping some commands together. Most of the commands to copy directory structure and its variations had either the find command or rsync in some fashion.

In the examples below, assume that the source directory has an absolute path /path/to/source and that the destination folder is at /path/to/dest.

Using find and mkdir

Most if not all of the options available will involve the find command in some way. That is because the find command is quite versatile and is an easy way to list just the files, the folders or both.

This option uses the mkdir command with the find command. This method also requires that you be inside the source folder while executing the command.

Using find and cpio

The cpio command in Linux is used to copy files to and from archives. We can re-purpose it in order to copy just the folders to another folder. This method has the additional advantage that you need not be in the source folder while executing the command, unlike the earlier method.

The cpio may not be installed by default in certain Linux distros. If that is the case, then you will need to install it before using this command.

bash$ find /path/to/source -type d | cpio -pd /path/to/dest/

Using rsync

rsync is yet another command that can be used to copy directory structure. This is also very useful if you like to keep two directory structures in sync over a period of time or you will need to copy directory structure in its entirety multiple times.

Using rsync also gives you the option to replicate by just updating the directories when run multiple times and also the ability to handle deletes in the source without having to remove the entire destination directory. You may use either of the following command line options to exclude files.

Читайте также:  Linux read file page by page

bash$ rsync -a —include ‘*/’ —exclude ‘*’ /path/to/source /path/to/dest

bash$ rysnc -a -f»+ */» -f»- *» /path/to/source /path/to/dest

We use the -a (or –archive) option that provides several useful functions, such as recursion, preserving the times, permissions and owner of the directories and symlink support. After the first replication, additional changes can be propagated without having to copy over the entire structure again.

bash$ rysnc -a -u —del —force —include ‘*/’ —exclude ‘*’ /path/to/source /path/to/dest

bash$ rysnc -a -u —del —force -f»+ */» -f»- *» /path/to/source /path/to/dest

The options used above are

-u (–update) : update the changed or modified directories or files only
-del : delete during the copy. The other options include –delete-before and –delete-after, that deletes files before or after the file transfers
–force : This option will delete the directories forcefully, even if the destination folder contains files or sub directories.

Excluding some sub-directories

All of the above options copies over the entire directory structure or updates them. But sometimes, you might want to exclude some sub-directories based on name. For example, you might want to exclude all the directories named logs that are scattered over various sub directories.

One option is to copy over all the directories and then delete the ones that are not needed. Even better is the option to not copy over the directories that are not needed.

bash$ find /path/to/source -type d -not -name logs| cpio -pd /path/to/dest/

This will not copy over directories with the path that ends with folder name logs. If the logs folder happens to have sub-folders then those will be copied thus copying the parent logs folder itself. In order avoid that, and ignore all the sub-directories under logs/ as well, use the following

bash$ find /path/to/source -type d -not -name logs -not -path */logs/* | cpio -pd /path/to/dest

You can do the same thing with the rsync command as well. The order in which the -f is provided in the command does matter, in that the first matched pattern is honored.

bash$ rysnc -a -f»- logs/» -f»+ */» -f»- *» /path/to/source /path/to/dest

Excluding some of the files and not all

All of the above method excludes all the files inside the source folder. Instead of excluding all the files, you can exclude files selectively based on some file criteria such as file names, extensions, owner, modification dates etc.

We will see how you can exclude all the log files (ie. files with a .log extension) while still copying over all the other files.

bash$ rsync -a -f»+ */» -f»- *.log» -f»+ *» /path/to/source /path/to/dest

Both the find and rsync commands are flexible enough to be used to copy directory structure, but the rsync is a better option for two main reasons: 1) It supports creation of directories and does need to be piped to another command such as mkdir or cpio and 2) the include and exclude options are much more flexible.

Читайте также:  Установка lineage сервер linux

All of the above commands can be made into custom scripts so that it can used and re-used. This is especially handy if you do use them frequently.

Источник

Tar only the Directory structure

I want to copy my directory structure excluding the files. Is there any option in the tar to ignore all files and copy only the Directories recursively.

5 Answers 5

You can use find to get the directories and then tar them:

find .. -type d -print0 | xargs -0 tar cf dirstructure.tar --no-recursion 

If you have more than about 10000 directories use the following to work around xargs limits:

find . -type d -print0 | tar cf dirstructure.tar --no-recursion --null --files-from - 

In earlier GNU tars apparently the position where you placed the —no-recursion argument did not matter for it to work while in more current versions it apparently needs to preceed the —files-from option. My backups just exploded after upgrading from Debian Wheezy to Debian Stretch which includes GNU tar 1.29. I edited the answer accordingly.

Directory names that contain spaces or other special characters may require extra attention. For example:

$ mkdir -p "backup/My Documents/stuff" $ find backup/ -type d | xargs tar cf directory-structure.tar --no-recursion tar: backup/My: Cannot stat: No such file or directory tar: Documents: Cannot stat: No such file or directory tar: backup/My: Cannot stat: No such file or directory tar: Documents/stuff: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors 

Here are some variations to handle these cases of «unusual» directory names:

$ find backup/ -type d -print0 | xargs -0 tar cf directory-structure.tar --no-recursion 

Using -print0 with find will emit filenames as null-terminated strings; with -0 xargs will interpret arguments that same way. Using null as a terminator helps ensure that even filenames with spaces and newlines will be interpreted correctly.

It’s also possible to pipe results straight from find to tar :

$ find backup/ -type d | tar cf directory-structure.tar -T - --no-recursion 

Invoking tar with -T — (or —files-from —) will cause it to read filenames from stdin, expecting each filename to be separated by a line break.

For maximum effect this can be combined with options for null-terminated strings:

$ find . -type d -print0 | tar cf directory-structure.tar --null --files-from - --no-recursion 

Of these I consider this last version to be the most robust, because it supports both unusual filenames and (unlike xargs) is not inherently limited by system command-line sizes. (see xargs —show-limits )

Источник

How to Copy Directory Structure Without Files in Linux

When it comes to copying a directory that contains other directories within directories (subdirectories) and files, implementing the cp command with the -R (recursive) flag can effectively accomplish operation via a simple command syntax like the one stated below.

$ cp -R path/to/source/directory path/to/destination/directory

However, in some scenarios, you might want to mimic a certain directory structure that already exists maybe for your personal projects or for a files storage system just because that directory structure makes perfect sense and might require a lot of time to completely recreate it from scratch.

Читайте также:  Dcp l2500dr драйвер linux

This article will walk us through valid approaches to copying an empty directory structure from already populated directory files in Linux.

Problem Statement

Since the primary objective of this article is to successfully copy a directory structure from an already existing and populated directory (with subdirectories), we need our own populated directory for reference purposes.

Consider the following directory structure previewed by the Linux tree command:

Check Linux Directory Structure

As per the tree command output, the root directory (dir1) has three subdirectories and a total of 10 files. Our aim is to copy this directory structure skeleton to a new destination without the 10 files that already exist.

1. Copy Linux Directory Structure Using tree and xargs Commands

We already know that the tree command mimics a tree-like format while listing a directory’s content. On the other hand, the xargs command takes standard input from a system user or previous command execution output to build and execute command lines.

The actions associated with combining these two commands are as follows:

  • Get all the directory paths from an existing directory structure.
  • Redirect and use the retrieved directory paths as input.
  • Use the mkdir -p command to recreate the retrieved directory paths on a new directory path.

Getting Linux Directory Paths

We have already acknowledged the tree command’s effectiveness in generating a tree-like format of an existing directory structure. However, we can add some flags and command options to ensure that the tree command outputs the directory paths without detailing the directory files.

List Linux Directory Paths

Our final command implementation will look like the following:

$ tree -dfi --noreport dir1 | xargs -I<> mkdir -p "$HOME/Downloads/<>"

Let us now use the tree command again to confirm if the directory structure was copied without files:

Copy Linux Directory Structure

We have three directories and zero files hence our objective is achieved.

2. Copy Linux Directory Structure Using find and xargs Commands

Just like with the tree command, we can use the find command to get the full directory paths on our targeted directory structure.

The -type d option informs find command to only retrieve directories.

View Linux Directories

The above command can then be piped to a xargs command with the destination (e.g $HOME/Documents) for creating the directory structure without files.

$ xargs -I<> mkdir -p "$HOME/Documents/<>"

Our final command will look like the following:

$ find dir1 -type d | xargs -I<> mkdir -p "$HOME/Documents/<>"

Copy Directory Structure in Linux

Confirm the creation of the directory structure:

Confirm Linux Directory Structure

Alternatively, combining the find command with the -exec argument yields the same results:

$ find dir1 -type d -exec mkdir -p "$HOME/Desktop/<>" \;

Know of other cool ways of copying a directory structure without files? Feel free to leave a comment or feedback.

Источник

Оцените статью
Adblock
detector