symlink in particular directory using rpm spec - symlink

rpm created using spec file will create directory "directory1" and all files in /var/lib/directory1.
For another use case i want to create another directory in "/var/lib" which should be a symlink to directory1.
eg:
cd /var/lib/
ls -la
directory2 -> directory1
directory1
how is it possible to do achieve this without using absolute paths in spec file?

%install
mkdir -p %{buildroot}/%{_sharedstatedir}/directory1
ln -s directory1 %{buildroot}/%{_sharedstatedir}/directory2
%files
%{_sharedstatedir}/directory1
%{_sharedstatedir}/directory2

Related

Unix command CP to copy a file to multiple directories

I have folder structure like this:
/home/
/folder1/
/backup/
/folder2/
/backup/
/folder3/
/folder4/
/backup/
/folder5/
(As you can see, no all directories "folder" have a directory "backup")
I need to copy the script "checker.php" to all "backup" directories only.
"checker.php" is at:
/home/checker.php
I am using this command:
cp /home/checker.php /home/*/backup/checker.php
But it is not working. Please help.
The cp command doesn't allow multiple destination directories.
A way forward is to loop through the folders:
for d in /home/*/backup; do
cp /home/checker.php "$d"
done

tar one folder to an other directory

I'm trying to create a bash script that creates an archive of one folder (or the folder content) to a specific directory.
My version works but instead to archive one folder it archives me the whole path which I don't want.
Is there a way to solve this problem without using cd? I saw some solutions using -C but I get the following error, no matter where I place it: refusing to create an empty archive
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names $SRCDIR
You can try this:
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR --absolute-names -C $(dirname $SRCDIR) $(basename $SRCDIR)
As you can see in my example I'm using -C option to change the directory in combination with dirname and basename for source directory
OTHER OPTIONS
-C, --directory DIR
change to directory DIR
Usually, when the '-C' is being used, the wild-card expansion does not work well - as it will try to expand the wildcard ('*' in this case) at the current directory where the command is invoked, and NOT at the SRCDIR location.
If you want all files to be included in the repository, use the '.' (it will also include hidden files!).
SRCDIR=~/Documents/sub1/sub2/sub3/source/*
DESTDIR=~/Documents/sub1/sub2/sub3/target/backup.tgz
tar czf $DESTDIR -C "$(dirname $SRCDIR)" .
As a side note, the original post is using '--absolute-names'. You might want to reconsider using this flag, as it make it extremely difficult to restore the files into any other location but the original location of the files.

Exclude current directory from tar

I'm trying to exclude the current directory from the tarball without excluding its contents, because when I extract it out using the -k flag I get an exit status of 1 and a message
./: Already exists
tar: Error exit delayed from previous errors.
How do I do this? I've tried the --exclude flag but that excludes the contents also (rightly so). I'm trying to code this for both the OSX/BSD and GNU versions of tar.
Test case:
# Setup
mkdir /tmp/stackoverflow
cd /tmp/stackoverflow
mkdir dir
touch dir/file
# Create
tar cCf dir dir.tar .
# List contents
tar tf dir.tar
gives
./
./file
showing that the current directory ./ is in the tar. This would be fine, but when I do the following:
mkdir dir2
tar xkfC dir.tar dir2
due to the -k flag, I get an exit code of 1 and the message
./: Already exists
tar: Error exit delayed from previous errors.
To exclude the current directory you can create your archive on this way:
tar cf /path/to/dir.tar ./*
use ./*instead of ., this will not match current directory (.) and therefore not include in the archive
This does the trick:
GLOBIGNORE=".:.."
cd dir
tar cf ../dir.tar *
The extra cd is to replace the use of the -C flag, so that we can use a glob. The GLOBIGNORE ignores the current directory, but also sets shopt -s dotglob implicitly to include hidden files. Using * instead of ./* means that the tar file listing doesn't list ./file, but instead lists it as file. The entire listing of the tar is:
file

Create tar.gz file for directory but with different name

I can create a file file.tar.gz file from a directory directory using
tar -zcvf file.tar.gz directory
Unpacking it using
tar xzf file.tar.gz
recreates the directory directory. But how to create a file.tar.gz from the directory directory that creates directory-foo when unpacking with the same command (the unpack command needs to be kept)? Renaming the directory directory to directory-foo before packing should be avoided as well as duplicating the directory.
When trying the suggested
OLDNAME=directory
NEWNAME=directory-foo
tar --transform='s,$OLDNAME/,$NEWNAME/,' -x -f file.tar.gz
I'm getting
tar: Option --transform=s,$OLDNAME/,$NEWNAME/, is not supported
Usage:
...
Tar admits a sed expression to modify file names. You may use --transform or --xform.

Command line zip everything within a directory, but do not include any directory as the root

I can't find the answer to this for the life of me. Because I am packaging a zip in a specific way for a build process, I don't want to include a folder at all in the resulting zip at the root. For example, if I have this file path:
MyFolder/
A.png
B.txt
C.mp3
And I use either the command:
zip -r -X "MyFolder.zip" MyFolder/*
or
cd MyFolder; zip -r -X "../MyFolder.zip" *
I end up with a zip file that has the root element of MyFolder. What I want is for when I unzip it is to dump all of it right into the directory, like this:
A.png
B.txt
C.mp3
In other words, I don't want MyFolder or any other folder as the root. I read through the whole manual and have tried numerous options and a lot of Google searching, and zip seems to just really want to have a folder at the root.
Thanks!
It was Archive Utility's fault (a Mac OS X unzipper app). When I used the unzip command from the command line, it works great.
(cd MyFolder && zip -r -X "../MyFolder.zip" .)
Stumbled across this answer but didnt want to have to change in out of directories. I found the -j option useful which adds all files to the root of the zip. Note that its is all files so subdirectory structure will not be preserved.
So with this folder structure:
MyFolder
- MyFile1
- MySubFolder
- MyFile2
And this command:
zip -rj MyFolder.zip MyFolder
You get this:
MyFolder.zip
- MyFile1
- MyFile2
I found the easier way to make an encrypted zip file with the terminal app on mac (mac os) just from the files of your folder.
The command for the terminal
zip -j -e wishedname.zip yourfolder/*
That's it. Enjoy!
*
For more information to zip command in the terminal app
man zip
What -j and -e do?
-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).
-e
--encrypt
Encrypt the contents of the zip archive using a password which is entered on the terminal in response to a prompt (this will not be echoed; if standard error is not a tty, zip will exit with an error). The password prompt is repeated to save the user from typing errors.
Install zip
sudo apt install zip
use zip
zip -r foo.zip .
You can use the flags -0 (none) to -9 (best) to change compressionrate
Excluding files can be done via the -x flag. From the man-page:
-x files
--exclude files
Explicitly exclude the specified files, as in:
zip -r foo foo -x \*.o
which will include the contents of foo in foo.zip while excluding all the files that end in .o. The backslash avoids the shell filename substitution, so that the name matching
is performed by zip at all directory levels.
Also possible:
zip -r foo foo -x#exclude.lst
which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.
The long option forms of the above are
zip -r foo foo --exclude \*.o
and
zip -r foo foo --exclude #exclude.lst
Multiple patterns can be specified, as in:
zip -r foo foo -x \*.o \*.c
If there is no space between -x and the pattern, just one value is assumed (no list):
zip -r foo foo -x\*.o
See -i for more on include and exclude.

Resources