zip the files and folders inside a parent directory without including the parent directory + Amazon Linux - linux

Is there a Linux command to zip the files and folders inside a parent directory without including the parent directory.
From all the solutions that I tried, the parent directory also is getting zipped

Move into the folder, then zip :
cd my-folder
zip -r ../my-archive.zip *
Dirty, but effective
Or, in a more fancy way, using tar:
tar czf my-archive.tar.gz -C /path/to/my-foder/ ./

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"

How to create a tar archive from an absolute path and package files as if they were relative?

On Linux, I am trying to create a .tar.gz archive from a different directory, that is I have a bash script that will be executed from a different directory. The script will package the folder, I will give the absolute directory of the folder say /home/user1/Documents/folder1 however when it packages the tar file, it puts the entire absolute directory in the archive, whereas I only want the relative one from folder1.
For example:
tar czf /home/user1/Documents/folder1.tar.gz /home/user1/Documents/folder1
This will create an archive but where the first folder will be home and then inside that user1 inside that documents and inside that the folder1, no other subfolders from other branches of course.
Also the console gives this error:
tar: Removing leading `/' from member names
I want it to be packaged as if I would execute the command from the same folder, so the only folder in the archive should be folder1, and inside that it's own subfolders.
So the archive inside should look just as if I would have executed this code from the same directory folder1 is in:
tar czf folder1.tar.gz folder1
You can use the -C option to change the directory before performing any operations:
tar czf /home/user1/Documents/folder1.tar.gz -C /home/user1/Documents folder1
Now, the contents of your archive will look like this:
$ tar tf /home/user1/Documents/folder1.tar.gz
folder1/
folder1/file1
The message you get is not an error, by the way. It's tar telling you that it made the paths in the archive relative so as to avoid overwriting files, which could easily happen when unpacking an archive with absolute paths. You can turn off the leading slash removal with -P, but you often don't want that.

how to tar without the parent directory but with the last directory

I am trying to tar a directory without the parent directories and I have found a solution so far which removes the parent directories and tar all the files under a directory called (DIR) to a DIR.tar.gz file using the following:
tar -cvzf $DIR.tar.gz -C $path/$DIR .
But if I want to untar the tared file, it will untar all the files in the current directory, however, I want to be able to tar the dir in a way which when I use tar -xzvf, it will untar it into folder called DIR. IT should be simple, but I am not able to find the solution on the net. Any hint?
I believe what you want is:
tar -cvzf $DIR.tar.gz -C $path ./$DIR
This should change the directory to your folder path (-C) and only include the relative directory ($DIR) that you're trying to archive.

Create tar.gz archive and add all files and folders without parent folder

For example i have folder "admin" which contains folder "1" and two php files "index.php" and "page.php". I try to use tar -zcvf admin.tar.gz admin and got admin.tar.gz archive. If i open this archive, i can see archive contains "admin" folder and inside this directory is folder "1" and two php files.
I want to create tar.gz archive with all files and folders, but without parent folder. Create archive and it contains only folder "1" and two php files. How i can do it?
You can use the -C option:
tar -C admin -zcvf admin.tar.gz .
See man tar
-C, --directory=DIR
Change to DIR before performing any operations.
This option is order-sensitive, i.e. it affects all options that follow.

Create a .tar.bz2 file Linux

On my Linux machine, I wish to create a .tar.bz2 file of a certain folder. Once I place myself in that folder (in the terminal), what do I type in the terminal command line to place the compressed folder in the home directory of my machine?
Let's say I am in the folder /home/user/folder. In the folder "folder" are several files (txt, .c etc). How do I compress that folder of type .tar.bz2 and place it in my /home directory?
In the /home/user/folder, I've tried sudo tar -cvjSf folder.tar.bz2 but get an error:
tar: Cowardly refusing to create an empty archive
You are not indicating what to include in the archive.
Go one level outside your folder and try:
sudo tar -cvjSf folder.tar.bz2 folder
Or from the same folder try
sudo tar -cvjSf folder.tar.bz2 *
Try this from different folder:
sudo tar -cvjSf folder.tar.bz2 folder/*
tar cvzf file.tar.gz *.c OR tar cvzf file.tar.gz *
for more read this article https://www.geeksforgeeks.org/tar-command-linux-examples/

Resources