- copy a directory structure with file names without content
- 4 Answers 4
- How to Copy Directory Structure Without Files in Linux
- Problem Statement
- 1. Copy Linux Directory Structure Using tree and xargs Commands
- Getting Linux Directory Paths
- 2. Copy Linux Directory Structure Using find and xargs Commands
- How do I create a copy of a directory in Unix/Linux? [closed]
- 3 Answers 3
- How do I copy a directory tree but not the files in Linux?
- 8 Answers 8
copy a directory structure with file names without content
I have a huge directory structure of movie files. For analysis of that structure I want to copy the entire directory structure, i.e. folders and files however I don’t want to copy all the movie files while I want to keep there file names. Ideally I get zero-byte files with the original movie file name. I tried to and then rsync to my remote machine which didn’t fetch the link files. Any ideas how to do that w/o writing scripts?
4 Answers 4
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/ .
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 ( */ )
It doesn’t copy file data. From manpage of cp
--attributes-only don't copy the file data, just the attributes
Note: I’m not sure this option available for other distributions, if anybody can confirm please update the answer.
I needed an alternative to this to sync only the file structure:
rsync --recursive --times --delete --omit-dir-times --itemize-changes "$src_path/" "$dst_path"
This is how I realized it:
# sync source to destination while IFS= read -r -d '' src_file; do dst_file="$dst_path$" # new files if [[ ! -e "$dst_file" ]]; then if [[ -d "$src_file" ]]; then mkdir -p "$dst_file" elif [[ -f $src_file ]]; then touch -r "$src_file" "$dst_file" else echo "Error: $src_file is not a dir or file" fi echo -n "+ " ls -ld "$src_file" # modification time changed (files only) elif [[ -f $dst_file ]] && [[ $(date -r "$src_file") != $(date -r "$dst_file") ]]; then touch -r "$src_file" "$dst_file" echo -n "+ " ls -ld "$src_file" fi done < <(find "$src_path" -print0) # delete files in destination if they disappeared in source while IFS= read -r -d '' dst_file; do src_file="$src_path$ " # file disappeard on source if [[ ! -e "$src_file" ]]; then delinfo=$(ls -ld "$dst_file") if [[ -d "$dst_file" ]] && rmdir "$dst_file" 2>/dev/null; then echo -n "- $delinfo" elif [[ -f $dst_file ]] && rm "$dst_file"; then echo -n "- $delinfo" fi fi done < <(find "$dst_path" -print0)
As you can see I use echo and ls to display changes.
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.
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:
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.
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:
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.
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/<>"
Confirm the creation of the 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.
How do I create a copy of a directory in Unix/Linux? [closed]
I want to recursively create a copy of a directory and all its contents (e.g. files and subdirectories).
3 Answers 3
The option you're looking for is -R .
cp -R path_to_source path_to_destination/
- If destination doesn't exist, it will be created.
- -R means copy directories recursively . You can also use -r since it's case-insensitive.
- To copy everything inside the source folder (symlinks, hidden files) without copying the source folder itself use -a flag along with trailing /. in the source (as per @muni764 's / @Anton Krug 's comment):
cp -a path_to_source/. path_to_destination/
i wonder why this exact command in dockerfile copies all source directory files into destination, instead of copying just whole directory.
I believe the '/' on the end makes a difference and that might account for your experience. If the source includes the trailing slash it will copy what is in the directory only. If it does not include the trailing slash, it will copy the directory as well and then the contents inside of it. My memory is this behavior varies by command and maybe event by OS a bit. Here's a reference with more info.
I would say if you don't want to include the source and you want to make sure everything is copied (symlinks, hidden files) without copying the source parent folder is to use -ra source/. destination. This will make sure the content of the folder is copied, but not the parent folder itself, which is sometimes handy. And the difference is the /.
Note the importance of "Slash dot" on your source in cp -r src/. dest I know it is mentioned but I still seem to miss it every time.
You are looking for the cp command. You need to change directories so that you are outside of the directory you are trying to copy.
If the directory you're copying is called dir1 and you want to copy it to your /home/Pictures folder:
Linux is case-sensitive and also needs the / after each directory to know that it isn't a file. ~ is a special character in the terminal that automatically evaluates to the current user's home directory. If you need to know what directory you are in, use the command pwd .
When you don't know how to use a Linux command, there is a manual page that you can refer to by typing:
Also, to auto complete long file paths when typing in the terminal, you can hit Tab after you've started typing the path and you will either be presented with choices, or it will insert the remaining part of the path.
There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R , -r , and --recursive are all equivalent, as mentioned in this answer. For portability, i.e. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r . It's important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).
How do I copy a directory tree but not the files in Linux?
I want to copy about 200 directories & subdirectories from one location to another but I don't want to copy the thousands of files within those directories. I am on Linux. Note: I don't have enough space to copy everything then delete all the files.
8 Answers 8
rsync -a -f"+ */" -f"- *" source/ destination/
find some/dir -type d -print0 | rsync --files-from=/dev/stdin -0 .
It would be useful to add what is -0 and what is the ellipsis. The former, I guess, is for rsync to read the output of find's print0 .
Another approach is with find and mkdir:
find SOURCE -type d -exec mkdir TARGET/<> \;
Just make sure TARGET already exists or use the -p option of mkdir.
find inputdir -type d | cpio -pdumv destdir
In the beginning of man cpio it says: "__WARNING__ The cpio utility is considered LEGACY based on POSIX specification. Users are encouraged to use other archiving tools for archive creation."
find some/dir -type d -print | tar --no-recursion -T- -c -p -f- | (cd another/dir && tar -x -p -f-)
You don't really need the -print0 on the find command line or the -0 on the rsync command line unless you have filenames that contain newline characters (which is possible but highly unlikely). Tar (and rsync, and cpio) read filenames line-by-line; using a NULL terminator is mostly useful with xargs , which normally reads whitespace separated filenames (and so does not handle files/directories with spaces in their names without -0 ).