shell - UNIX: copy entire directory structure and only specific file type


How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

3 Answers: 

here is one line command :)

rsync -vapEtogR --progress --stats --log-file="log_file_name.txt" --include '*/' --include '*.txt' --exclude '*' source_path/ destination_path/

Use rsync --help to know about the options used.

cd source-dir
find . -type d -mindepth 1 -exec mkdir target-dir/{} \;

to avoid creating directory "." and error "No such file or directory" respectively.


To create the directory structure:

cd source-dir
find . -type d -exec mkdir target-dir/{} \;

You can ignore the message that . cannot be created.

Then, copy the txt files into it:

find . -type f -name '*.txt' -exec cp {} target-dir/{} \;

More Articles

bash - append text to file using shell from php

I trying to append passed text as parameter to file using shell scriptthis is the code of shell echo $1>>/etc/freeradius/mods-enabled/ldapthis shell will get the text to add it to the file ldap in /etc/freeradius/mods-enabled path , I call this shell in this format # sh /etc/ hello

ubuntu - Shell command to get remote system's ip address from its mac address?

I have the HWAddress of remote machine but its IP address is not static and keeps changing. How do I get its IP address through shell?Relevant questions mostly have the remote system in the same network, but in my case its not.EDIT: I know its IP range.

Get Python command line output when called from a bash script

This may be a super simple question. I am calling a Python script on an Lubuntu OS (Cubietruck) via a shell script. This is to automate the process on start up (I want to maintain this process). Is there a simple way to view the output from Python? At the moment the Python script runs in the backgro

shell - Gmail imap can't access folders with space (or other escapted characters) in the title

Lets say i'm connection to via shell>> openssl s_client -connect -crlf>> a1 login asdasdasdasdGetting the list of the folders:>> a1 list "" *Respond:....* LIST (\HasChildren) "/" "github"* LIST (\HasNoChildren) "/" "Wizz&AKA-Ai

realtime data communication between two c++ executeables one windows and other in virtual machine ubuntu

I am running ros to control a robot using ubuntu. But the drivers are available for windows only.I have setup a virtual machine where the robot controller is built on roscppI want pass these values generated using ros to a cpp in should be realtime. data should be flowing back and forth.d

php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias

shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu

node.js - Not able to run my node app at port 80 on ubuntu 16.04

I want to run my node app at port 80 in ubuntu 16.04. But the moment I'm trying to run my app with npm start its throwing error Port in already in use whereas it's not in use. According to Github Reply, We need root privileges to run on ports below 1048 which is correct.So the moment I'm trying to r

ruby on rails - tmux: are my other terminals still running?

I have started using a certain technique for launching my web code since I switched server setups. I type tmuxsudo startserverrailsCtrl+B then DThen to reboot I type tmuxsudo killall rubysudo startserverrailsCtrl+B then DMy questions are: when I kill the ruby, does that end the session that I starte

php - How to redirect everything after slash to domain before?

This is what I'm trying to do:I want{whatever} redirected to{whatever}. For example, should be redirected to if there is nothing after /car/, then just leave it as that. How can i do that in .htaccess ?