Zipping everything in directory with 7z except one file or one file type - zip

i'd like to zip everything except one file
7z a -tzip files.zip *
this will zip all the files in my current directory.. is there a way I can tell it to not zip one file or one file type ?

Per the 7za command-line help, you use the -x switch to do this:
-x[r[-|0]]]{#listfile|!wildcard}: eXclude filenames
To exclude the file foo.txt you would add:
-x!foo.txt
To exclude all .html files (*.html) you would add:
-x!*.html
You can add multiple -x entries to exclude multiple filenames and/or wildcards in one zip command. Adding the following will exclude foo.txt and *.html:
-x!foo.txt -x!*.html
So with your example, this would add all files to files.zip EXCEPT files named "FILENAME" or that matched the *.extension wildcard:
7z a -tzip files.zip * -x!FILENAME -x!*.extension

If you are using batch script do not forget to escape ! mark.
7z a -xr^^!*.xml "dest_dir.zip" "Source_dir"

Related

How to rename all the files from a directory?

So, i have a given directory, and that directory contains other subdirectories, and every subdirectory can contain a file. I have to change all the files names, so that it will end in ".txt" .
**You can do something like this using bash : and go over all your directories with it **
for f in *.their_current_end;
do
mv -- "$f" "${f%.their_current_end}.txt"
done
another solution would be using : rename in Linux
rename [OPTIONS] perlexpr files
where the rename will rename all the files mentioned like the regex supplied in the perlexpr parameter.
example :
rename 's/.html/.php/' \*.html

How to create zip archive with files with only .txt extension in Linux using terminal

I have directory "Documents" with these files:
file1.txt.
file2.txt
index.html
index.php
script.pl
I want to create zip archive named files.zip with only .txt extension files using terminal. How can I do these?
At the basic level, if you are in the same directory that your files are in, you can do :
zip files.zip *txt
And if you want to zip the files with .txt extention, by giving the absolute path, if they are in Documents directory, which will create files.zip in the current directory you are in:
zip files.zip /the/path/to/Documents/*txt
If you also want this zipped file to be in Documents folder, you should specify it as:
zip /the/path/to/Documents/files.zip /the/path/to/Documents/*txt
An add-on to the existing answer, if you are adding to an existing zip archive, be careful if you have identically named entries in the zip archive & the inpath. From man zip:
Command format. The basic command format is
zip options archive inpath inpath ...
where archive is a new or existing zip archive and inpath is a directory or file path optionally including wildcards. When given the
name of an existing zip archive, zip will replace iden‐
tically named entries in the zip archive (matching the relative names as stored in the archive) or add entries for new names. For
example, if foo.zip exists and contains foo/file1 and
foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:
zip -r foo.zip foo
or more concisely
zip -r foo foo
will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with
foo/file2 unchanged from before.
So if before the zip command is executed foo.zip has:
foo/file1 foo/file2
and directory foo has:
file1 file3
then foo.zip will have:
foo/file1 foo/file2 foo/file3
where foo/file1 is replaced and foo/file3 is new.
Give a try to:
zip only-txt.zip `find . -name "*.txt"`
This will create a zip file named only-txt.zip including all the *.txt files located within the directory you run the command, notice that this will search for *.txt files recursively in all subfolders of the dir

How to zip a folder, but minus the files using zip command line

I am creating a zip file of my application tree, minus folders that have temporary files. For now I exclude the folders using -x option and manually created them with unzip. Is there a way with zip to exclude the files but include the folder (i.e. it would be an empty folder in the zip file?)
I am using
zip -r zipfile.zip . -x appsessions/\* workfolder/\*
but of course it excludes the folders and files in them. I would like to keep appsessions/ and workfolder/ in the zip file, but empty.
Give a try to this:
find . -type d -print | zip name.zip -#
Also check this answer: https://stackoverflow.com/a/13234936/1135424

Rsync, include only certain files types excluding some directories

I want to rsync only certain file types (e.g. .py) and I want to exclude some directories (e.g. venv).
This is what I have tried:
rsync -avz --include='*/' --exclude='venv/' --include='*.py' --exclude='*' /tmp/src/ /tmp/dest/
But it doesn't work.
What am I missing?
With rsync you do not need to use --include="*.py" to include '*.py' files in the copy. The --include option will only include files that have been excluded by --exclude= before. rsync specifies ** as the wildcard specifier. For example, if you want to copy all .py files in the current directory (and subdirectories), but not copy anything from the venc directory, you can do something similar to:
rsync -uav --exclude="venc" **.py destination
(note -a implies -rlptgoD)
which would recursively copy all .py files in present working directory to destination excluding the venc directory.
To recursively copy only *.py files from all directories below the current path excluding any venc directories, you can build a temporary file with the results of find containing the *.py files and exclude files containing venc/ as part of the path, and then transfer all filenames in the temporary file using the --files-from and --no-R (no relative) options to rsync as:
$ find /path/to -type f -name "*.py" | grep -v 'venc/' > tmpfile \
rsync -uav --no-R --files-from=tmpfile / host:/dest/dir \
rm tmpfile
This will capture all *.py files in any subdirectories excluding all directories including the name venc/ and anything below them. The --no-R option is needed to prevent the absolute filenames in tmpfile from be taken as relative to the current working directory.

How to zip all EXCEPT a few, specified directories?

I want to use zip on most of the directories in my home directories but there are a few specific directories (let's call them dir1 and dir2) with contents that I do not want zip to add to my subsequent archive file. I tried
zip foo.zip ./* -x#exclude.lst
with exclude.lst containing:
./dir1/*
./dir2/*
but zip still is including the contents of those directories. How can I have zip skip the contents of those specific directories?
I tried this using just:
dir1/*
in the file and found that dir2/ gets included but dir1/ and things under it don't.

Resources