I've searched and tried myself now for a few hours and just can't seem to quite get through the last step.
What I need to do is search through two layers of subdirectories for all .xml files, and any first layer subdirectory that contains a .xml file is a subdirectory I want to tar - however I can't get the sed command right so I haven't even moved on to figuring out the tar command. Example, if i have the following files
/dir1/subdir1/file.xml
/dir2/subdir2/file.xml
/dir3/file.doc
/dir4/file.xml
/dir5/subdir1/file.doc
I only want to end up tarring dir1, dir2, and dir4; the .xml files could be within 1 or 2 layers deep and i don't want to tar directories that don't have a .xml file.
I think the solution is a combination of find __ | sed ____ | tar, however I can't get the sed command right. So far the find __ | sed ___ command I have is:
find . -maxdepth 3 -type f -name '.xml' | sed 's/*.xml//'
which finds all '.xml' files within the next 2 sub-directories and then prints a list of the path to that file sans the '.xml' extension. This is the closest i've gotten even though what i really need is without the file all together.
Even just helping with the sed command would be helpful! thank you!
Instead of sed
, you can use cut
:
find -maxdepth 3 -type f -name "*.xml" | cut -d / -f 2 | sort -u | xargs tar -cf stuff.tar
The sort -u
is needed so you don't include a directory more than once when it contains more than one XML file.
I suppose find will give you proper list, and you don't need to sed, just using dirname is enough.
Try something like find . -maxdepth 3 -type f -name '*.xml' | xargs dirname
find . -maxdepth 3 -type f -name '*.xml' | xargs dirname
find . -maxdepth 3 -type f -name '*.xml' | xargs dirname
and pipe it again to tar.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.