tar: unable to include folder but exclude content - tar

I have a project folder with several directories
- archive
- include
- lib
- src
- src/obj (obj is a subdirectory of src)
I would like tar to pack these directories and their contents into a main.tar, then I will the main.tar into the archive directory.
tar cvz \
--exclude="*.obsolete" --exclude="*DS_Store" --exclude="./archive/*" \
-f main.tar \
./archive ./include ./lib ./src
I would like to exclude the contents of the archive directory but still package the empty directory itself. You can see I am also excluding some other stuff from various places, OSX likes to write .DS_Store files everywhere on my filesystem and I occasionally make copies of files and append .obsolete to the end while working on a new version.
Unfortunately, the empty archive directory is not included in main.tar.
According to this thread, my command should work.
How can the files be excluded from archive but the empty directory be packed into the tar file?

edit
The following fails:
--exclude="./archive/*"
The following works:
--exclude="./archive/*.*"
So the whole command is:
tar cvz \
--exclude="*.obsolete" --exclude="*DS_Store" --exclude="./archive/*.*" \
-f main.tar \
./archive ./include ./lib ./src

Related

Cmake zip folder but remove leading path

I would like to zip folder in my project from CMake. For that I use following code snippet:
ADD_CUSTOM_COMMAND (
TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E tar cvf ${ZIP_OUT_DIR}/my_archive.zip --format=zip -- ${FOLDER_TO_ZIP}/another_folder/
)
The problem with this code is that the files after unzipping contain path component (../../my_file.txt in my case). I tried to use tar cvf -C ${FOLDER_TO_ZIP}/another_folder but unfortunatelly CMake doesn't accept this option.
How can I get rid of leading path from zip archive when using CMake ?
The paths are relative to the working directory. So you just need to specify the WORKING_DIRECTORY:
ADD_CUSTOM_COMMAND(
TARGET ${PROJECT_NAME}
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E tar cvf ${ZIP_OUT_DIR}/my_archive.zip --format=zip -- .
WORKING_DIRECTORY ${FOLDER_TO_ZIP}/another_folder
)

How to use gcovr with source files outside the current/build/run directory?

mkdir -p /tmp/build &&
cd /tmp/build &&
mkdir -p /tmp/src &&
echo "int main(){return 0;}" > /tmp/src/prog.c &&
gcc --coverage -o prog /tmp/src/prog.c &&
./prog &&
gcovr -v -r .
will output an empty report.
Scanning directory . for gcda/gcno files...
Found 2 files (and will process 1)
Processing file: /tmp/build/prog.gcda
Running gcov: 'gcov /tmp/build/prog.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory /tmp/build' in '/tmp/build'
Finding source file corresponding to a gcov data file
currdir /tmp/build
gcov_fname #tmp#src#prog.c.gcov
[' -', ' 0', 'Source', '/tmp/src/prog.c\n']
source_fname /tmp/build/prog.gcda
root /tmp/build
fname /tmp/src/prog.c
Parsing coverage data for file /tmp/src/prog.c
Filtering coverage data for file /tmp/src/prog.c
Gathered coveraged data for 0 files
------------------------------------------------------------------------------
GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
------------------------------------------------------------------------------
TOTAL 0 0 --%
------------------------------------------------------------------------------
However if I manually run
gcov /tmp/build/prog.gcda --branch-counts --branch-probabilities --preserve-paths --object-directory /tmp/build
I get correct results
File '/tmp/src/prog.c'
Lines executed:100.00% of 1
No branches
No calls
Creating '#tmp#src#prog.c.gcov'
It seems that gcovr did not extract the coverage from the otherwise correct gcov output. This only happens if the source file is outside the current directory (same as build directory, same as output directory, same as run directory), and gcc ics called with an absolute path to the source file.
How can I fix this?
Edit
Fixed in upstream gcovr for relative paths, but looks like a bug for absolute paths.
See https://github.com/gcovr/gcovr/issues/169.
What I understood from your code up there is that you made everything and ran the program but you are still inside build directory where the object file resides.
So, what you need to understand is:
gcovr -v -r <path>
this -r flag takes the root directory, which means the parent directory inside which the source and object directory resides. So that it can trace them both and generate coverage data and whatever else you want it to generate.
Try doing that and it will work.
For your understanding:
The .gcno files that gets generated after compilation is just the flowchart kind of data for that particular source file.
Then later when you execute the program, a .gcda file gets generated for each source file. This file contains real coverage data, but for gcovr all these three files are necessary (.gcno, .gcda, sourceFile)
Hope it helped. :)
update:
Back with the work around
You can supply your coverage data location as a pure arg (no option) and point the root to your sources.
gcovr .../path/To/GCDA -r .../path/to/src/ [rest desired flags]
This will solve your problem for sure.
Worked for me in covering my projects.
Gcovr only generates reports for source files within your project. This is intended to exclude coverage from library headers etc.
The question is, which files are in your project? This is determined by the -r root path.
If you are in /tmp/build and root is . aka /tmp/build and the source file is /tmp/src/prog.c, then that source file is clearly outside of your project. In the verbose output, gcovr will report Filtering coverage data for file /tmp/src/prog.c.
If you are in /tmp/build and root is .. aka /tmp and the source file is /tmp/src/prog.c, then that source file is within the project.
If you are in /tmp/build and root is . aka /tmp/build and the source file is ../src/prog.c, then gcovr seems to do something questionable: It joins the file name with the current directory and checks that. So we actually see /tmp/build/../src/prog.c. As far as gcovr is concerned, that's within your project. It seems this behaviour is necessary to include code that is symlinked into a project.
You can disable this “is the source within the project?” filter by providing your own, better filter. For example, you can ask gcovr to only report coverage for sources under /tmp/src:
gcovr -r . -f /tmp/src

tar --excludes option to exclude only the directory at current working directory, not subdirectories

I want to tar/zip a directory ./ (current working directory) and exclude files in the directory ./vendor, I happened to also have a subdirectory named vendor at ./public/web/vendor, but I want to keep that. I've tried:
tar cfz /private/var/folders/temp/mage6BRQWJ.tar.gz --exclude=vendor/* ./
tar cfz /private/var/folders/temp/mage6BRQWJ.tar.gz --exclude=./vendor/* ./
tar cfz /private/var/folders/temp/mage6BRQWJ.tar.gz --exclude="vendor/*" ./
But these both exclude the subdirectory.
I want to use relative path because I want to exclude all .svn (e.g. example) files from all directories, too.
Is there a way, using relative path , to exclude files in the ./vendor directory but not ./public/web/vendor ?
All you need is the --anchored tag:
GNU tar:
tar cfz mage6BRQWJ.tar.gz --anchored --exclude=vendor *
bsdtar:
bsdtar -czf mage6BRQWJ-1.tar.gz --exclude=^vendor *
That works.

Unpack tar.gz folder to part of filename

I have a file dagens_130325.tar.gz containing the folder dagens. In one folder I have hundreds of these daily info. I would like to unpack dagens_130325.tar.gz/dagens to 130325 with all the files inside. Then 130326 etc.
Is there a way to do it?
Not sure this is the right stack where to ask this kind of question, however try with
tar -zxvf dagens_130325.tar.gz -C /tmp/130325 dagens
This way, the folder dagens for the archive dagens_130325.tar.gz is going to be extracted into /tmp/130325. However, note that the target folder must exist, otherwise the command will fail
So, supposedly you have 4 archives in the form dagens_1.tar.gz, dagens_2.tar.gz, ..., you can write an extract.sh file containing
#!/bin/bash
for i in {1..4}
do
mkdir /tmp/$i
FILE="dagens_$i.tar.gz"
tar -zxvf $FILE -C /tmp/$i dagens
done
Having this file the execute permission, being in the same folder as your archives and executing it should produced the result you asked.
This was the solution I came up with in the end
#!/bin/bash
search_dir=/yourdir/with/tar.gz
for entry in "$search_dir"/*.tar.gz
do
substring=$(basename "$entry")
echo $substring
sub2=${substring:7:6}
tar -xvzf $substring
rm -rf $sub2
mv dagens $sub2
done
use
#!/bin/bash
for file in dagens_*.tar.gz
do
from=${file%_*} #removes chars after _
to=${file#*_} #removes chars before _
to=${to%.t*} #removes chars after .t (.tar.gz)
tar -zxf $file --show-transformed --transform "s/$from/$to/"
done

tar exclude single files/directories, not patterns

I'm using tar to make daily backups of a server and want to avoid backup of /proc and /sys system directories, but without excluding any directories named "proc" or "sys" somewhere else in the file tree.
For, example having the following directory tree ("bla" being normal files):
# find
.
./sys
./sys/bla
./foo
./foo/sys
./foo/sys/bla
I would like to exclude ./sys but not ./foo/sys.
I can't seem to find an --exclude pattern that does that...
# tar cvf /dev/null * --exclude=sys
foo/
or...
# tar cvf /dev/null * --exclude=/sys
foo/
foo/sys/
foo/sys/bla
sys/
sys/bla
Any ideas?
(Linux Debian 6)
You can specify absolute paths to the exclude pattern, this way other sys or proc directories will be archived:
tar --exclude=/sys --exclude=/proc /
Using tar you can exclude directories by placing a tag file in any directory that should be skipped.
Create tag files,
touch /sys/.exclude_from_backup
touch /proc/.exclude_from_backup
Then,
tar -czf backup.tar.gz --exclude-tag-all=.exclude_from_backup *
In this case you might want to use:
--anchored --exclude=sys/\*
because in case your tar does not show the leading "/" you have a problem with the filter.

Resources