linux command to copy contents of folder to root - linux

I am using ubuntu I want to copy folder contents into another folder . I used below command
cp -R /home/user/public_html/domain.com /home/user/public_html/
But I get source and destinations are same error.

I would use tar (because it block copies). Something like
cd /home/user/public_html/domain.com
tar cf - * | (cd .. ; tar xvf -)
or (using your cp) like
cd /home/user/public_html/domain.com
cp -R * ../

Related

How to jump the unpack directory in shell script?

I want to unpack a .tgz file and jump to the directory released. I know the command in terminal is:
tar -xvzf xxx.tgz
and then jump to the direcotory:
cd xxx
But how can I do this in shell script? I don't know how to get the directory the 'tar' command released, can anyone help me?
As mentioned here:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
The following would unarchive xxx.tgz and cd into whichever directory it extracts into:
tar -xvzf xxx.tgz && cd $(tar -tf ${_} | head -1 | cut -d / -f 1)
This takes that into account that the directory which the archive is extracted to might not be the same name as the archive. The method extracts the archive then checks it's contents in order to determine where it actually extracts to. There is some level of fragility to be aware of and adjustments might need to take place for unusual filenames.
Here are a couple of other (slightly more generic) examples:
mkdir -p xxx && tar -xvzf ${_}.tgz -C ${_%%.tgz}
Would create the directory xxx and extract xxx.tgz in xxx even if it normally extracts to xyz.
tar -xvzf xxx.tgz && cd ${_%%.tgz}
Unarchives xxx.tgz then uses bash parameter substitution to cd into the xxx directory.

Extract tar file after copy without success

Im copy tar file form folder A to folder B and I want that when
it finish to copy it to unzip it, I try with the following in my shell script which doesnt work,any idea?
cp /home/i557956/A/file.tar /home/i557956/B
tar -xvf /home/i557956/B/file.tar
The copy was success but the tar is not extracted in B folder...
Try to move into the B folder before extracting:
cp /home/i557956/A/file.tar /home/i557956/B
(cd /home/i557956/B/ && tar -xvf file.tar)

What is the linux command to move the files of subdirecties into one level up respectively

The path structure of the files on my server is similar to shown below,
/home/sun/sdir1/mp4/file.mp4
/home/sun/collection/sdir2/mp4/file.mp4
I would like to move the files of "mp4" into one level up(into sdir1 and sdir2 respectively)
So the output should be,
/home/sun/sdir1/file.mp4
/home/sun/collection/sdir2/file.mp4
I have no idea to do this, so not tried yet anything...
There are different ways to solve your problem
If you just want to move those specific files, run these commands:
cd /home/sun/
mv sdir1/mp4/file.mp4 sdir1/
mv sdir2/mp4/file.mp4 sdir2/
If you want to move all mp4 files on those directories (sdir1 and sdir2), run these commands:
cd /home/sun/
mv sdir1/mp4/*.mp4 sdir1/
mv sdir2/mp4/*.mp4 sdir2/
Edit:
Make a script that iterates all the directories:
Create a script and name it and edit it with your favorite editor (nano, vim, gedit, ...):
gedit folderIterator.sh
The script file content is:
#/bin/bash
# Go to the desired directory
cd /home/sun/
# Do an action over all the subdirectories in the folder
for dir in /home/sun/*/
do
dir=${dir%*/}
mv "$dir"/mp4/*.mp4 "$dir"/
# If you want to remove the subdirectory after moving the files, uncomment the following line
# rm -rf "$dir"
done
Save the file and give it execute permissions:
chmod +x folderIterator.sh
And execute it:
./folderIterator.sh
You can do this:
# move all .mp4 files from sdir1/mp4 to sdir1 directory
user#host:~/home/sun$ mv sdir1/mp4/*.mp4 sdir/
# move all .mp4 files from collection/sdir2/mp4 to collection/sdir2 directory
user#host:~/home/sun$ mv collection/sdir2/mp4/*.mp4 collection/sdir2/
# move only 1 file
user#host:~/home/sun$ mv sdir1/mp4/file.mp4 sdir/
user#host:~/home/sun$ mv collection/sdir2/mp4/file.mp4 collection/sdir2/
I suggest you use find and something like
cd /home/sun/sdir1/mp4/
find . -name "*" -exec mv {} /home/sun/sdir1/ \;
cd /home/sun/collection/sdir2/mp4/
find . -name "*" -exec mv {} /home/sun/collection/sdir2/ \;
Alternatively, you could use tar and something like
cd /home/sun/sdir1/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
cd /home/sun/collection/sdir2/mp4/
tar cfp - * | (cd ../ ; tar xvvf -)
# Make sure everything looks good
rm -rf mp4
The command to move a file (or directory) up one level is:
mv /home/sun/sdir1/mp4/file.mp4 ..
Wildcards can be used to select more files & directories, you can also provide more than one directory at a time.
mv /home/sun/sdir1/mp4/*.mp4 /home/sun/collection/sdir2/mp4/*.mp4 ..

linux shell tar unwanted extra directories

I have the following problem:
I have directorties a/b/c and inside c many text files.
I want to make a .tar.gz file in drectory a/mydir with the c directory inside and then unzip it to that same directory to create a/mydir/c (with all the files inside)
I am at directory a and run: (shell)
~:$ tar -czf mydir/output.tar.gz b/c
~:$ tar -zxf mydir/output.tar.gz -c mydir
but the result is directories a/mydir/b/c (with the files inside)
The problem is I don't want directory b in the middle, just c with all its contents
This works for me. Create data
mkdir -p a/b/c
echo 42 > a/b/c/file.dat
Archive
tar zc -f c.tar.gz -C a/b c
created a/b/c directories, from directory a kindly try this command.
so the file under b/c/files were done out.tar.gz
new directory "mydir" create under "b" and files extracted too.
out.tar.gz removed from "a".
# tar -cvzf out.tar.gz b/c/* ; mkdir -p b/mydir ; tar -xvzf out.tar.gz -C b/mydir/ ; rm -rf out.tar.gz
Thanks!

How do I tar a directory without retaining the directory structure?

I'm working on a backup script and want to tar up a file directory:
tar czf ~/backup.tgz /home/username/drupal/sites/default/files
This tars it up, but when I untar the resulting file, it includes the full file structure: the files are in home/username/drupal/sites/default/files.
Is there a way to exclude the parent directories, so that the resulting tar just knows about the last directory (files)?
Use the --directory option:
tar czf ~/backup.tgz --directory=/home/username/drupal/sites/default files
Hi I've a better solution when enter in the specified directory it's impossible (Makefiles,etc)
tar -cjvf files.tar.bz2 -C directory/contents/to/be/compressed .
Do not forget the dot (.) at the end !!
cd /home/username/drupal/sites/default/files
tar czf ~/backup.tgz *
Create a tar archive
tar czf $sourcedir/$backup_dir.tar --directory=$sourcedir WEB-INF en
Un-tar files on a local machine
tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
Upload to a server
scp -r -i $privatekey $sourcedir/$backup_dir.tar $server:$deploydir/med365/
echo "File uploaded.. deployment folders"
Un-tar on server
ssh -i $privatekey $server tar -xvf $deploydir/med365/$backup_dir.tar -C $deploydir/med365/
To gunzip all txt (*.txt) files from /home/myuser/workspace/zip_from/
to /home/myuser/workspace/zip_to/ without directory structure of source files use following command:
tar -P -cvzf /home/myuser/workspace/zip_to/mydoc.tar.gz --directory="/home/myuser/workspace/zip_from/" *.txt
If you want to tar files while keeping the structure but ignore it partially or completely when extracting, use the --strip-components argument when extracting.
In this case, where the full path is /home/username/drupal/sites/default/files, the following command would extract the tar.gz content without the full parent directory structure, keeping only the last directory of the path (e.g. files/file1).
tar -xzv --strip-components=5 -f backup.tgz
I've found this tip on https://www.baeldung.com/linux/tar-archive-without-directory-structure#5-using-the---strip-components-option.
To build on nbt's and MaikoID's solutions:
tar -czf destination.tar.gz -C source/directory $(ls source/directory)
This solution:
Includes all files and folders in the directory
Does not include any of the directory structure (or .) in the final product
Does not require you to change directories.
However, it requires the directory to be given twice, so it may be most useful in another script. It may also be less efficient if there are a lot of files/folders in source/directory. Adjust the subcommand as necessary.
So for instance for the following structure:
|- source
| |- one
| `- two
`- working
the following command:
working$ tar -czf destination.tar.gz -C ../source $(ls ../source)
will produce destination.tar.gz where both one and two (and sub-files/-folders) are the first items.
This worked for me:
gzip -dc "<your_file>.tgz" | tar x -C <location>
For me -C or --directory did not work, I use this
cd source/directory/or/file
tar -cvzf destination/packaged-app.tgz *.jar
# this will put your current directory to what it previously was
cd -
Kindly use the below command to generate tar file without directory structure
tar -C <directoryPath> -cvzf <Path of the tar.gz file> filename1 filename2... filename N
eg:
tar -C /home/project/files -cvzf /home/project/files/test.tar.gz text1.txt text2.txt
tar -Cczf ~/backup.tgz /home/username/drupal/sites/default/files
-C does the cd for you

Resources