Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a text file which contains a list of files.

I use.

tar --null --no-recursion -uf abc.tar --directory= /tmp/temp --files-from abc.txt

This command simply creates a tar with all the files listed in the text file.

Is there any way where I can tar only selected number of files from the list? Say for example only first 50 files listed in the text file.

share|improve this question

4 Answers 4

You may try something like: (Updated to allow for whitespaces in file names)

head -50 abc.txt | tr "\n" "\0" | xargs -0 tar --null -no-recursion -uf abc.tar --directory=/tmp/temp

The command head -50 will get you the first 50 lines of the list file. This result is piped | to tr. Basically, tr will perform a translation operation, replacing the newline characters \n by a null character \0. The result is piped to xargs -0, which will convert all these lines into arguments to the tar command. The -0 arguments for xargs makes it separate the arguments based on null characters instead of white spaces. This will make the command works even if your file names has spaces.

Thanks to Jordanm for his comment.

share|improve this answer

Inspired by jordanm's comment on Bichoy's answer, here's how you can deal with file names with spaces in them:

count=50
i=1
while [ $i -le $count ]
do
    read filename
    tar --null -no-recursion -ruf abc.tar "$filename" --directory=/tmp/temp
    i=$(($i+1))
done <abc.txt

I used a while loop instead of doing for i in {1..50} in order to allow you to soft-code the number of lines required.

share|improve this answer
    
In my scenario , the files are seperated using ^@ and i need to tar only certain number of files . is there any command for that in linux? cause i have to do it through java –  Mano Aug 5 '13 at 10:18
    
@user1752557 Do you mean a literal '^' followed by a literal '@' or the control character '^@'? –  Joseph R. Aug 5 '13 at 16:42
    
@user1752557 As for doing it in Java, can you not make a Linux system all from Java? (I'm not that experienced with it so I'm not really certain). –  Joseph R. Aug 5 '13 at 17:46

You can use head or tail to control the number of files that you're limiting from the file abc.txt.

Example

Sample abc.txt file.

$ cat abc.txt
afile1
afile10
afile2
afile3
afile4
afile5
afile6
afile7
afile8
afile9

Now we'll tar only the 1st 3 files from abc.txt:

$ head -3 abc.txt | tr '\n' '\0' | tar --null --no-recursion -uf abc.tar -T -

The above takes the output from head ..., converts the end of line characters (\n) to null characters (\0). This output is then piped to tar, which takes it in through STDIN, (-T -). This last bit tells tar to take STDIN as input, this is the list of file names to tar.

Confirmation of abc.tar:

$ tar tvf abc.tar 
-rw-rw-r-- saml/saml         0 2013-08-05 13:24 afile1
-rw-rw-r-- saml/saml         0 2013-08-05 13:24 afile10
-rw-rw-r-- saml/saml         0 2013-08-05 13:24 afile2

You can change the above from head -3 ... to head -100 ...for example, to get the 1st 100. Or you could usetail -100 ...` to get the last 100 in the file.

share|improve this answer

If you are sure that there are no newlines in your file names, convert the null bytes to newlines and call head to retain only the first few lines.

<abc.txt tr '\0' '\n' |
head -n 50 |
tar --null --no-recursion -uf abc.tar --directory= /tmp/temp --files-from -

To cope with filenames containing newlines, you can transpose newlines and nulls, then let head to its stuff, and transpose back.

<abc.txt tr '\0\n' '\n\0' |
head -n 50 |
tr '\0\n' '\n\0' |
tar --null --no-recursion -uf abc.tar --directory= /tmp/temp --files-from -
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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