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

前端 未结 18 960
孤城傲影
孤城傲影 2021-01-29 17:04

I typically do:

tar -czvf my_directory.tar.gz my_directory

What if I just want to include everything (including any hidden system files) in my_

18条回答
  •  天命终不由人
    2021-01-29 17:38

    Have a look at --transform/--xform, it gives you the opportunity to massage the file name as the file is added to the archive:

    % mkdir my_directory
    % touch my_directory/file1
    % touch my_directory/file2
    % touch my_directory/.hiddenfile1
    % touch my_directory/.hiddenfile2
    % tar -v -c -f my_dir.tgz --xform='s,my_directory/,,' $(find my_directory -type f)
    my_directory/file2
    my_directory/.hiddenfile1
    my_directory/.hiddenfile2
    my_directory/file1
    % tar -t -f my_dir.tgz 
    file2
    .hiddenfile1
    .hiddenfile2
    file1
    

    Transform expression is similar to that of sed, and we can use separators other than / (, in the above example).
    https://www.gnu.org/software/tar/manual/html_section/tar_52.html

提交回复
热议问题