Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip command without including the compressed dir itself

Tags:

unix

cmd

zip

Suppose the structure:

/foo/bar/
        --file1
        --file2
        --file3
        --folder1
          --file4
        --folder2
          --file5

I want to run the unix zip utility, compressing the bar folder and all of it's files and subfolders, from foo folder, but not have the bar folder inside the zip, using only command line.

If I try to use the -j argument, it doesn't create the bar folder inside the zip as I want, but doesn't create folder1 and folder2. Doing -rj doesn't work.

(I know I can enter inside bar and do zip -r bar.zip . I want to know if it's possible to accomplish what $/foo/bar/ zip -r bar.zip . but doing it from $/foo).

like image 555
Somebody still uses you MS-DOS Avatar asked Sep 02 '10 18:09

Somebody still uses you MS-DOS


People also ask

How do I zip a file without a folder?

You can also create a ZIP file without creating a new folder. Simply select all of the files, open the context menu with a right-click, and click directly on “Send to” and “Compressed (zipped) folder”.

How do I zip all files individually?

Locate the file or folder that you want to zip. Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do you zip all the .TXT files in a directory assume the directory doesn't contain any folders?

Syntax : $zip –m filename.zip file.txt 4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

How do I zip a folder without a new folder in Linux?

You can use -j . -j --junk-paths Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).


1 Answers

You have to do cd /foo/bar then zip -r bar.zip ., however, you can group them with parentheses to run in a subshell:

# instead of: cd /foo/bar; zip -r bar.zip; cd -

( cd /foo/bar; zip -r bar.zip . )

The enclosed (paren-grouped) commands are run in a subshell and cd within it won't affect the outer shell session.

See sh manual.

Compound Commands
    A compound command is one of the following:

    (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below).  
           Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.  
           The return status is the exit status of list.
like image 107
ryenus Avatar answered Oct 22 '22 23:10

ryenus