Zip together all HTML files under current directory - linux

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).

Related

Create ZIP of hundred thousand files based on date newer than one year on Linux

I have a /folder with over a half million files created in the last 10 years. I'm restructuring the process so that in the future there are subfolders based on the year.
For now, I need to backup all files modified within the last year. I tried
zip -r /backup.zip $(find /folder -type f -mtime -365
but get error: Argument list too long.
Is there any alternative to get the files compressed and archived?
Zip has an option to read the filelist from stdin. Below is from the zip man page
-# 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.
This should do what you need
find /folder -type f -mtime -365 | zip -# /backup.zip
Note that I've removed the -r option because it isn't doing anything - you are explicitly selecting standard files with the find command (-type f)
You'll have to switch from passing all the files at once to piping the files one at a time to the zip command.
find /folder -type f -mtime -365 | while read FILE;do zip -r /backup.zip $FILE;done
You can also work with the -exec parameter in find, like this:
find /folder -type f -mtime -365 -exec zip -r /backup.zip \;
(or whatever your command is). For every file, the given command is executed with the file passed as a last parameter.
Find the files and then execute the zip command on as many files as possible using + as opposed to ;
find /folder -type f -mtime -365 -exec zip -r /backup.zip '{}' +

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.

How to zip a directory in linux excluding a single file?

I would like to zip a directory and I am able to do so with
zip -r zip_file_name directory
however, I would like exclude a single file in the directory from the zip file. How would I go about doing this?
Enter the directory which you want to zip. Then:
find . -not -name "file_to_exclude" | zip zip_file_name -#
The command above will create zip_file_name.zip in directory itself.
To create zip at a particular path, Enter the directory which you want to zip. Then:
find . -not -name "file_to_exclude" | zip ~/ParticularPath/zip_file_name -#
From linux man page for zip:
-# 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 command. For example, to archive all the C source files in the current directory and its subdirectories:
find . -name "*.[ch]" -print | zip source -#

how can i search for files and zip them in one zip file

I tried to search files and zip them with the following commmand
find . regexpression -exec zip {} \;
however it is not working. How can i do this?
The command you use will run zip on each file separately, try this:
find . -name <name> -print | zip newZipFile.zip -#
The -# tells zip to read files from the input. From man zip(1),
-# 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.
Your response is close, but this might work better:
find -regex 'regex' -exec zip filname.zip {} +
That will put all the matching files in one zip file called filename.zip. You don't have to worry about special characters in the filename (like a line break), which you would if you piped the results.
You can also provide the names as the result of your find command:
zip name.zip `find . -name <name> -print`
This is a feature of the shell you are using. You can search for "backticks" to determine how your shell handles this.

List all files in a directory with abosolute paths (excluding directories)

I've currently got:
ls -1 $(pwd)/*
Gives me all the files in a directory with absolute paths - but formats it with the directory at the start of each list of files.
Is there a way just to get a list of files in a directory recursively (absolute paths) - excluding the directory/sub-directories themselves?
find $(pwd) -type f -print
or
find $(pwd) -type f -ls
If you are feeding it into something else, you might want -print0 (to handle filenames with spaces).
E.g.: find . -type f -print0 | xargs --null --no-run-if-empty grep

Resources