asterisk wildcard not working - zip

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.

Related

How to compress files with specified suffix in subdirectory?

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?

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.

Unzipping from a folder of unknown name?

I have a bunch of zip files, and I'm trying to make a bash script to automate the unzipping of certain files from it.
Things is, although I know the name of the file I want, I don't know the name of the folder it's in; it is one folder depth in
How can I extract these files, preferably discarding the folder?
Here's how to unzip any given file at any depth and junk the folder paths on the way out:
unzip -j somezip.zip *somefile.txt
The -j junks any folder structure in the zip file and the asterisk gives a wildcard to match along any path.
if you're in:
some_directory/
and the zip files are in any number of subdirectories, say:
some_directory/foo
find ./ -name myfile.zip -exec unzip {} -d /directory \;
Edit: As for the second part, removing the directory that contained the zip file I assume?
find ./ -name myfile.zip -exec unzip {} -d /directory \; -exec echo rm -rf `dirname {}` \;
Notice the "echo." That's a sanity check. I always echo first when executing something destructive like rm -rf in a loop/iterative sequence like this. Good luck!
Have you tried unzip somefile.zip "*/blah.txt"?
You can use find to find the file that you need to unzip, and xargs to call unzip:
find /path/to/root/ -name 'zipname.zip' -print0 | xargs -0 unzip
print0 enables the command to work with files or paths that have white space in them. -0 is the option to xargs that makes it work with print0.

Resources