Tar command to exclude full path

Posted on 2005-05-02
Last Modified: 2008-03-10
I'm looking for the TAR command that wil exclude directories.
I'm using this command to tar a file, but when i tar it, it wil take the whole pad within the rar. e.g. i want all files in the direcotry /home/user tarred to a tar file. but when i tar the file it will tar /home/user + files!
when i untar the tar file it will tar the content to /home/user/+files

my tar command
tar zcf archive.$day.tar.gz   /var/log/compnaam/*.$day

how to do that

thnx in advance
Question by:Mr-sark
    LVL 4

    Expert Comment


    tar zcf archive.$day.tar.gz  -C /var/log/compnaam/      *.$day

    It changes to the directory /var/log/compnaam/ before tarring, but it's got a funky expansion of * in a lot  of shells (at least bash).
    So if *.$day matches anything in your current dir, that one is used as the argument to tar  (which is bad).

    to be 100% safe you would need to do;
    (cd /var/log/compnaam/; tar zcf $OLDPWD/archive.$day.tar.gz *.$day)

    # this changes to the dir in a "subshell" (...), inside the subsshell you change dir so that $OLDPWD becomes your current dir outside the subshell
    LVL 1

    Expert Comment

    You can create a file and use

    tar --exclude FILE

    in that file, list the things you want excluded...

    LVL 40

    Accepted Solution


    cd /var/log/compnaam
    tar zcf archive.$day.tar.gz   *.$day

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now