Create new TAR file in current directory to a new directory without using "mv" (shell script) - linux

I'm currently setting up a backupmanager to automatically archive directories from webserver. I'm searching for an answer how to create a new TAR file of the current directory to a new (different) directory without using mv after the archiving process. See my command:
dcreate=$(date +%Y_%d_%m) tar -cvpzf backup_$dcreate.tar.gz plugins/folder_to_archive/
This command works fine, but i'm struggeling now on how to move it to a new directory directly after the archiv process is terminated, for example:
plugins/plugin_name/ to plugins/backups/
Any help appreciated.
Regards

The -f option of tar is the destination file for the archive; it can be anywhere you want, i.e., you can change
-cvpzf "backup_$dcreate.tar.gz"
to
-cvpzf "plugins/backups/backup_$dcreate.tar.gz"
if you want your new archive to be created in plugins/backups/

Related

Tar command keeps bundling up entire directory path

I have a few sub-directories with files inside each of them in /home/user/archived/myFiles that I'm trying to bundle into a single tar file. The issue is, it keeps bundling a full directory path instead of just everything in the myFiles folder.
When I untar the file, I just want all the bundled sub-directories/files inside to appear in the directory I extracted the file rather than having to go through a series of folders that get created.
Instead, when I currently untar the file, I get a "home" folder and I have to go through /home/user/archived/myFiles to reach all the files.
I tried using the -C flag that I saw suggested online here Tar a directory, but don't store full absolute paths in the archive where you insert parameters for the full directory minus the last folder, and then the name of the last folder which contains all the stuff you want bundled. But the tar command doesn't work as I get a no such file or directory error.
#!/bin/bash
archivedDir="/home/user/archived/myFiles"
tar -czvf "archived-files.tar.gz" "${archivedDir}"/*
rm -vrf "${archivedDir}"/*
# Attempt with -C flag
#tar -cvf "${archivedDir}/archived-files.tar.gz" -C "${archivedDir}" "/*"
So for example, if I did an ls on /home/user/archived/myFiles, and it listed two directories called folderOne and folderTwo, and I ran this bash script and did an ls on /home/user/archived/myFiles again, that directory should only contain archived-files.tar.gz.
If I extracted the tar file, then folderOne and folderTwo would appear.
As I explain already here you should first change to this directory and then create the archive.
So change you script to something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -czvf "../archived-files.tar.gz" *
This will create the archive in upper directory so you will not remove it with the next command.
the extraction should be something like:
archivedDir="/home/user/archived/myFiles"
cd $archivedDir
tar -xzvf "../archived-files.tar.gz"

Creating a flat tar file holding every filename in directory starting with "a"

I am using a command terminal inside a VirtualBox CentOS 7 linux system. I am attempting to create a tar file into a seperate directory that contains all the files in my current directory that start with the letter "a".
I have tried tar -cvf fileName.tar /newDirectory ls a* but I think that I'm doing something wrong. I assume this should only take one line of the command terminal to execute, does anybody know the right way to do it?
The first parameter is the tar file name (full path) and the second is the files you want take. Try it:
tar -cvf newDirectory/fileName.tar a*

Create tar file in parent of working directory

I am trying to create a tar file but not place it in the current working directory but rather the parent of whatever the current working dir is.
I've tried the -C option but with no success.
Did you try like this
$ tar -cvf ../filename.tar ToBeArchivedFolder
Make sure you are using lower case 'c' in the command line options

How can I execute a command anywhere if certain required files are different directories?

Let say the command be my_command
And this command has to be prepared specific files (file1, file2, and file3) in the current working directory.
Because I often use my_command in many different directories, I'd like to keep the certain files in a certain directory and execute my_command without those three files in the working directory.
I mean I don't want to copy those three files to every working directory.
For example:
Directory containing the three files /home/chest
Working directory: /home/wd
If I execute command my_command, it automatically recognizes the three files in /home/chest/
I've thought the way is similar to add $PATH and not the executable files but just files.
It seems like the files needs to be in the current working directory for the vasp_std command to work as expected, I am thinking that you could simply add all files in a include folder in you home directory and then create a symbolic link to this folder from your script. In the end of your script the symbolic link will then be deleted:
#!/bin/bash
# create a symbolic link to our resource folder
ln -s ~/include src
# execute other commands here
# finally remove the symbolic link from the current directory
unlink src
If the vasp_std command require that the files are placed directly under the current working directory you could instead create a symbolic link for each file:
#!/bin/bash
# create link for to all resource files
for file in ~/include/*
do
ln -s $file `basename $file`
done
# execute other commands here
# remove any previously created links
for file in ~/include/*
do
unlink `basename $file`
done

How to rename .tar.gz file without extracting the contents and creating the new .tar.gz file in UBUNTU?

I have a command which will create a new .tar.gz file from the existing one,
sudo tar -zcvf Existing.tar.gz New.tar.gz
this command will create a new New.tar.gz file from the existing Existing.tar.gz file.
Can anyone tell me, is there any way to rename the exiting file without creating the new one?
Thanks.
The easiest is to simply rename ("move") the file:
mv Existing.tar.gz New.tar.gz

Resources