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

前端 未结 18 689
孤城傲影
孤城傲影 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条回答
  •  猫巷女王i
    2021-01-29 17:47

    # tar all files within and deeper in a given directory
    # with no prefixes ( neither / nor ./ )
    # parameters:  
    function tar_all_in_dir {
        { cd "$1" && find -type f -print0; } \
        | cut --zero-terminated --characters=3- \
        | tar --create --file="$2" --directory="$1" --null --files-from=-
    }
    

    Safely handles filenames with spaces or other unusual characters. You can optionally add a -name '*.sql' or similar filter to the find command to limit the files included.

提交回复
热议问题