2

I have a Tar file: testwebsite.tar

I've placed it in the directory I would like to extract its contents to on my Web server which is mytestdirectory

I run this command via PuTTY: tar -xvf testwebsite.tar

Which results in the content being extracted but in this format:

mytestdirectory/srv/test/www.testwebsite.com/

All of the files I would like have appear in mytestdirectory are contained in the www.testwebsite.com sub directory.

Is there a way I can move all of the content within this sub directory to the parent directory using the mv command?

I was looking around and tried a few answers, one being to run this command from within the sub directory:

mv * .[^.]* ..

But that resulted in this message being shown:

mv: cannot stat `*': No such file or directory
mv: cannot stat `.[^.]*': No such file or directory

Could someone please help me on this issue, I'm basically trying to move all of the content (including hidden files) from the sub directory www.testwebsite.com to the parent directory mytestdirectory.

3

You need to use the --strip-components option of tar; that's because the paths you don't need are contained in the tar archive.

So for instance if the tar contains this: srv/test/www.testwebsite.com/index.html and you want to obtain this mytestdirectory/index.html, you need

$ cd /path/to/mytestdirectory
$ tar xf testwebsite.tar --strip-components=3 

If you use the verbose command, you will still set the full original filenames, so you can also add the --show-transformed argument to list the modified paths:

$ tar tfz workspace.tar.gz --strip-components=2 workspace/project
-rw-rw-r-- guido/guido   11134 2009-01-22 23:21 workspace/project/aaa
-rw-rw-r-- guido/guido   11134 2009-01-22 23:21 workspace/project/bbb
[... list continues ...]
$ tar tfz workspace.tar.gz --strip-components=2 --show-transformed workspace/project
-rw-rw-r-- guido/guido   11134 2009-01-22 23:21 aaa
-rw-rw-r-- guido/guido   11134 2009-01-22 23:21 bbb
[ ... and so on ...]

To fix your situation with mv, that would be:

# cd /path/to/mytestdirectory 
# mv srv/test/www.testwebsite.com/* .

For taking care of hidden files, one soution will be this:

# shopt -s dotglob

executed before the above commands, to the glob also match dotfiles; or even better, delete your current target directory, then move and rename the one you want to copy:

[ delete or rename current /path/to/mytestdirectory ]
# cd /path/to
# mv srv/test/www.testwebsite.com/ mytestdirectory/
# rmdir srv/test/ srv/
  • Your answer is very helpful, but when I try the above command $ tar xf testwebsite.tar --strip-components=4 I get a whole load of images being placed in the root directory as opposed to all of the files? – Javacadabra Mar 13 '15 at 12:43
  • 1
    the 4 in the answer was just en example, it depends on how many path components you need to strip away: I guess in your tar archive all file paths start with srv/test/www.testwebsite.com/, if that's the case you should execute the tar command inside mytestdirectory and use --strip-components=3 – guido Mar 13 '15 at 12:48
  • THANK YOU SO MUCH IT WORKED! You've saved me so much pain and headaches. I'm a novice when it comes to Linux and get by with just about as much as I can. This answer was so so helpful – Javacadabra Mar 13 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.