LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2020, 12:07 PM   #16
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

Original Poster
Rep: Reputation: Disabled

Nevermind, i copy the files and directories one by one. I guess there is no simple solution for what i am looking for.
 
Old 02-09-2020, 12:33 PM   #17
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,620

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by peter7089 View Post
I guess there is no simple solution for what i am looking for.
Sure there is - first you need to fix your text file to only contain the absolute file paths you want (i.e. no directories, no "package diverts others to: /usr/bin/firefox.real", etc).

Something like (there's probably a simpler way than this, and you'll want to verify it does the right thing):
Code:
dpkg -L firefox-esr | while IFS= read -r line; do test -f "$line" && echo "$line" >> ~/firefox-files.txt || echo "$line" >> ~/firefox-skipped.txt ;done

(UPDATE: ignore the rest and use the cleaner version crts wrote below)

Then you can just do:
Code:
tar -cvf ~/firefox-files.tar $(cat ~/firefox-files.txt)
And you will collect all the files (and their paths) into a single file, which can then be extracted with:

Code:
cd ~/firefox1
tar -xvf ~/firefox-files.tar
And you'll get the directory structure preserved.

(And for the benefit of anyone transferring between different machines, you can add -z to both tar commands to compress the tar file into a tgz (.tar.gz) which will transfer quicker.)


Last edited by boughtonp; 02-09-2020 at 12:52 PM.
 
Old 02-09-2020, 12:36 PM   #18
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,343
Blog Entries: 3

Rep: Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754
Quote:
Originally Posted by peter7089 View Post
I guess there is no simple solution for what i am looking for.
There have been several offered. :/
 
Old 02-09-2020, 12:45 PM   #19
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by peter7089 View Post
Nevermind, i copy the files and directories one by one. I guess there is no simple solution for what i am looking for.
Actually, there is:
Code:
tar c -T /path/to/filelist | tar x -C /path/to/targetdirectory
The first tar creates an archive and strips the leading '/' from the filenames. The second tar extracts the archive into the destination directory and preserves the directory structure.

Edit:

The main problem is that technically you do not have a filelist because it also contains directories and an "invalid" entry. So you will need to filter that list:
Code:
find $(</path/to/filelist) -maxdepth 0 -type f | tar c -T - | tar x -C /path/to/targetdirectory
or maybe (?)

Code:
find $(dpkg -L firefox) -maxdepth 0 -type f | tar c -T - | tar x -C /path/to/targetdirectory

Last edited by crts; 02-09-2020 at 01:00 PM. Reason: filter for files only
 
2 members found this post helpful.
Old 02-09-2020, 12:58 PM   #20
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Sorry the various solutions proposed do not suit your needs

I agree with others that there are viable solutions discussed and presented.
 
Old 02-09-2020, 03:56 PM   #21
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
cpio -pdumva < list targetdir

cpio is exactly the correct tool for that.
 
Old 02-10-2020, 02:55 AM   #22
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by crts View Post
Actually, there is:
Code:
tar c -T /path/to/filelist | tar x -C /path/to/targetdirectory

If i run:
Code:
tar c -T file.txt
i get:

Code:
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
And the text file contain only list of files.
 
Old 02-10-2020, 03:05 AM   #23
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by berndbausch View Post
cpio -pdumva < list targetdir

cpio is exactly the correct tool for that.

This worked very well. Thanks.
 
Old 02-10-2020, 03:06 AM   #24
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,343
Blog Entries: 3

Rep: Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754Reputation: 3754
Quote:
Originally Posted by peter7089 View Post
If i run:
Code:
tar c -T file.txt
i get:

Of course. That's because a pipe is needed to deal with the outout.

What happens when you put in the rest of the line as suggested?

Code:
tar c -T /path/to/filelist | tar x -C /path/to/targetdirectory
 
Old 02-10-2020, 05:23 AM   #25
peter7089
Member
 
Registered: May 2016
Distribution: MX Linux
Posts: 249

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Of course. That's because a pipe is needed to deal with the outout.

What happens when you put in the rest of the line as suggested?

Code:
tar c -T /path/to/filelist | tar x -C /path/to/targetdirectory
It works but the list have to contain only files. If it have directories it gives error like this:

Code:
tar: usr/bin: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
But with 'cpio' i don't have to clean the list first and remove directories, it works if it contain files and directories. So, i think 'cpio' is the better and simpler solution.
 
Old 02-10-2020, 08:25 AM   #26
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,620

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by peter7089 View Post
It works but the list have to contain only files. If it have directories it gives error like this:

Code:
tar: usr/bin: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
Well you'd already been offered two different solutions for how to filter dpkg output to be files only, but that error message is not because the list contains directories - it is pointing at the relative path "usr/bin", not the absolute path "/usr/bin".
(Did you edit the file manually and make an editing mistake - have you verified no other mistakes were made?)

The same error message it output with cpio, however cpio skips invalid entries instead of aborting (tar can do the same with --ignore failed-read option).

If you corrected the path (or "cd /" first) then it will not error, (but it will include the entire contents of the /usr/bin directory, which is not what you want).

A second difference of cpio compared to tar is that when it see a directory in the list cpio only copies the directory, not its contents - a useful feature to know about, and one that I don't see an equivalent tar option for, which as you say does make cpio a more suitable option for this scenario.

One more note - the -u option overrides the default behaviour of skipping files that haven't changed or are newer, so I would consider not using it, especially if running the command multiple times.

 
Old 02-11-2020, 01:47 PM   #27
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by peter7089 View Post
If i run:
Code:
tar c -T file.txt
i get:

Code:
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
And the text file contain only list of files.
Honestly, I am completely lost here, OP. I just cannot fathom how you could possibly end up taking the course of action that you did. What you basically did is the equivalent of tying only one of your shoelaces and then complain that shoes do not work because you keep tripping.

I am genuinely curious, what was your thought process that made you cut off my proposed solution in the middle of the line and then come back here posting:

"It no worky, worky"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to copy directory and file structure with zero length files dchurch315 Linux - Newbie 7 07-25-2016 08:41 PM
Lost Boot Stick, Can I Install Different Distro and Retain File Structure zarb Linux - General 10 11-03-2014 09:02 AM
[SOLVED] rm files only - retain directory structure schneidz Programming 15 05-30-2013 04:08 PM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Trying to move files beyond cutoff date in subfolders and retain directory structure Erik Mesoy Linux - Newbie 2 03-16-2008 08:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration