How to compress files with specified suffix in subdirectory? - linux

For example, current directory is /A/B/, some scripts whose suffixes are .py and .sh in /A/B/C/,/A/B/C/D/ and /A/B/E/.
How to generate such a compressed file which has the structure of directories and contains the python /shell scripts?

Use find with your compression, e.g.:
zip outfile -r `find . -name '*.py'` `find . -name '*.sh'`

find ./someDir -name ".php" -o -name ".html" | tar -cf my_archive -T -
as seen here in a question very similar to yours.
How to tar certain file types in all subdirectories?

Related

In BASH, how do you reference a directory name in a copy statement of files you recursively "find"

I want to recursively find all files that end with *DETAIL.pdf
Create a new directory (in another drive) for each file with the same name as the original directory
Copy the file into the new directory
I have this as my current attempt:
find . -name \*DETAIL.pdf -type f -not -path "./test2" -exec cp -R {} ./test2 \;
I am struggling to create new directories for all these files by referencing the original directory name of each file.
The example mentions using cp but the question/problem itself does not, so I would suggest just using find and tar. Also, though the question is a little ambiguous as noted in the comments above, the example seems to suggest that the desired output directory is a child of the same directory being searched. Given that:
find . -path "./test2" -prune -o -type f -name '*DETAIL.pdf' -print0 | \
tar c --null --files-from=- | \
tar xC test2
This uses find for file selection, generating a (null-separated) file list, which tar then uses to copy the files, and the second tar will create the relative directories as needed and write the copied files.

asterisk wildcard not working

I am trying to zip all PHP files including those in subfolders. But
zip -r PHP.zip *.php
only zips php files under current folder, if I do
zip -r ALL.zip *
it can zip all files including subfolders. So what is wrong with "*.php"?
You can either use,
find -iname '*.php' -print0 | xargs -0 zip -r php.zip
or
zip -r php.zip . --include \*.php
Both of the above commands will do the job.

Gzip only files containing "*foo*" over SSH

In a directory, is it possible to gzip only files containing "foo"? I can find them all by find . -name "*foo*" but i need a way to archive them.
Thank you.
I assume you mean archive into a single tar file? Not individual .gz files?
Try this: (assumes there aren't too many files)
find . -name "*foo*" | xargs tar cvzf archive.tar.gz
An alternative is to do something like:
find . -name "*foo*" > list.txt
tar cvzf archive.tar.gz -T list.txt #(works only with gnu tar, not bsd i think)

Zip together all HTML files under current directory

I am looking to zip together *.html files recursively under the current directory.
My current command is:
zip all-html-files.zip *.html
But this doesn't work recursively. Nor does adding the -r option it seems. Can anybody advise? I want to zip all html files under the current directory, including those underneath subdirectories, but zip the HTML files only, not their file folders.
Thanks!
What about this?
find /your/path/ -type f -name "*.html" | xargs zip all_html_files.zip
looks for all .html files under the directory /your/path (change it for yours). Then, pipes the result to xargs, which creates the zip file.
To junk the paths, add -j option:
find /your/path/ -type f -name "*.html" | xargs zip -j all_html_files.zip
find . -name "*.html" -print | zip all-html-files.zip -#
Try
find . -type f -name "*.html" | xargs zip all-html-files
You can also say
find . -type f -name "*.html" | zip all-html-files -#
If you do not want to preserve the directory structure, specify the -j option:
find . -type f -name "*.html" | zip -j all-html-files -#
man zip says:
-# file lists. If a file list is specified as -# [Not on MacOS], zip
takes the list of input files from standard input instead of from the
command line. For example,
zip -# foo
will store the files listed one per line on stdin in foo.zip.
Under Unix, this option can be used to powerful effect in conjunction
with the find (1) command. For example, to archive all the C source
files in the current directory and its subdirectories:
find . -name "*.[ch]" -print | zip source -#
(note that the pattern must be quoted to keep the shell from expanding
it).
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).

I need to delete all non php files from a linux server or download them

I have thousands of files, maybe hundreds of thousands of files on a Linux Server and they are in directories, and sub directories -
The files are all located in /home/sas/httpdocs -
I want to get a copy of the entire directory with just the php files, but preserving the same directory structure -
I have two options:
either remove ALL of the non php files, then tarball it up and download it -
Or simply extract only all of the php files in a new directory but keeping the same directory structure -
Any ideas on how to do this?
Sas
This will copy only php files into a separate dir
cd /home/sas/httpdocs
tar -cf - `find . -name "*.php" -print` | ( cd /destination_dir && tar xBf - )
Ther is another method of deleting non-php files. Here is it, detailed elegantly
https://superuser.com/questions/168130/unix-delete-files-and-folders-excluding-pattern
Using rsync could be an option:
rsync -av --include "*/" --include "*.php" --exclude "*" /home/sas/httpdocs/. /copy/dir/
To delete the file not ending by .php:
find /dir -type f ! -name "*.php" -print
When you are happy with the output, replace the -print by -delete.

Resources