7zip CLI whitelist files to add by extension - file-extension

If I'm running the 7z a -tzip myDestFile.zip some/dir command and I want to add to it a file extension whitelist, such that if I put *.txt *.xls *.doc, it will only add files with those extensions to the archive, how would I do that? The documentation does well at whitelisting by a single extension, but what about multiple? Am I just going to have to run the command several times in my script?
I've tried the following:
7za a -tzip test.zip ./subdir *.txt *.xlsx # throws error
7za a -tzip test.zip ./subdir -i!./*.txt -i!./*.xlsx # also results in error
7za a -tzip test.zip ./subdir -i!*.txt -i!*.xlsx # also results in error

This Question shouldn't remain open. The answer that worked for me is below:
7za a -tzip test.zip ./*.txt ./*.xlsx

Related

Zip and Tar compress entire top directory and not (sub)directory

I'm trying to only zip the directory where I am exactly, this is part of a bigger bash script so I need to cd into the directory where I want to extract files and then exit.
However, using either tar or zip, the entire top directory path is recreated and not just the subdirectory that I'm interested in.
I get the following error:
zip warning: name not matched: $PWD/*
What I'm trying to do:
#Sub Directory and contents will be compressed
cd "$Sub_Dir"
Zipped_Files=$(basename "$Sub_Dir")
zip -r "$Zipped_Files".zip "$PWD/*"
#or
zip -j "$Zipped_Files".zip "$PWD/*"
#or
#tar -zcf "$Zipped_Files".zip "$PWD"
echo "Files have been compressed"
You have already cd into the sub dir, zip -r "$Zipped_Files".zip ./* should work.

Linux - How to zip files per subdirectory separately

I have directory structure like this.
From this I want to create different zip files such as
data-A-A_1-A_11.zip
data-A-A_1-A_12.zip
data-B-B_1-B_11.zip
data-C-C_1-C_11.zip
while read line;
do
echo "zip -r ${line//\//-}.zip $line";
# zip -r "${line//\//-}.zip" "$line"
done <<< "$(find data -maxdepth 3 -mindepth 2 -type d)"
Redirect the result of a find command into a while loop. The find command searches the directory data for directories only, searching 3 directories deep only. In the while loop with use bash expansion to convert all forward slashes to "-" and add ".zip" in such a way that we can build a zip command on each directory. Once you are happy that the zip command looks fine when echoed for each directory, comment in the actual zip command

tar exclude is not working inside the bash script

I am trying to create a tar file of a folder, which has a lot of files to be excluded. So I wrote a script (mytar):
#!/usr/bin/env bash
# more files to be included
IGN=""
IGN="$IGN --exclude='notme.txt'"
tar --ignore-failed-read $IGN -cvf "$1" "$2"
# following command is working perfectly
# bash -c "tar --ignore-failed-read $IGN -cvf '$1' '$2'"
Test folder:
test/
notme.txt
test.txt
test2.txt
If I execute the script, it creates a tar file but doesn't exclude the files I have listed in IGN
Apparently, the command is:
tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test
The command is working perfectly fine if it's directly executing in the shell. Also I have found a workaround for the script: using bash -c in script file
bash -c "tar --ignore-failed-read $IGN -cvf '$1' '$2'"
I am wondering and trying to figure out it,
Why this simple command is not working without bash -c?
Why it's working with bash -c?
Output:
First output shouldn't container notme.txt file like later
UPDATE 1 script updated
This has to do with the way bash expands variables in its shell.
When you set:
IGN="--exclude='notme.txt'"
it will be expanded as :
tar --ignore-failed-read '--exclude='\''notme.txt'\''' -cvf test1.tar test
And as such tar will look to exlcude a file named \''notme.txt'\'', which it won't find.
You may use:
IGN=--exclude='notme.txt'
which will be be interpreted correctly after shell expansion and tar will know it, but I would rather suggest you use your variable to only store the file name to be excluded:
IGN="notme.txt"
tar --exclude="$IGN" -cvf ./test1.tar ./*
in following command single quotes are syntactical (not literal, filename argument is not literaly surounded by quotes) to prevent shell for splitting argument in the case it contains a space or a tab
tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test
the closest is to use array instead of string variable :
ign=( --exclude='notme.txt' )
tar --ignore-failed-read "${ign[#]}" -cvf test1.tar test

Staying in another folder, how can i tar specific files from another directory?

Thanks for your support,
I have the following folder structure on my linux laptop
/home
/A
/B
In folder "B", I have files of type *.csv, *.dat.
Now from folder A, How can I create a tar file containing files *.csv in folder B. I am running the command in folder A
Here is the command, I have tried but its not working,
In /home/A folder, I am running the following command
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 *.csv
and also tried with this,
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 --wildcards *.csv
For both of the commands, I get the following error,
tar: *.csv: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
In the tar file, I dont want to include the whole folder structure and this is the reason, I am using option -C (capital)
Moreover, the following command works but it tars all *.csv and *.dat files.
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 .
You can edit the names in the tar command to remove the path. (Assuming that you have GNU tar.)
tar -cf /home/A/Sample1.tar --transform 's,.*/\([^/]*\),\1,' /home/B/ZSBSDP4/*.csv
Note that if you specify more source directories on the command, you could accidentally put more than one file with the same name in the tar file. Then when unpacking, the last one will overwrite those with the same name that precede it.
You can use the --exclude=PATTERN option:
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 . --exclude=*.dat
Other "local file selection" options listed in the man page: http://linux.die.net/man/1/tar

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

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"

Resources