Tar Archiving That Takes Input from a List of Files

Tar archiving that takes input from a list of files

Yes:

tar -cvf allfiles.tar -T mylist.txt

Create tar gz on linux with specific list of files from sed output

to use --null, you need to convert newlines to nulls first:

...
| tr '\n' '\0' \
| tar -czvf images.tar.gz --null -T -

(tested, working.)

also, here are a number of suggestions on speed and style in decreasing order of importance.

a. don't find and run file on more files than you need to:

find . -type f -iname "*.png" -or -iname "*.jpg"

b. for commands that can run on multiple files per command, such as file, use xargs to save a lot of time:

find . -type f -iname "*.png" -or -iname "*.jpg" -print0 | xargs -0 file

c. if you put | at the end of each line, you can continue on the next line without also using \.

find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file

d. you can save yourself a lot of trouble since your max width is 999 by just greping for 1, 2, or 3 digit widths, though the awk '$1<1000' is ultimately better in case you ever want to use a different threshold:

find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x '

e. grep and awk are faster than sed, so use them where possible:

find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)'

final command:

find . -type f -iname "*.png" -or -iname "*.jpg" -print0 |
xargs -0 file |
grep ', [0-9][0-9]\?[0-9]\? x ' |
grep -o -i '.*\.\(png\|jpg\)' |
tr '\n' '\0' |
tar -czvf images.tar.gz --null -T -

tar using input list of files/folders with space in their names

When you turn on debug with set -x (and turn off with set -), you will see that the result of $(...) is split up in parameters using spaces as delimiters. In your case (no filenames with newline) you was almost done when you created a list of files, each on one line.
GNU tar can read from a file with the -T option

-T, --files-from=FILE
Get names to extract or create from FILE.
Unless specified otherwise, the FILE must contain a list of names separated by
ASCII LF (i.e. one name per line). The names read are handled the same way as
command line arguments.

You can use the construction <(cmd) when you want the result of cmd being handled as a file.

tar czvf backup_before_update.tgz -C ./test/ -T <(unzip -Z1 update.zip|grep -v \/$)

How do I tar a directory of files and folders without including the directory itself?


cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd - 

should do the job in one line. It works well for hidden files as well. "*" doesn't expand hidden files by path name expansion at least in bash. Below is my experiment:

$ mkdir my_directory
$ touch my_directory/file1
$ touch my_directory/file2
$ touch my_directory/.hiddenfile1
$ touch my_directory/.hiddenfile2
$ cd my_directory/ && tar -zcvf ../my_dir.tgz . && cd ..
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2
$ tar ztf my_dir.tgz
./
./file1
./file2
./.hiddenfile1
./.hiddenfile2

How to tar a set of files?

To simply create a tarball of these files I would just do:

tar cf ones.tar 1_*.txt
tar cf twos.tar 2_*.txt

Most likely you want to compress the tarballs, so use the z option:

tar czf ones.tar.gz 1_*.txt
tar czf twos.tar.gz 2_*.txt

View the contents of your tarballs with tar tf <tarball>.

Reading a specific file from a .tar file via readRDS

It’s a bit tricky but the following works:

tar_filename = 'data.tar'
rds_name = 'file_1.rds'

con = pipe(paste('tar xf', shQuote(tar_filename), '-O' , shQuote(rds_name)), 'rb')
result = readRDS(gzcon(con))
close(con)


Related Topics



Leave a reply



Submit