Some time in the past, we discovered easy methods to copy sure sort of recordsdata from one listing to a different in Linux. As we speak we’re going to do the identical however protect the listing construction as nicely. This temporary tutorial explains easy methods to copy particular file sorts whereas holding the listing construction in Linux. Right here I’ve given two alternative ways to do that. Simply choose anybody that works for you.
Copy Particular File Varieties Whereas Protecting Listing Construction In Linux
Image this situation.
I’ve a listing named “Linux” with completely different sort of recordsdata saved in several sub-directories. Take a look on the following listing construction:
$ tree Linux/
│ ├── English
│ │ └── Kina – Can We Kiss Without end.mp3
│ ├── Instrumental
│ │ └── Chill Research Beats.mp3
│ └── Tamil
│ ├── Kannan Vanthu.mp3
│ └── yarenna.mp3
│ ├── file.docx
│ └── Raja Raja Chozan Naan.mp3
│ ├── Bamboo Flute – Meditation – Therapeutic – Sleep – Zen.mp3
│ └── pic.jpg
7 directories, 10 recordsdata
As you see within the above listing construction, the Linux listing has 4 sub-directories specifically dir1, dir2, dir3 and dir4. The mp3 recordsdata are scattered throughout all 4 sub-directories. Now, I need to copy all mp3 recordsdata to a different listing named “ostechnix” and likewise I need to preserve the identical listing construction within the goal listing.
First we are going to see how to do that utilizing “discover” command.
Technique 1 – Copy particular file sorts whereas preserving listing construction utilizing “discover” and “cp” or “cpio” instructions
Go to the supply listing:
$ cd Linux/
And replica all mp3 fie sorts utilizing “discover” command:
$ discover . -name ‘*.mp3’ -exec cp –parents ~/ostechnix ;
Allow us to break down the above command and see what every possibility does.
discover – command to search out recordsdata and folders in Unix-like methods.
the dot (.) – represents we copy the contents from present listing.
-iname ‘*.mp3’ – seek for recordsdata matching with extension .mp3.
-exec cp – execute the ‘cp’ command to repeat recordsdata from supply to vacation spot listing.
–dad and mom – create the intermediate mum or dad directories if wanted to protect the mum or dad listing construction.
– is mechanically changed with the file title of the recordsdata discovered by ‘discover’ command. And the braces are escaped
~/ostechnix – goal listing to save lots of the matching recordsdata.
; – signifies it that the instructions to be executed are actually full, and to hold out the command once more on the following match.
This command will discover and replica all mp3 sort recordsdata from ~/Linux listing to ~/ostechnix listing. And in addition it preserves the identical listing construction within the goal listing.
You possibly can confirm it utilizing “tree” command at each places like under.
As you see within the above output, the vacation spot listing solely has the mp3 recordsdata and its listing construction is identical as supply listing.
In case you are doing this from another location, specify the total path of supply listing like under.
$ discover ~/Linux -name ‘*.mp3’ -exec cp –parents ~/ostechnix ;
This command will discover the entire file in Linux/
If –dad and mom possibility doesn’t work, you’ll be able to mix discover command with cpio command to repeat recordsdata holding listing construction.
$ discover . -name ‘*.mp3’ | cpio -pdm ~/ostechnix
cpio – Command to repeat recordsdata to and from archives.
-p – Learn a listing of file names from the usual enter and replica them to the required listing.
-d – Create directories the place wanted.
-m – Protect file modification time.
For extra particulars, refer man pages.
$ man discover
$ man cp
$ man cpio
Technique 2 – Copy particular file sorts whereas preserving listing construction utilizing Rsync
Rsync is a strong instrument used to/from native and distant methods. To repeat sure sort of recordsdata from one listing to a different whereas holding the mum or dad listing construction, run:
$ rsync -a -m –include ‘*/’ –include ‘*.mp3’ –exclude ‘*’ ~/Linux/ ~/ostechnix
$ rsync -a –prune-empty-dirs –include ‘*/’ –include ‘*.mp3’ –exclude ‘*’ ~/Linux/ ~/ostechnix
rsync – distant (and native) file-copying instrument.
-a – archive mode to protect virtually every little thing (together with symlinks, modification dates, file permissions, homeowners and so on.)
-m, –prune-empty-dirs – prune empty directories from supply tree. If you wish to embrace empty directories, simply take away this selection from the above command.
–embrace=”*/” –embrace=”*.mp3″ –exclude=”*” – To incorporate solely particular recordsdata, you first want to incorporate these particular recordsdata, then exclude all different recordsdata. In our case, we have now included *.mp3 recordsdata and exclude every little thing else.
~/Linux – Supply listing.
~/ostechnix – Vacation spot listing.
For extra particulars, refer man pages.
$ man rsync
Hope this helps.
Thanks for stopping by!
Assist us that can assist you:
Have a Good day!!