Archive

Archive for the ‘Linux Commands’ Category

Linux Commands: Move or rename files or directories

March 2nd, 2009 No comments

 

Source: http://webtools.live2support.com/linux/mv.php

 

mv-Linux Command

 

mv

Move or rename files or directories.

SYNTAX
      mv [options]... Source Dest

      mv [options]... Source... Directory

If the last argument names an existing directory, `mv’ moves each other given file into a file with the same name in that directory. Otherwise, if only two files are given, it renames the first as the second. It is an error if the last argument is not a directory and more than two files are given.

OPTIONS  

-b
--backup
     Make a backup of each file that would otherwise be overwritten or
     removed.

-f
--force
     Remove existing destination files and never prompt the user.

-i
--interactive
     Prompt whether to overwrite each existing destination file,
     regardless of its permissions.  If the response does not begin
     with `y' or `Y', the file is skipped.

-S SUFFIX
--suffix=SUFFIX
     Append SUFFIX to each backup file made with `-b'.
     The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.

-u
--update
     Do not move a nondirectory that has an existing destination with
     the same or newer modification time.

-v
--verbose
     Print the name of each file before moving it.

-V METHOD
--version-control=METHOD'
     Change the type of backups made with `-b'. METHOD can be:

       t, numbered     make numbered backups
       nil, existing   numbered if numbered backups exist, simple otherwise
       never, simple   always make simple backups

 --help                   display help and exit
 --version                output version information and exit

Examples



Rename the file apple as orange.doc:
mv apple orange.doc

Move orange.doc to the Documents folder:
mv orange.doc ~/Documents/orange.doc

Rename a bunch of file extensions
e.g. change *.txt into *.htm
  for f in *.txt; do mv ./"$f" "${f%txt}htm"; done

`mv’ can move only regular files across filesystems.

If a destination file exists but is normally unwritable, standard input is a terminal, and the `-f’ or `–force’ option is not given, `mv’ prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response does not begin with `y’ or `Y’, the file is skipped.

Related Linux Bash commands:

cp – Copy one or more files to another location

Categories: Linux Commands Tags:

Linux / Unix Command: unzip

February 21st, 2009 No comments

 

Source: http://linux.about.com/od/commands/l/blcmdl1_unzip.htm

 

NAME

unzip – list, test and extract compressed files in a ZIP archive

unzip [-Z] [-cflptuvz[abjnoqsCLMVX$/:]] file[.zip] [file(s) ...] [-x xfile(s) ...] [-d exdir]

DESCRIPTION

unzip will list, test, or extract files from a ZIP archive, commonly found on MS-DOS systems. The default behavior (with no options) is to extract into the current directory (and subdirectories below it) all files from the specified ZIP archive. A companion program, zip(1L), creates ZIP archives; both programs are compatible with archives created by PKWARE’s PKZIP and PKUNZIP for MS-DOS, but in many cases the program options or default behaviors differ.

ARGUMENTS

file[.zip]
Path of the ZIP archive(s). If the file specification is a wildcard, each matching file is processed in an order determined by the operating system (or file system). Only the filename can be a wildcard; the path itself cannot. Wildcard expressions are similar to those supported in commonly used Unix shells (sh, ksh, csh) and may contain:
*
matches a sequence of 0 or more characters
?
matches exactly 1 character
[...]
matches any single character found inside the brackets; ranges are specified by a beginning character, a hyphen, and an ending character. If an exclamation point or a caret (`!’ or `^’) follows the left bracket, then the range of characters within the brackets is complemented (that is, anything except the characters inside the brackets is considered a match).
(Be sure to quote any character that might otherwise be interpreted or modified by the operating system, particularly under Unix and VMS.) If no matches are found, the specification is assumed to be a literal filename; and if that also fails, the suffix .zip is appended. Note that self-extracting ZIP files are supported, as with any other ZIP archive; just specify the .exe suffix (if any) explicitly.
[file(s)]
An optional list of archive members to be processed, separated by spaces. (VMS versions compiled with VMSCLI defined must delimit files with commas instead. See -v in OPTIONS below.) Regular expressions (wildcards) may be used to match multiple members; see above. Again, be sure to quote expressions that would otherwise be expanded or modified by the operating system.
[-x xfile(s)]
An optional list of archive members to be excluded from processing. Since wildcard characters match directory separators (`/’), this option may be used to exclude any files that are in subdirectories. For example, “unzip foo *.[ch] -x */*” would extract all C source files in the main directory, but none in any subdirectories. Without the -x option, all C source files in all directories within the zipfile would be extracted.
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, “-d ~” (tilde) is expanded by Unix C shells into the name of the user’s home directory, but “-d~” is treated as a literal subdirectory “~” of the current directory.


OPTIONS

Note that, in order to support obsolescent hardware, unzip‘s usage screen is limited to 22 or 23 lines and should therefore be considered only a reminder of the basic unzip syntax rather than an exhaustive list of all possible flags. The exhaustive list follows:

-Z
zipinfo(1L) mode. If the first option on the command line is -Z, the remaining options are taken to be zipinfo(1L) options. See the appropriate manual page for a description of these options.
-A
[OS/2, Unix DLL] print extended help for the DLL’s programming interface (API).
-c
extract files to stdout/screen (“CRT”). This option is similar to the -p option except that the name of each file is printed as it is extracted, the -a option is allowed, and ASCII-EBCDIC conversion is automatically performed if appropriate. This option is not listed in the unzip usage screen.
-f
freshen existing files, i.e., extract only those files that already exist on disk and that are newer than the disk copies. By default unzip queries before overwriting, but the -o option may be used to suppress the queries. Note that under many operating systems, the TZ (timezone) environment variable must be set correctly in order for -f and -u to work properly (under Unix the variable is usually set automatically). The reasons for this are somewhat subtle but have to do with the differences between DOS-format file times (always local time) and Unix-format times (always in GMT/UTC) and the necessity to compare the two. A typical TZ value is “PST8PDT” (US Pacific time with automatic adjustment for Daylight Savings Time or “summer time”).
-l
list archive files (short format). The names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified. If UnZip was compiled with OS2_EAS defined, the -l option also lists columns for the sizes of stored OS/2 extended attributes (EAs) and OS/2 access control lists (ACLs). In addition, the zipfile comment and individual file comments (if any) are displayed. If a file was archived from a single-case file system (for example, the old MS-DOS FAT file system) and the -L option was given, the filename is converted to lowercase and is prefixed with a caret (^).
-p
extract files to pipe (stdout). Nothing but the file data is sent to stdout, and the files are always extracted in binary format, just as they are stored (no conversions).
-t
test archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum) of the expanded file with the original file’s stored CRC value.
-T
[most OSes] set the timestamp on the archive(s) to that of the newest file in each one. This corresponds to zip‘s -go option except that it can be used on wildcard zipfiles (e.g., “unzip -T \*.zip”) and is much faster.
-u
update existing files and create new ones if needed. This option performs the same function as the -f option, extracting (with query) files that are newer than those with the same name on disk, and in addition it extracts those files that do not already exist on disk. See -f above for information on setting the timezone properly.
-v
be verbose or print diagnostic version info. This option has evolved and now behaves as both an option and a modifier. As an option it has two purposes: when a zipfile is specified with no other options, -v lists archive files verbosely, adding to the basic -l info the compression method, compressed size, compression ratio and 32-bit CRC. When no zipfile is specified (that is, the complete command is simply “unzip -v”), a diagnostic screen is printed. In addition to the normal header with release date and version, unzip lists the home Info-ZIP ftp site and where to find a list of other ftp and non-ftp sites; the target operating system for which it was compiled, as well as (possibly) the hardware on which it was compiled, the compiler and version used, and the compilation date; any special compilation options that might affect the program’s operation (see also DECRYPTION below); and any options stored in environment variables that might do the same (see ENVIRONMENT OPTIONS below). As a modifier it works in conjunction with other options (e.g., -t) to produce more verbose or debugging output; this is not yet fully implemented but will be in future releases.
-z
display only the archive comment.


MODIFIERS

-a
convert text files. Ordinarily all files are extracted exactly as they are stored (as “binary” files). The -a option causes files identified by zip as text files (those with the `t’ label in zipinfo listings, rather than `b’) to be automatically extracted as such, converting line endings, end-of-file characters and the character set itself as necessary. (For example, Unix files use line feeds (LFs) for end-of-line (EOL) and have no end-of-file (EOF) marker; Macintoshes use carriage returns (CRs) for EOLs; and most PC operating systems use CR+LF for EOLs and control-Z for EOF. In addition, IBM mainframes and the Michigan Terminal System use EBCDIC rather than the more common ASCII character set, and NT supports Unicode.) Note that zip‘s identification of text files is by no means perfect; some “text” files may actually be binary and vice versa. unzip therefore prints “[text]” or “[binary]” as a visual check for each file it extracts when using the -a option. The -aa option forces all files to be extracted as text, regardless of the supposed file type.
-b
[general] treat all files as binary (no text conversions). This is a shortcut for —a.
-b
[Tandem] force the creation files with filecode type 180 (‘C’) when extracting Zip entries marked as "text". (On Tandem, -a is enabled by default, see above).
-b
[VMS] auto-convert binary files (see -a above) to fixed-length, 512-byte record format. Doubling the option (-bb) forces all files to be extracted in this format. When extracting to standard output (-c or -p option in effect), the default conversion of text record delimiters is disabled for binary (-b) resp. all (-bb) files.
-B
[Unix only, and only if compiled with UNIXBACKUP defined] save a backup copy of each overwritten file with a tilde appended (e.g., the old copy of “foo” is renamed to “foo~”). This is similar to the default behavior of emacs(1) in many locations.
-C
match filenames case-insensitively. unzip‘s philosophy is “you get what you ask for” (this is also responsible for the -L/-U change; see the relevant options below). Because some file systems are fully case-sensitive (notably those under the Unix operating system) and because both ZIP archives and unzip itself are portable across platforms, unzip‘s default behavior is to match both wildcard and literal filenames case-sensitively. That is, specifying “makefile” on the command line will only match “makefile” in the archive, not “Makefile” or “MAKEFILE” (and similarly for wildcard specifications). Since this does not correspond to the behavior of many other operating/file systems (for example, OS/2 HPFS, which preserves mixed case but is not sensitive to it), the -C option may be used to force all filename matches to be case-insensitive. In the example above, all three files would then match “makefile” (or “make*”, or similar). The -C option affects files in both the normal file list and the excluded-file list (xlist).
-E
[MacOS only] display contents of MacOS extra field during restore operation.
-F
[Acorn only] suppress removal of NFS filetype extension from stored filenames.
-F
[non-Acorn systems supporting long filenames with embedded commas, and only if compiled with ACORN_FTYPE_NFS defined] translate filetype information from ACORN RISC OS extra field blocks into a NFS filetype extension and append it to the names of the extracted files. (When the stored filename appears to already have an appended NFS filetype extension, it is replaced by the info from the extra field.)
-i
[MacOS only] ignore filenames stored in MacOS extra fields. Instead, the most compatible filename stored in the generic part of the entry’s header is used.
-j
junk paths. The archive’s directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one).
-J
[BeOS only] junk file attributes. The file’s BeOS file attributes are not restored, just the file’s data.
-J
[MacOS only] ignore MacOS extra fields. All Macintosh specific info is skipped. Data-fork and resource-fork are restored as separate files.
-L
convert to lowercase any filename originating on an uppercase-only operating system or file system. (This was unzip‘s default behavior in releases prior to 5.11; the new default behavior is identical to the old behavior with the -U option, which is now obsolete and will be removed in a future release.) Depending on the archiver, files archived under single-case file systems (VMS, old MS-DOS FAT, etc.) may be stored as all-uppercase names; this can be ugly or inconvenient when extracting to a case-preserving file system such as OS/2 HPFS or a case-sensitive one such as under Unix. By default unzip lists and extracts such filenames exactly as they’re stored (excepting truncation, conversion of unsupported characters, etc.); this option causes the names of all files from certain systems to be converted to lowercase. The -LL option forces conversion of every filename to lowercase, regardless of the originating file system.
-M
pipe all output through an internal pager similar to the Unix more(1) command. At the end of a screenful of output, unzip pauses with a “–More–” prompt; the next screenful may be viewed by pressing the Enter (Return) key or the space bar. unzip can be terminated by pressing the “q” key and, on some systems, the Enter/Return key. Unlike Unix more(1), there is no forward-searching or editing capability. Also, unzip doesn’t notice if long lines wrap at the edge of the screen, effectively resulting in the printing of two or more lines and the likelihood that some text will scroll off the top of the screen before being viewed. On some systems the number of available lines on the screen is not detected, in which case unzip assumes the height is 24 lines.
-n
never overwrite existing files. If a file already exists, skip the extraction of that file without prompting. By default unzip queries before extracting any file that already exists; the user may choose to overwrite only the current file, overwrite all files, skip extraction of the current file, skip extraction of all existing files, or rename the current file.
-N
[Amiga] extract file comments as Amiga filenotes. File comments are created with the -c option of zip(1L), or with the -N option of the Amiga port of zip(1L), which stores filenotes as comments.
-o
overwrite existing files without prompting. This is a dangerous option, so use it with care. (It is often used with -f, however, and is the only way to overwrite directory EAs under OS/2.)
-P password
use password to decrypt encrypted zipfile entries (if any). THIS IS INSECURE! Many multi-user operating systems provide ways for any user to see the current command line of any other user; even on stand-alone systems there is always the threat of over-the-shoulder peeking. Storing the plaintext password as part of a command line in an automated script is even worse. Whenever possible, use the non-echoing, interactive prompt to enter passwords. (And where security is truly important, use strong encryption such as Pretty Good Privacy instead of the relatively weak encryption provided by standard zipfile utilities.)
-q
perform operations quietly (-qq = even quieter). Ordinarily unzip prints the names of the files it’s extracting or testing, the extraction methods, any file or zipfile comments that may be stored in the archive, and possibly a summary when finished with each archive. The -q[q] options suppress the printing of some or all of these messages.
-s
[OS/2, NT, MS-DOS] convert spaces in filenames to underscores. Since all PC operating systems allow spaces in filenames, unzip by default extracts filenames with spaces intact (e.g., “EA DATA. SF”). This can be awkward, however, since MS-DOS in particular does not gracefully support spaces in filenames. Conversion of spaces to underscores can eliminate the awkwardness in some cases.
-U
(obsolete; to be removed in a future release) leave filenames uppercase if created under MS-DOS, VMS, etc. See -L above.
-V
retain (VMS) file version numbers. VMS files can be stored with a version number, in the format file.ext;##. By default the “;##” version numbers are stripped, but this option allows them to be retained. (On file systems that limit filenames to particularly short lengths, the version numbers may be truncated or stripped regardless of this option.)
-X
[VMS, Unix, OS/2, NT] restore owner/protection info (UICs) under VMS, or user and group info (UID/GID) under Unix, or access control lists (ACLs) under certain network-enabled versions of OS/2 (Warp Server with IBM LAN Server/Requester 3.0 to 5.0; Warp Connect with IBM Peer 1.0), or security ACLs under Windows NT. In most cases this will require special system privileges, and doubling the option (-XX) under NT instructs unzip to use privileges for extraction; but under Unix, for example, a user who belongs to several groups can restore files owned by any of those groups, as long as the user IDs match his or her own. Note that ordinary file attributes are always restored–this option applies only to optional, extra ownership info available on some operating systems. [NT's access control lists do not appear to be especially compatible with OS/2's, so no attempt is made at cross-platform portability of access privileges. It is not clear under what conditions this would ever be useful anyway.]
-$
[MS-DOS, OS/2, NT] restore the volume label if the extraction medium is removable (e.g., a diskette). Doubling the option (-$$) allows fixed media (hard disks) to be labelled as well. By default, volume labels are ignored.
-/ extensions
[Acorn only] overrides the extension list supplied by Unzip$Ext environment variable. During extraction, filename extensions that match one of the items in this extension list are swapped in front of the base name of the extracted file.
-:
[all but Acorn, VM/CMS, MVS, Tandem] allows to extract archive members into locations outside of the current “ extraction root folder”. For security reasons, unzip normally removes “parent dir” path components (“../”) from the names of extracted file. This safety feature (new for version 5.50) prevents unzip from accidentally writing files to “sensitive” areas outside the active extraction folder tree head. The -: option lets unzip switch back to its previous, more liberal behaviour, to allow exact extraction of (older) archives that used “../” components to create multiple directory trees at the level of the current extraction folder.


ENVIRONMENT OPTIONS

unzip‘s default behavior may be modified via options placed in an environment variable. This can be done with any option, but it is probably most useful with the -a, -L, -C, -q, -o, or -n modifiers: make unzip auto-convert text files by default, make it convert filenames from uppercase systems to lowercase, make it match names case-insensitively, make it quieter, or make it always overwrite or never overwrite files as it extracts them. For example, to make unzip act as quietly as possible, only reporting errors, one would use one of the following commands:

Unix Bourne shell:
UNZIP=-qq; export UNZIP
Unix C shell:
setenv UNZIP -qq
OS/2 or MS-DOS:
set UNZIP=-qq
VMS (quotes for lowercase):
define UNZIP_OPTS ""-qq""

Environment options are, in effect, considered to be just like any other command-line options, except that they are effectively the first options on the command line. To override an environment option, one may use the “minus operator” to remove it. For instance, to override one of the quiet-flags in the example above, use the command

unzip --q[other options] zipfile

The first hyphen is the normal switch character, and the second is a minus sign, acting on the q option. Thus the effect here is to cancel one quantum of quietness. To cancel both quiet flags, two (or more) minuses may be used:

unzip -t--q zipfile
unzip ---qt zipfile

(the two are equivalent). This may seem awkward or confusing, but it is reasonably intuitive: just ignore the first hyphen and go from there. It is also consistent with the behavior of Unix nice(1).

As suggested by the examples above, the default variable names are UNZIP_OPTS for VMS (where the symbol used to install unzip as a foreign command would otherwise be confused with the environment variable), and UNZIP for all other operating systems. For compatibility with zip(1L), UNZIPOPT is also accepted (don’t ask). If both UNZIP and UNZIPOPT are defined, however, UNZIP takes precedence. unzip‘s diagnostic option (-v with no zipfile name) can be used to check the values of all four possible unzip and zipinfo environment variables.

The timezone variable (TZ) should be set according to the local timezone in order for the -f and -u to operate correctly. See the description of -f above for details. This variable may also be necessary in order for timestamps on extracted files to be set correctly. Under Windows 95/NT unzip should know the correct timezone even if TZ is unset, assuming the timezone is correctly set in the Control Panel.

DECRYPTION

Encrypted archives are fully supported by Info-ZIP software, but due to United States export restrictions, de-/encryption support might be disabled in your compiled binary. However, since spring 2000, US export restrictions have been liberated, and our source archives do now include full crypt code. In case you need binary distributions with crypt support enabled, see the file “WHERE” in any Info-ZIP source or binary distribution for locations both inside and outside the US.

Some compiled versions of unzip may not support decryption. To check a version for crypt support, either attempt to test or extract an encrypted archive, or else check unzip‘s diagnostic screen (see the -v option above) for “[decryption]” as one of the special compilation options.

As noted above, the -P option may be used to supply a password on the command line, but at a cost in security. The preferred decryption method is simply to extract normally; if a zipfile member is encrypted, unzip will prompt for the password without echoing what is typed. unzip continues to use the same password as long as it appears to be valid, by testing a 12-byte header on each file. The correct password will always check out against the header, but there is a 1-in-256 chance that an incorrect password will as well. (This is a security feature of the PKWARE zipfile format; it helps prevent brute-force attacks that might otherwise gain a large speed advantage by testing only the header.) In the case that an incorrect password is given but it passes the header test anyway, either an incorrect CRC will be generated for the extracted data or else unzip will fail during the extraction because the “decrypted” bytes do not constitute a valid compressed data stream.

If the first password fails the header check on some file, unzip will prompt for another password, and so on until all files are extracted. If a password is not known, entering a null password (that is, just a carriage return or “Enter”) is taken as a signal to skip all further prompting. Only unencrypted files in the archive(s) will thereafter be extracted. (In fact, that’s not quite true; older versions of zip(1L) and zipcloak(1L) allowed null passwords, so unzip checks each encrypted file to see if the null password works. This may result in “false positives” and extraction errors, as noted above.)

Archives encrypted with 8-bit passwords (for example, passwords with accented European characters) may not be portable across systems and/or other archivers. This problem stems from the use of multiple encoding methods for such characters, including Latin-1 (ISO 8859-1) and OEM code page 850. DOS PKZIP 2.04g uses the OEM code page; Windows PKZIP 2.50 uses Latin-1 (and is therefore incompatible with DOS PKZIP); Info-ZIP uses the OEM code page on DOS, OS/2 and Win3.x ports but Latin-1 everywhere else; and Nico Mak’s WinZip 6.x does not allow 8-bit passwords at all. UnZip 5.3 (or newer) attempts to use the default character set first (e.g., Latin-1), followed by the alternate one (e.g., OEM code page) to test passwords. On EBCDIC systems, if both of these fail, EBCDIC encoding will be tested as a last resort. (EBCDIC is not tested on non-EBCDIC systems, because there are no known archivers that encrypt using EBCDIC encoding.) ISO character encodings other than Latin-1 are not supported.

EXAMPLES

To use unzip to extract all members of the archive letters.zip into the current directory and subdirectories below it, creating any subdirectories as necessary:

unzip letters

To extract all members of letters.zip into the current directory only:

unzip -j letters

To test letters.zip, printing only a summary message indicating whether the archive is OK or not:

unzip -tq letters

To test all zipfiles in the current directory, printing only the summaries:

unzip -tq \*.zip

(The backslash before the asterisk is only required if the shell expands wildcards, as in Unix; double quotes could have been used instead, as in the source examples below.) To extract to standard output all members of letters.zip whose names end in .tex, auto-converting to the local end-of-line convention and piping the output into more(1):

unzip -ca letters \*.tex | more

To extract the binary file paper1.dvi to standard output and pipe it to a printing program:

unzip -p articles paper1.dvi | dvips

To extract all FORTRAN and C source files–*.f, *.c, *.h, and Makefile–into the /tmp directory:

unzip source.zip "*.[fch]" Makefile -d /tmp

(the double quotes are necessary only in Unix and only if globbing is turned on). To extract all FORTRAN and C source files, regardless of case (e.g., both *.c and *.C, and any makefile, Makefile, MAKEFILE or similar):

unzip -C source.zip "*.[fch]" makefile -d /tmp

To extract any such files but convert any uppercase MS-DOS or VMS names to lowercase and convert the line-endings of all of the files to the local standard (without respect to any files that might be marked “binary”):

unzip -aaCL source.zip "*.[fch]" makefile -d /tmp

To extract only newer versions of the files already in the current directory, without querying (NOTE: be careful of unzipping in one timezone a zipfile created in another–ZIP archives other than those created by Zip 2.1 or later contain no timezone information, and a “newer” file from an eastern timezone may, in fact, be older):

unzip -fo sources

To extract newer versions of the files already in the current directory and to create any files not already there (same caveat as previous example):

unzip -uo sources

To display a diagnostic screen showing which unzip and zipinfo options are stored in environment variables, whether decryption support was compiled in, the compiler with which unzip was compiled, etc.:

unzip -v

In the last five examples, assume that UNZIP or UNZIP_OPTS is set to -q. To do a singly quiet listing:

unzip -l file.zip

To do a doubly quiet listing:

unzip -ql file.zip

(Note that the “.zip” is generally not necessary.) To do a standard listing:

unzip --ql file.zip

or

unzip -l-q file.zip

or

unzip -l--q file.zip

(Extra minuses in options don’t hurt.)

TIPS

The current maintainer, being a lazy sort, finds it very useful to define a pair of aliases: tt for “unzip -tq” and ii for “unzip -Z” (or “zipinfo”). One may then simply type “tt zipfile” to test an archive, something that is worth making a habit of doing. With luck unzip will report “No errors detected in compressed data of zipfile.zip,” after which one may breathe a sigh of relief.

The maintainer also finds it useful to set the UNZIP environment variable to “-aL” and is tempted to add “-C” as well. His ZIPINFO variable is set to “-z”.

SEE ALSO

funzip(1L), zip(1L), zipcloak(1L), zipgrep(1L), zipinfo(1L), zipnote(1L), zipsplit(1L)

URL

The Info-ZIP home page is currently at

http://www.info-zip.org/pub/infozip/

or

ftp://ftp.info-zip.org/pub/infozip/ .
Categories: Linux Commands Tags:

Linux / Unix Command: tar

February 21st, 2009 No comments

 

Source: http://linux.about.com/od/commands/l/blcmdl1_tar.htm

 

NAME

tar – The GNU version of the tar archiving utility

SYNOPSIS

tar [ - ] A –catenate –concatenate | c –create | d –diff –compare | r –append | t –list | u –update | x -extract –get [ --atime-preserve ] [ -b, --block-size N ] [ -B, --read-full-blocks ] [ -C, --directory DIR ] [ --checkpoint ]
[ -f, --file [HOSTNAME:]F ] [ --force-local ]
[ -F, --info-script F --new-volume-script F ] [ -G, --incremental ] [ -g, --listed-incremental F ] [ -h, --dereference ] [ -i, --ignore-zeros ] [ -j, -I, --bzip ] [ --ignore-failed-read ] [ -k, --keep-old-files ] [ -K, --starting-file F ] [ -l, --one-file-system ] [ -L, --tape-length N ] [ -m, --modification-time ] [ -M, --multi-volume ] [ -N, --after-date DATE, --newer DATE ] [ -o, --old-archive, --portability ] [ -O, --to-stdout ] [ -p, --same-permissions, --preserve-permissions ] [ -P, --absolute-paths ] [ --preserve ]
[ -R, --record-number ] [ --remove-files ] [ -s, --same-order, --preserve-order ] [ --same-owner ] [ -S, --sparse ] [ -T, --files-from=F ] [ --null ]
[ --totals ]
[ -v, --verbose ] [ -V, --label NAME ] [ --version ]
[ -w, --interactive, --confirmation ] [ -W, --verify ]
[ --exclude FILE ] [ -X, --exclude-from FILE ] [ -Z, --compress, --uncompress ] [ -z, --gzip, --ungzip ]
[ --use-compress-program PROG ] [ --block-compress ] [ -[0-7][lmh] ]

filename1 [ filename2, ... filenameN ]
directory1 [ directory2, ...directoryN ]


DESCRIPTION

This manual page documents the GNU version of tar , an archiving program designed to store and extract files from an archive file known as a tarfile. A tarfile may be made on a tape drive, however, it is also common to write a tarfile to a normal file. The first argument to tar must be one of the options: Acdrtux, followed by any optional functions. The final arguments to tar are the names of the files or directories which should be archived. The use of a directory name always implies that the subdirectories below should be included in the archive.

FUNCTION LETTERS

One of the following options must be used:
-A, –catenate, –concatenate
append tar files to an archive
-c, –create
create a new archive
-d, –diff, –compare
find differences between archive and file system
–delete
delete from the archive (not for use on mag tapes!)
-r, –append
append files to the end of an archive
-t, –list
list the contents of an archive
-u, –update
only append files that are newer than copy in archive
-x, –extract, –get
extract files from an archive


OTHER OPTIONS

–atime-preserve
don’t change access times on dumped files
-b, –block-size N
block size of Nx512 bytes (default N=20)
-B, –read-full-blocks
reblock as we read (for reading 4.2BSD pipes)
-C, –directory DIR
change to directory DIR
–checkpoint
print directory names while reading the archive
-f, –file [HOSTNAME:]F
use archive file or device F (default /dev/rmt0)
–force-local
archive file is local even if has a colon
-F, –info-script F –new-volume-script F
run script at end of each tape (implies -M)
-G, –incremental
create/list/extract old GNU-format incremental backup
-g, –listed-incremental F
create/list/extract new GNU-format incremental backup
-h, –dereference
don’t dump symlinks; dump the files they point to
-i, –ignore-zeros
ignore blocks of zeros in archive (normally mean EOF)
-j, -I, –bzip
filter the archive through bzip2. Note: -I is deprecated and may get a different meaning in the near future.
–ignore-failed-read
don’t exit with non-zero status on unreadable files
-k, –keep-old-files
keep existing files; don’t overwrite them from archive
-K, –starting-file F
begin at file F in the archive
-l, –one-file-system
stay in local file system when creating an archive
-L, –tape-length N
change tapes after writing N*1024 bytes
-m, –modification-time
don’t extract file modified time
-M, –multi-volume
create/list/extract multi-volume archive
-N, –after-date DATE, –newer DATE
only store files newer than DATE
-o, –old-archive, –portability
write a V7 format archive, rather than ANSI format
-O, –to-stdout
extract files to standard output
-p, –same-permissions, –preserve-permissions
extract all protection information
-P, –absolute-paths
don’t strip leading `/’s from file names
–preserve
like -p -s
-R, –record-number
show record number within archive with each message
–remove-files
remove files after adding them to the archive
-s, –same-order, –preserve-order
list of names to extract is sorted to match archive
–same-owner
create extracted files with the same ownership
-S, –sparse
handle sparse files efficiently
-T, –files-from=F
get names to extract or create from file F
–null
-T reads null-terminated names, disable -C
–totals
print total bytes written with –create
-v, –verbose
verbosely list files processed
-V, –label NAME
create archive with volume name NAME
–version
print tar program version number
-w, –interactive, –confirmation
ask for confirmation for every action
-W, –verify
attempt to verify the archive after writing it
–exclude FILE
exclude file FILE
-X, –exclude-from FILE
exclude files listed in FILE
-Z, –compress, –uncompress
filter the archive through compress
-z, –gzip, –ungzip
filter the archive through gzip
–use-compress-program PROG
filter the archive through PROG (which must accept -d)
Categories: Linux Commands Tags:

Linux / Unix Command: gzip

February 21st, 2009 No comments

 

Source: http://linux.about.com/od/commands/l/blcmdl1_gzip.htm

 

NAME

gzip, gunzip, zcat – compress or expand files

SYNOPSIS

gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ]
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]
zcat [ -fhLV ] [ name ... ]

DESCRIPTION

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. (The default extension is -gz for VMS, z for MSDOS, OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if a file name is "-", the standard input is compressed to the standard output. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.

 

If the compressed file name is too long for its file system, gzip truncates it. Gzip attempts to truncate only the parts of the file name longer than 3 characters. (A part is delimited by dots.) If the name consists of small parts only, the longest parts are truncated. For example, if file names are limited to 14 characters, gzip.msdos.exe is compressed to gzi.msd.exe.gz. Names are not truncated on systems which do not have a limit on file name length.

By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the -N option. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.

Compressed files can be restored to their original form using gzip -d or gunzip or zcat. If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it legal.

gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, _z or .Z and which begins with the correct magic number with an uncompressed file without the original extension. gunzip also recognizes the special extensions .tgz and .taz as shorthands for .tar.gz and .tar.Z respectively. When compressing, gzip uses the .tgz extension if necessary instead of truncating a file with a .tar extension.

gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack. The detection of the input format is automatic. When using the first two formats, gunzip checks a 32 bit CRC. For pack, gunzip checks the uncompressed length. The standard compress format was not designed to allow consistency checks. However gunzip is sometimes able to detect a bad .Z file. If you get an error when uncompressing a .Z file, do not assume that the .Z file is correct simply because the standard uncompress does not complain. This generally means that the standard uncompress does not check its input, and happily generates garbage output. The SCO compress -H format (lzh compression method) does not include a CRC but also allows some consistency checks.

Files created by zip can be uncompressed by gzip only if they have a single member compressed with the ‘deflation’ method. This feature is only intended to help conversion of tar.zip files to the tar.gz format. To extract zip files with several members, use unzip instead of gunzip.

zcat is identical to gunzip -c. (On some systems, zcat may be installed as gzcat to preserve the original link to compress.) zcat uncompresses either a list of files on the command line or its standard input and writes the uncompressed data on standard output. zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.

Gzip uses the Lempel-Ziv algorithm used in zip and PKZIP. The amount of compression obtained depends on the size of the input and the distribution of common substrings. Typically, text such as source code or English is reduced by 60-70%. Compression is generally much better than that achieved by LZW (as used in compress), Huffman coding (as used in pack), or adaptive Huffman coding (compact).

Compression is always performed, even if the compressed file is slightly larger than the original. The worst case expansion is a few bytes for the gzip file header, plus 5 bytes every 32K block, or an expansion ratio of 0.015% for large files. Note that the actual number of used disk blocks almost never increases. gzip preserves the mode, ownership and timestamps of files when compressing or decompressing.

OPTIONS

-a –ascii
Ascii text mode: convert end-of-lines using local conventions. This option is supported only on some non-Unix systems. For MSDOS, CR LF is converted to LF when compressing, and LF is converted to CR LF when decompressing.
-c –stdout –to-stdout
Write output on standard output; keep original files unchanged. If there are several input files, the output consists of a sequence of independently compressed members. To obtain better compression, concatenate all input files before compressing them.
-d –decompress –uncompress
Decompress.
-f –force
Force compression or decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal. If the input data is not in a format recognized by gzip, and if the option –stdout is also given, copy the input data without change to the standard ouput: let zcat behave as cat. If -f is not given, and when not running in the background, gzip prompts to verify whether an existing file should be overwritten.
-h –help
Display a help screen and quit.
-l –list
For each compressed file, list the following fields:

compressed size: size of the compressed file
uncompressed size: size of the uncompressed file
ratio: compression ratio (0.0% if unknown)
uncompressed_name: name of the uncompressed file

The uncompressed size is given as -1 for files not in gzip format, such as compressed .Z files. To get the uncompressed size for such a file, you can use:

zcat file.Z | wc -c

In combination with the –verbose option, the following fields are also displayed:

method: compression method
crc: the 32-bit CRC of the uncompressed data
date & time: time stamp for the uncompressed file

The compression methods currently supported are deflate, compress, lzh (SCO compress -H) and pack. The crc is given as ffffffff for a file not in gzip format.

With –name, the uncompressed name, date and time are those stored within the compress file if present.

With –verbose, the size totals and compression ratio for all files is also displayed, unless some sizes are unknown. With –quiet, the title and totals lines are not displayed.

-L –license
Display the gzip license and quit.
-n –no-name
When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had to be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the compressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This option is the default when decompressing.
-N –name
When compressing, always save the original file name and time stamp; this is the default. When decompressing, restore the original file name and time stamp if present. This option is useful on systems which have a limit on file name length or when the time stamp has been lost after a file transfer.
-q –quiet
Suppress all warnings.
-r –recursive
Travel the directory structure recursively. If any of the file names specified on the command line are directories, gzip will descend into the directory and compress all the files it finds there (or decompress them in the case of gunzip ).
-S .suf –suffix .suf
Use suffix .suf instead of .gz. Any suffix can be given, but suffixes other than .z and .gz should be avoided to avoid confusion when files are transferred to other systems. A null suffix forces gunzip to try decompression on all given files regardless of suffix, as in:

gunzip -S "" * (*.* for MSDOS)

Previous versions of gzip used the .z suffix. This was changed to avoid a conflict with pack(1).

-t –test
Test. Check the compressed file integrity.
-v –verbose
Verbose. Display the name and percentage reduction for each file compressed or decompressed.
-V –version
Version. Display the version number and compilation options then quit.
-# –fast –best
Regulate the speed of compression using the specified digit #, where -1 or –fast indicates the fastest compression method (less compression) and -9 or –best indicates the slowest compression method (best compression). The default compression level is -6 (that is, biased towards high compression at expense of speed).


ADVANCED USAGE

Multiple compressed files can be concatenated. In this case, gunzip will extract all members at once. For example:

gzip -c file1 > foo.gz
gzip -c file2 >> foo.gz

Then

gunzip -c foo

is equivalent to

cat file1 file2

In case of damage to one member of a .gz file, other members can still be recovered (if the damaged member is removed). However, you can get better compression by compressing all members at once:

cat file1 file2 | gzip > foo.gz

compresses better than

gzip -c file1 file2 > foo.gz

If you want to recompress concatenated files to get better compression, do:

gzip -cd old.gz | gzip > new.gz

If a compressed file consists of several members, the uncompressed size and CRC reported by the –list option applies to the last member only. If you need the uncompressed size for all members, you can use:

gzip -cd file.gz | wc -c

If you wish to create a single archive file with multiple members so that members can later be extracted independently, use an archiver such as tar or zip. GNU tar supports the -z option to invoke gzip transparently. gzip is designed as a complement to tar, not as a replacement.

Categories: Linux Commands Tags:

Linux / Unix Command: gzip

February 21st, 2009 No comments

 

Source: http://linux.about.com/od/commands/l/blcmdl1_gzip.htm

 

NAME

gzip, gunzip, zcat – compress or expand files

SYNOPSIS

gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ]
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]
zcat [ -fhLV ] [ name ... ]

DESCRIPTION

Gzip reduces the size of the named files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times. (The default extension is -gz for VMS, z for MSDOS, OS/2 FAT, Windows NT FAT and Atari.) If no files are specified, or if a file name is "-", the standard input is compressed to the standard output. Gzip will only attempt to compress regular files. In particular, it will ignore symbolic links.

 

If the compressed file name is too long for its file system, gzip truncates it. Gzip attempts to truncate only the parts of the file name longer than 3 characters. (A part is delimited by dots.) If the name consists of small parts only, the longest parts are truncated. For example, if file names are limited to 14 characters, gzip.msdos.exe is compressed to gzi.msd.exe.gz. Names are not truncated on systems which do not have a limit on file name length.

By default, gzip keeps the original file name and timestamp in the compressed file. These are used when decompressing the file with the -N option. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer.

Compressed files can be restored to their original form using gzip -d or gunzip or zcat. If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it legal.

gunzip takes a list of files on its command line and replaces each file whose name ends with .gz, -gz, .z, -z, _z or .Z and which begins with the correct magic number with an uncompressed file without the original extension. gunzip also recognizes the special extensions .tgz and .taz as shorthands for .tar.gz and .tar.Z respectively. When compressing, gzip uses the .tgz extension if necessary instead of truncating a file with a .tar extension.

gunzip can currently decompress files created by gzip, zip, compress, compress -H or pack. The detection of the input format is automatic. When using the first two formats, gunzip checks a 32 bit CRC. For pack, gunzip checks the uncompressed length. The standard compress format was not designed to allow consistency checks. However gunzip is sometimes able to detect a bad .Z file. If you get an error when uncompressing a .Z file, do not assume that the .Z file is correct simply because the standard uncompress does not complain. This generally means that the standard uncompress does not check its input, and happily generates garbage output. The SCO compress -H format (lzh compression method) does not include a CRC but also allows some consistency checks.

Files created by zip can be uncompressed by gzip only if they have a single member compressed with the ‘deflation’ method. This feature is only intended to help conversion of tar.zip files to the tar.gz format. To extract zip files with several members, use unzip instead of gunzip.

zcat is identical to gunzip -c. (On some systems, zcat may be installed as gzcat to preserve the original link to compress.) zcat uncompresses either a list of files on the command line or its standard input and writes the uncompressed data on standard output. zcat will uncompress files that have the correct magic number whether they have a .gz suffix or not.

Gzip uses the Lempel-Ziv algorithm used in zip and PKZIP. The amount of compression obtained depends on the size of the input and the distribution of common substrings. Typically, text such as source code or English is reduced by 60-70%. Compression is generally much better than that achieved by LZW (as used in compress), Huffman coding (as used in pack), or adaptive Huffman coding (compact).

Compression is always performed, even if the compressed file is slightly larger than the original. The worst case expansion is a few bytes for the gzip file header, plus 5 bytes every 32K block, or an expansion ratio of 0.015% for large files. Note that the actual number of used disk blocks almost never increases. gzip preserves the mode, ownership and timestamps of files when compressing or decompressing.

OPTIONS

-a –ascii
Ascii text mode: convert end-of-lines using local conventions. This option is supported only on some non-Unix systems. For MSDOS, CR LF is converted to LF when compressing, and LF is converted to CR LF when decompressing.
-c –stdout –to-stdout
Write output on standard output; keep original files unchanged. If there are several input files, the output consists of a sequence of independently compressed members. To obtain better compression, concatenate all input files before compressing them.
-d –decompress –uncompress
Decompress.
-f –force
Force compression or decompression even if the file has multiple links or the corresponding file already exists, or if the compressed data is read from or written to a terminal. If the input data is not in a format recognized by gzip, and if the option –stdout is also given, copy the input data without change to the standard ouput: let zcat behave as cat. If -f is not given, and when not running in the background, gzip prompts to verify whether an existing file should be overwritten.
-h –help
Display a help screen and quit.
-l –list
For each compressed file, list the following fields:

compressed size: size of the compressed file
uncompressed size: size of the uncompressed file
ratio: compression ratio (0.0% if unknown)
uncompressed_name: name of the uncompressed file

The uncompressed size is given as -1 for files not in gzip format, such as compressed .Z files. To get the uncompressed size for such a file, you can use:

zcat file.Z | wc -c

In combination with the –verbose option, the following fields are also displayed:

method: compression method
crc: the 32-bit CRC of the uncompressed data
date & time: time stamp for the uncompressed file

The compression methods currently supported are deflate, compress, lzh (SCO compress -H) and pack. The crc is given as ffffffff for a file not in gzip format.

With –name, the uncompressed name, date and time are those stored within the compress file if present.

With –verbose, the size totals and compression ratio for all files is also displayed, unless some sizes are unknown. With –quiet, the title and totals lines are not displayed.

-L –license
Display the gzip license and quit.
-n –no-name
When compressing, do not save the original file name and time stamp by default. (The original name is always saved if the name had to be truncated.) When decompressing, do not restore the original file name if present (remove only the gzip suffix from the compressed file name) and do not restore the original time stamp if present (copy it from the compressed file). This option is the default when decompressing.
-N –name
When compressing, always save the original file name and time stamp; this is the default. When decompressing, restore the original file name and time stamp if present. This option is useful on systems which have a limit on file name length or when the time stamp has been lost after a file transfer.
-q –quiet
Suppress all warnings.
-r –recursive
Travel the directory structure recursively. If any of the file names specified on the command line are directories, gzip will descend into the directory and compress all the files it finds there (or decompress them in the case of gunzip ).
-S .suf –suffix .suf
Use suffix .suf instead of .gz. Any suffix can be given, but suffixes other than .z and .gz should be avoided to avoid confusion when files are transferred to other systems. A null suffix forces gunzip to try decompression on all given files regardless of suffix, as in:

gunzip -S "" * (*.* for MSDOS)

Previous versions of gzip used the .z suffix. This was changed to avoid a conflict with pack(1).

-t –test
Test. Check the compressed file integrity.
-v –verbose
Verbose. Display the name and percentage reduction for each file compressed or decompressed.
-V –version
Version. Display the version number and compilation options then quit.
-# –fast –best
Regulate the speed of compression using the specified digit #, where -1 or –fast indicates the fastest compression method (less compression) and -9 or –best indicates the slowest compression method (best compression). The default compression level is -6 (that is, biased towards high compression at expense of speed).


ADVANCED USAGE

Multiple compressed files can be concatenated. In this case, gunzip will extract all members at once. For example:

gzip -c file1 > foo.gz
gzip -c file2 >> foo.gz

Then

gunzip -c foo

is equivalent to

cat file1 file2

In case of damage to one member of a .gz file, other members can still be recovered (if the damaged member is removed). However, you can get better compression by compressing all members at once:

cat file1 file2 | gzip > foo.gz

compresses better than

gzip -c file1 file2 > foo.gz

If you want to recompress concatenated files to get better compression, do:

gzip -cd old.gz | gzip > new.gz

If a compressed file consists of several members, the uncompressed size and CRC reported by the –list option applies to the last member only. If you need the uncompressed size for all members, you can use:

gzip -cd file.gz | wc -c

If you wish to create a single archive file with multiple members so that members can later be extracted independently, use an archiver such as tar or zip. GNU tar supports the -z option to invoke gzip transparently. gzip is designed as a complement to tar, not as a replacement.

Categories: Linux Commands Tags:

Linux / Unix Command: zip

February 21st, 2009 No comments

 

Source: http://linux.about.com/od/commands/l/blcmdl1_zip.htm

 

zip [-aABcdDeEfFghjklLmoqrRSTuvVwXyz!@$] [-b path] [-n suffixes] [-t mmddyyyy] [-tt mmddyyyy] [ zipfile [ file1 file2 ...]] [-xi list]

zipcloak [-dhL] [-b path] zipfile

zipnote [-hwL] [-b path] zipfile

zipsplit [-hiLpst] [-n size] [-b path] zipfile

DESCRIPTION

zip is a compression and file packaging utility for Unix, VMS, MSDOS, OS/2, Windows NT, Minix, Atari and Macintosh, Amiga and Acorn RISC OS.

 

It is analogous to a combination of the UNIX commands tar(1) and compress(1) and is compatible with PKZIP (Phil Katz’s ZIP for MSDOS systems).

A companion program (unzip(1L)), unpacks zip archives. The zip and unzip(1L) programs can work with archives produced by PKZIP, and PKZIP and PKUNZIP can work with archives produced by zip. zip version 2.3 is compatible with PKZIP 2.04. Note that PKUNZIP 1.10 cannot extract files produced by PKZIP 2.04 or zip 2.3. You must use PKUNZIP 2.04g or unzip 5.0p1 (or later versions) to extract them.

For a brief help on zip and unzip, run each without specifying any parameters on the command line.

The program is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.

The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command. Compression ratios of 2:1 to 3:1 are common for text files. zip has one compression method (deflation) and can also store files without compression. zip automatically chooses the better of the two for each file to be compressed.

When given the name of an existing zip archive, zip will replace identically named entries in the zip archive or add entries for new names. For example, if foo.zip exists and contains foo/file1 and foo/file2, and the directory foo contains the files foo/file1 and foo/file3, then:

zip -r foo foo

will replace foo/file1 in foo.zip and add foo/file3 to foo.zip. After this, foo.zip contains foo/file1, foo/file2, and foo/file3, with foo/file2 unchanged from before.

If the file list is specified as -@, [Not on MacOS] zip takes the list of input files from standard input. Under UNIX, this option can be used to powerful effect in conjunction with the find(1) command. For example, to archive all the C source files in the current directory and its subdirectories:

find . -name "*.[ch]" -print | zip source -@

(note that the pattern must be quoted to keep the shell from expanding it). zip will also accept a single dash ("-") as the zip file name, in which case it will write the zip file to standard output, allowing the output to be piped to another program. For example:

zip -r – . | dd of=/dev/nrst0 obs=16k

would write the zip output directly to a tape with the specified block size for the purpose of backing up the current directory.

zip also accepts a single dash ("-") as the name of a file to be compressed, in which case it will read the file from standard input, allowing zip to take input from another program. For example:

tar cf – . | zip backup –

would compress the output of the tar command for the purpose of backing up the current directory. This generally produces better compression than the previous example using the -r option, because zip can take advantage of redundancy between files. The backup can be restored using the command

unzip -p backup | tar xf –

When no zip file name is given and stdout is not a terminal, zip acts as a filter, compressing standard input to standard output. For example,

tar cf – . | zip | dd of=/dev/nrst0 obs=16k

is equivalent to

tar cf – . | zip – - | dd of=/dev/nrst0 obs=16k

zip archives created in this manner can be extracted with the program funzip which is provided in the unzip package, or by gunzip which is provided in the gzip package. For example:

dd if=/dev/nrst0 ibs=16k | funzip | tar xvf –

When changing an existing zip archive, zip will write a temporary file with the new contents, and only replace the old one when the process of creating the new version has been completed without error.

If the name of the zip archive does not contain an extension, the extension .zip is added. If the name already contains an extension other than .zip the existing extension is kept unchanged.

OPTIONS

-a
[Systems using EBCDIC] Translate file to ASCII format.
-A
Adjust self-extracting executable archive. A self-extracting executable archive is created by prepending the SFX stub to an existing archive. The -A option tells zip to adjust the entry offsets stored in the archive to take into account this "preamble" data.

Note: self-extracting archives for the Amiga are a special case. At present, only the Amiga port of Zip is capable of adjusting or updating these without corrupting them. -J can be used to remove the SFX stub if other updates need to be made.

-B
[VM/CMS and MVS] force file to be read binary (default is text).
-Bn
[TANDEM] set Edit/Enscribe formatting options with n defined as
bit 0: Don’t add delimiter (Edit/Enscribe)
bit 1: Use LF rather than CR/LF as delimiter (Edit/Enscribe)
bit 2: Space fill record to maximum record length (Enscribe)
bit 3: Trim trailing space (Enscribe)
bit 8: Force 30K (Expand) large read for unstructured files
-b path
Use the specified path for the temporary zip archive. For example:
zip -b /tmp stuff *
will put the temporary zip archive in the directory /tmp, copying over stuff.zip to the current directory when done. This option is only useful when updating an existing archive, and the file system containing this old archive does not have enough space to hold both old and new archives at the same time.
-c
Add one-line comments for each file. File operations (adding, updating) are done first, and the user is then prompted for a one-line comment for each file. Enter the comment followed by return, or just return for no comment.
-d
Remove (delete) entries from a zip archive. For example:
zip -d foo foo/tom/junk foo/harry/\* \*.o
will remove the entry foo/tom/junk, all of the files that start with foo/harry/, and all of the files that end with .o (in any path). Note that shell pathname expansion has been inhibited with backslashes, so that zip can see the asterisks, enabling zip to match on the contents of the zip archive instead of the contents of the current directory.
Under MSDOS, -d is case sensitive when it matches names in the zip archive. This requires that file names be entered in upper case if they were zipped by PKZIP on an MSDOS system.
-df
[MacOS] Include only data-fork of files zipped into the archive. Good for exporting files to foreign operating-systems. Resource-forks will be ignored at all.
-D
Do not create entries in the zip archive for directories. Directory entries are created by default so that their attributes can be saved in the zip archive. The environment variable ZIPOPT can be used to change the default options. For example under Unix with sh:
ZIPOPT="-D"; export ZIPOPT
(The variable ZIPOPT can be used for any option except -i and -x and can include several options.) The option -D is a shorthand for -x "*/" but the latter cannot be set as default in the ZIPOPT environment variable.
-e
Encrypt the contents of the zip archive using a password which is entered on the terminal in response to a prompt (this will not be echoed; if standard error is not a tty, zip will exit with an error). The password prompt is repeated to save the user from typing errors.
-E
[OS/2] Use the .LONGNAME Extended Attribute (if found) as filename.
-f
Replace (freshen) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive; unlike the update option (-u) this will not add files that are not already in the zip archive. For example:
zip -f foo
This command should be run from the same directory from which the original zip command was run, since paths stored in zip archives are always relative.
Note that the timezone environment variable TZ should be set according to the local timezone in order for the -f , -u and -o options to work correctly.
The reasons behind this are somewhat subtle but have to do with the differences between the Unix-format file times (always in GMT) and most of the other operating systems (always local time) and the necessity to compare the two. A typical TZ value is “MET-1MEST” (Middle European time with automatic adjustment for “summertime” or Daylight Savings Time).
-F
Fix the zip archive. This option can be used if some portions of the archive are missing. It is not guaranteed to work, so you MUST make a backup of the original archive first.
When doubled as in -FF the compressed sizes given inside the damaged archive are not trusted and zip scans for special signatures to identify the limits between the archive members. The single -F is more reliable if the archive is not too much damaged, for example if it has only been truncated, so try this option first.
Neither option will recover archives that have been incorrectly transferred in ascii mode instead of binary. After the repair, the -t option of unzip may show that some files have a bad CRC. Such files cannot be recovered; you can remove them from the archive using the -d option of zip.
-g
Grow (append to) the specified zip archive, instead of creating a new one. If this operation fails, zip attempts to restore the archive to its original state. If the restoration fails, the archive might become corrupted. This option is ignored when there’s no existing archive or when at least one archive member must be updated or deleted.
-h
Display the zip help information (this also appears if zip is run with no arguments).
-i files
Include only the specified files, as in:
zip -r foo . -i \*.c
which will include only the files that end in .c in the current directory and its subdirectories. (Note for PKZIP users: the equivalent command is
pkzip -rP foo *.c
PKZIP does not allow recursion in directories other than the current one.) The backslash avoids the shell filename substitution, so that the name matching is performed by zip at all directory levels.
Also possible:
zip -r foo . -i@include.lst
which will only include the files in the current directory and its subdirectories that match the patterns in the file include.lst.
-I
[Acorn RISC OS] Don’t scan through Image files. When used, zip will not consider Image files (eg. DOS partitions or Spark archives when SparkFS is loaded) as directories but will store them as single files.

For example, if you have SparkFS loaded, zipping a Spark archive will result in a zipfile containing a directory (and its content) while using the ‘I’ option will result in a zipfile containing a Spark archive. Obviously this second case will also be obtained (without the ‘I’ option) if SparkFS isn’t loaded.

-j
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 path).
-jj
[MacOS] record Fullpath (+ Volname). The complete path including volume will be stored. By default the relative path will be stored.
-J
Strip any prepended data (e.g. a SFX stub) from the archive.
-k
Attempt to convert the names and paths to conform to MSDOS, store only the MSDOS attribute (just the user write attribute from UNIX), and mark the entry as made under MSDOS (even though it was not); for compatibility with PKUNZIP under MSDOS which cannot handle certain names such as those with two dots.
-l
Translate the Unix end-of-line character LF into the MSDOS convention CR LF. This option should not be used on binary files. This option can be used on Unix if the zip file is intended for PKUNZIP under MSDOS. If the input files already contain CR LF, this option adds an extra CR. This ensure that unzip -a on Unix will get back an exact copy of the original file, to undo the effect of zip -l.
-ll
Translate the MSDOS end-of-line CR LF into Unix LF. This option should not be used on binary files. This option can be used on MSDOS if the zip file is intended for unzip under Unix.
-L
Display the zip license.
-m
Move the specified files into the zip archive; actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous so it is recommended to use it in combination with -T to test the archive before removing all input files.
-n suffixes
Do not attempt to compress files named with the given suffixes. Such files are simply stored (0% compression) in the output zip file, so that zip doesn’t waste its time trying to compress them. The suffixes are separated by either colons or semicolons. For example:
zip -rn .Z:.zip:.tiff:.gif:.snd foo foo
will copy everything from foo into foo.zip, but will store any files that end in .Z, .zip, .tiff, .gif, or .snd without trying to compress them (image and sound files often have their own specialized compression methods). By default, zip does not compress files with extensions in the list .Z:.zip:.zoo:.arc:.lzh:.arj. Such files are stored directly in the output archive. The environment variable ZIPOPT can be used to change the default options. For example under Unix with csh:
setenv ZIPOPT "-n .gif:.zip"
To attempt compression on all files, use:
zip -n : foo
The maximum compression option -9 also attempts compression on all files regardless of extension.
On Acorn RISC OS systems the suffixes are actually filetypes (3 hex digit format). By default, zip does not compress files with filetypes in the list DDC:D96:68E (i.e. Archives, CFS files and PackDir files).
-N
[Amiga, MacOS] Save Amiga or MacOS filenotes as zipfile comments. They can be restored by using the -N option of unzip. If -c is used also, you are prompted for comments only for those files that do not have filenotes.
-o
Set the "last modified" time of the zip archive to the latest (oldest) "last modified" time found among the entries in the zip archive. This can be used without any other operations, if desired. For example:
zip -o foo
will change the last modified time of foo.zip to the latest time of the entries in foo.zip.
-P password
use password to encrypt zipfile entries (if any). THIS IS INSECURE! Many multi-user operating systems provide ways for any user to see the current command line of any other user; even on stand-alone systems there is always the threat of over-the-shoulder peeking. Storing the plaintext password as part of a command line in an automated script is even worse. Whenever possible, use the non-echoing, interactive prompt to enter passwords. (And where security is truly important, use strong encryption such as Pretty Good Privacy instead of the relatively weak encryption provided by standard zipfile utilities.)
-q
Quiet mode; eliminate informational messages and comment prompts. (Useful, for example, in shell scripts and background tasks).
-Qn
[QDOS] store information about the file in the file header with n defined as
bit 0: Don’t add headers for any file
bit 1: Add headers for all files
bit 2: Don’t wait for interactive key press on exit
-r
Travel the directory structure recursively; for example:
zip -r foo foo
In this case, all the files and directories in foo are saved in a zip archive named foo.zip, including files with names starting with ".", since the recursion does not use the shell’s file-name substitution mechanism. If you wish to include only a specific subset of the files in directory foo and its subdirectories, use the -i option to specify the pattern of files to be included. You should not use -r with the name ".*", since that matches ".." which will attempt to zip up the parent directory (probably not what was intended).
-R
Travel the directory structure recursively starting at the current directory; for example:
zip -R foo ‘*.c’
In this case, all the files matching *.c in the tree starting at the current directory are stored into a zip archive named foo.zip. Note for PKZIP users: the equivalent command is
pkzip -rP foo *.c
-S
[MSDOS, OS/2, WIN32 and ATARI] Include system and hidden files.
[MacOS] Includes finder invisible files, which are ignored otherwise.
-t mmddyyyy
Do not operate on files modified prior to the specified date, where mm is the month (0-12), dd is the day of the month (1-31), and yyyy is the year. The ISO 8601 date format yyyy-mm-dd is also accepted. For example:
zip -rt 12071991 infamy foo

zip -rt 1991-12-07 infamy foo

will add all the files in foo and its subdirectories that were last modified on or after 7 December 1991, to the zip archive infamy.zip.
-tt mmddyyyy
Do not operate on files modified after or at the specified date, where mm is the month (0-12), dd is the day of the month (1-31), and yyyy is the year. The ISO 8601 date format yyyy-mm-dd is also accepted. For example:
zip -rtt 11301995 infamy foo

zip -rtt 1995-11-30 infamy foo

will add all the files in foo and its subdirectories that were last modified before the 30 November 1995, to the zip archive infamy.zip.
-T
Test the integrity of the new zip file. If the check fails, the old zip file is unchanged and (with the -m option) no input files are removed.
-u
Replace (update) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive. For example:
zip -u stuff *
will add any new files in the current directory, and update any files which have been modified since the zip archive stuff.zip was last created/modified (note that zip will not try to pack stuff.zip into itself when you do this).
Note that the -u option with no arguments acts like the -f (freshen) option.
-v
Verbose mode or print diagnostic version info.
Normally, when applied to real operations, this option enables the display of a progress indicator during compression and requests verbose diagnostic info about zipfile structure oddities.
When -v is the only command line argument, and stdout is not redirected to a file, a diagnostic screen is printed. In addition to the help screen header with program name, version, and release date, some pointers to the Info-ZIP home and distribution sites are given. Then, it shows information about the target environment (compiler type and version, OS version, compilation date and the enabled optional features used to create the zip executable.
-V
[VMS] Save VMS file attributes. zip archives created with this option will generally not be usable on other systems.
-w
[VMS] Append the version number of the files to the name, including multiple versions of files. (default: use only the most recent version of a specified file).
-x files
Explicitly exclude the specified files, as in:
zip -r foo foo -x \*.o
which will include the contents of foo in foo.zip while excluding all the files that end in .o. The backslash avoids the shell filename substitution, so that the name matching is performed by zip at all directory levels.
Also possible:
zip -r foo foo -x@exclude.lst
which will include the contents of foo in foo.zip while excluding all the files that match the patterns in the file exclude.lst.
-X
Do not save extra file attributes (Extended Attributes on OS/2, uid/gid and file times on Unix).
-y
Store symbolic links as such in the zip archive, instead of compressing and storing the file referred to by the link (UNIX only).
-z
Prompt for a multi-line comment for the entire zip archive. The comment is ended by a line containing just a period, or an end of file condition (^D on UNIX, ^Z on MSDOS, OS/2, and VAX/VMS). The comment can be taken from a file:
zip -z foo < foowhat
-#
Regulate the speed of compression using the specified digit #, where -0 indicates no compression (store all files), -1 indicates the fastest compression method (less compression) and -9 indicates the slowest compression method (optimal compression, ignores the suffix list). The default compression level is -6.
-!
[WIN32] Use priviliges (if granted) to obtain all aspects of WinNT security.
-@
Take the list of input files from standard input. Only one filename per line.
-$
[MSDOS, OS/2, WIN32] Include the volume label for the the drive holding the first file to be compressed. If you want to include only the volume label or to force a specific drive, use the drive name as first file name, as in:
zip -$ foo a: c:bar


EXAMPLES

The simplest example:

zip stuff *

creates the archive stuff.zip (assuming it does not exist) and puts all the files in the current directory in it, in compressed form (the .zip suffix is added automatically, unless that archive name given contains a dot already; this allows the explicit specification of other suffixes).

Because of the way the shell does filename substitution, files starting with "." are not included; to include these as well:

zip stuff .* *

Even this will not include any subdirectories from the current directory.

To zip up an entire directory, the command:

zip -r foo foo

creates the archive foo.zip, containing all the files and directories in the directory foo that is contained within the current directory.

You may want to make a zip archive that contains the files in foo, without recording the directory name, foo. You can use the -j option to leave off the paths, as in:

zip -j foo foo/*

If you are short on disk space, you might not have enough room to hold both the original directory and the corresponding compressed zip archive. In this case, you can create the archive in steps using the -m option. If foo contains the subdirectories tom, dick, and harry, you can:

zip -rm foo foo/tom
zip -rm foo foo/dick
zip -rm foo foo/harry

where the first command creates foo.zip, and the next two add to it. At the completion of each zip command, the last created archive is deleted, making room for the next zip command to function.

PATTERN MATCHING

This section applies only to UNIX. Watch this space for details on MSDOS and VMS operation.

The UNIX shells (sh(1) and csh(1)) do filename substitution on command arguments. The special characters are:

?
match any single character
*
match any number of characters (including none)
[]
match any character in the range indicated within the brackets (example: [a-f], [0-9]).

When these characters are encountered (without being escaped with a backslash or quotes), the shell will look for files relative to the current path that match the pattern, and replace the argument with a list of the names that matched.

The zip program can do the same matching on names that are in the zip archive being modified or, in the case of the -x (exclude) or -i (include) options, on the list of files to be operated on, by using backslashes or quotes to tell the shell not to do the name expansion. In general, when zip encounters a name in the list of files to do, it first looks for the name in the file system. If it finds it, it then adds it to the list of files to do. If it does not find it, it looks for the name in the zip archive being modified (if it exists), using the pattern matching characters described above, if present. For each match, it will add that name to the list of files to be processed, unless this name matches one given with the -x option, or does not match any name given with the -i option.

The pattern matching includes the path, and so patterns like \*.o match names that end in ".o", no matter what the path prefix is. Note that the backslash must precede every special character (i.e. ?*[]), or the entire argument must be enclosed in double quotes ("").

In general, use backslash to make zip do the pattern matching with the -f (freshen) and -d (delete) options, and sometimes after the -x (exclude) option when used with an appropriate operation (add, -u, -f, or -d).

Categories: Linux Commands Tags:

Linux / Unix rmdir command (remove – delete)

February 16th, 2009 No comments

 

Source: http://www.computerhope.com/unix/urmdir.htm

 

About rmdir

Deletes a directory.

Syntax

rmdir [OPTION]… DIRECTORY…

–ignore-fail-on-non-empty
ignore each failure that is solely because a directory is non-empty.

-p, –parents
Remove DIRECTORY and its ancestors. E.g., `rmdir -p a/b/c’ is similar to `rmdir a/b/c a/b a’.

-v, –verbose
output a diagnostic for every directory processed.

–version
output version information and exit.

Examples

rmdir mydir – removes the directory mydir

rm -r directory – would remove a directory, even if files existed in that directory.

Related commands

rm

Categories: Linux Commands Tags:

Linux / Unix rm command (remove – delete)

February 16th, 2009 No comments

 

Source: http://www.computerhope.com/unix/urm.htm

 

About rm

Deletes a file without confirmation (by default).

Syntax

rm [-f] [-i] [-R] [-r] [filenames | directory]

-f
Remove all files (whether write-protected or not) in a directory without prompting the user. In a write-protected directory, however, files are never removed (whatever their permissions are), but no messages are displayed. If the removal of a write-protected directory is attempted, this option will not suppress an error message.

-i
Interactive. With this option, rm prompts for confirmation before removing any files. It over- rides the -f option and remains in effect even if the standard input is not a terminal.

-R
Same as -r option.

-r
Recursively remove directories and subdirectories in the argument list. The directory will be emptied of files and removed. The user is normally prompted for removal of any write-protected files which the directory contains. The write-protected files are removed without prompting, however, if the -f option is used, or if the standard input is not a terminal and the -i option is not used.  Symbolic links that are encountered with this option will not be traversed.  If the removal of a non-empty, write-protected directory is attempted, the utility will always fail (even if the -f option is used), resulting in an error message.

filenames
A path of a filename to be removed.

Examples

rm myfile.txt

Remove the file myfile.txt without prompting the user.

rm -r directory

Remove a directory, even if files existed in that directory.  

Note that if you use rm to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using shred.

Categories: Linux Commands Tags:

Linux / Unix shred command

February 16th, 2009 No comments

 

Source: http://www.computerhope.com/unix/shred.htm

 

About shred

Delete a file securely, first overwriting it to hide its contents.

Syntax

shred [OPTIONS] FILE [...]

-f, –force
Change permissions to allow writing if necessary.

-n, –iterations=N
Overwrite N times instead of the default (25)

-s, –size=N
shred this many bytes (suffixes like K, M, G accepted)

-u, –remove
truncate and remove file after overwriting

-v, –verbose
show progress

-x, –exact
do not round file sizes up to the next full block; this is the default for non-regular files

-z, –zero
add a final overwrite with zeros to hide shredding

-
shred standard output

–help
display this help and exit

–version
output version information and exit

Examples

shred myfile.txt

Permanently deleted and makes the file myfile.txt unrecoverable (unless stored elsewhere or on a backup).

Categories: Linux Commands Tags:

Linux / Unix find command

January 20th, 2009 No comments

 

Source: http://www.computerhope.com/unix/ufind.htm 

 

About find

Finds one or more files assuming that you know their approximate filenames.

Syntax

find path expressions

path
A path name of a starting point in the directory hierarchy.

-atime n
True if the file was accessed n days ago. The access time of directories in path is changed by find itself.

-cpio device
Always true; write the current file on device in cpio format (5120-byte records).

-ctime n
True if the file’s status was changed n days ago.

-depth
Always true; causes descent of the directory hierarchy to be done so that all entries in a directory are acted on before the directory itself. This can be useful when find is used with cpio to transfer files that are contain edin directories without write permission.

-exec command
True if the executed command returns a zero value as exit status. The end of command must be punctuated by an escaped semicolon. A command argument {} is replaced by the current path name.

-follow
Always true; causes symbolic links to be followed. When following symbolic links, find keeps track of the directories visited so that it can detect infinite loops; for example, such a loop would occur if a symbolic link pointed to an ancestor. This expression should not be used with the -type l expression.

-fstype type
True if the filesystem to which the file belongs is of type type .

-group gname
True if the file belongs to the group gname. If gname is numeric and does not appear in the /etc/group file, it is taken as a group ID.

-inum n
True if the file has inode number n.

-links
True if the file has n links.

-local
True if the file system type is not a remote file system type as defined in the /etc/dfs/fstypes file. nfsis used as the default remote filesystem type if the/etc/dfs/fstypes file is not present.

-ls
Always true; prints current path name together
with its associated statistics. These include
(respectively):

  • inode number
  • size in kilobytes (1024 bytes)
  • protection mode
  • number of hard links
  • user
  • group
  • size in bytes
  • modification time.

If the file is a special file the size field will instead contain the major and minor device numbers.

If the file is a symbolic link the pathname of the linked-to file is printed preceded by `->’. The format is identical to that of ls -gilds ls Note: Formatting is done internally, without executing the ls program.

-mount
Always true; restricts the search to the file system containing the directory specified. Does not list mount points to other file systems.

-mtime n
True if the file’s data was modified n days ago.

-name pattern
True if pattern matches the current file name.  Normal shell file name generation characters (see sh) may be used. A backslash (\) is used as an escape character within the pattern. The pattern should be escaped or quoted when find is invoked from the shell.

-ncpio device
Always true; write the current file on device in cpio -c format (5120 byte records).

-newer file
True if the current file has been modified more recently than the argument file.

-nogroup
True if the file belongs to a group not in the /etc/group file.

-nouser
True if the file belongs to a user not in the /etc/passwd file.

-ok command
Like -exec except that the generated command line is printed with a question mark first, and is executed only if the user responds by typing y.

-perm [-]mode
The mode argument is used to represent file mode bits. It will be identical in format to the <symbolicmode> operand described in chmod, and will be interpreted as follows. To start, a template will be assumed with all file mode bits cleared. An op symbol of:

+
will set the appropriate mode bits in the template;

-
will clear the appropriate bits;

=
will set the appropriate mode bits, without regard to the contents of process’ file mode creation mask.

The op symbol of – cannot be the first character of mode; this avoids ambiguity with the optional leading hyphen. Since the initial mode is all bits off, there are not any symbolic modes that need to use – as the first character.

If the hyphen is omitted, the primary will evaluate as true when the file permission bits exactly match the value of the resulting template.

Otherwise, if mode is prefixed by a hyphen, the primary will evaluate as true if at least all the bits in the resulting template are set in the file permission bits.

-perm [-]onum
True if the file permission flags exactly match the octal number onum see chmod). If onum is prefixed by a minus sign (-), only the bits that are set in onum are compared with the file permission flags, and the expression evaluates true if they match.

-print
Always true; causes the current path name to be printed.

-prune
Always yields true. Do not examine any directories or files in the directory structure below the pattern just matched. If -depth is specified, -prune will have no effect.

-size n[c]
True if the file is n blocks long (512 bytes per block). If n is followed by a c, the size is in
bytes.

-type c
True if the type of the file is c, where c is b, c, d, D, f, l, p, or s for block special file, character special file, directory, door, plain file, symbolic link, fifo (named pipe), or socket, respectively.

-user uname
True if the file belongs to the user uname . If uname is numeric and does not appear as a login name in the /etc/passwd file, it is taken as a user ID.

-xdev
Same as the -mount primary.

When using find to determine files modified within a range of time, one must use the ?time argument before the -print argument; otherwise, find will give all files.

Examples

find -name ‘mypage.htm’

In the above command the system would search for any file named mypage.htm in the current directory and any subdirectory.

find / -name ‘mypage.htm’

In the above example the system would search for any file named mypage.htm on the root and all subdirectories from the root.

find -name ‘file*’

In the above example the system would search for any file beginning with file in the current directory and any subdirectory.

find -name ‘*’ -size +1000k

In the above example the system would search for any file that is larger then 1000k.

 

 

Source : http://www.linux.ie/newusers/beginners-linux-guide/find.php 

 

finder-keepers.

In it’s simplest use the find command searches for files in the current directory and its subdirectories:

$ find .
./tp1301.txt
./up1301.txt
./tp1302.txt
./up1302.txt
./Up1303.txt
./misc/uploads
./misc/uploads/patch12_13.diff

As always, the dot indicates the current directory. Here find has listed all files found in the current directory and its subdirectories.

If we only want to find files with ‘up’ at the start of their name, we use the ‘-name’ argument.

So the following would be used:

$ find . -name up\*
./up1301.txt
./up1302.txt
./misc/uploads

find defaults to being case sensitive. If we want the find utility to locate the file ‘Up1303.txt’ we could either do ‘find -name Up\*‘ or use the iname argument instead of the name argument.

The wildcard character is escaped with a slash so BASH sends a literal asterisk to the find utility as an argument instead of performing filename expansion and passing any number of files in as arguments.

This ‘gotcha’ is important. Be aware of the characters which the shell attaches special meaning to.

Now we know there are files that should have their names in lowercase we can utilise find to get a list of files with names that aren’t:

$ find -iname up\* -not -name up\*

Smooth Operator

find supports boolean algebra with the -and, -or and -not arguments. These are abbreviated as -a, -o and ! (which in bash must be escaped as \!) respectively. The and operator is mentioned here for completeness. Its presence is implied:

$ find . -iname david\*gray\*ogg -type f > david_gray.m3u

These operators are processed in the following order:

Parentheses

Use parentheses to force the order in which the operators are evaluated.

-not

Invert the result of the tested expression.

-and

E.g. ex1 -and ex2; the second expression isn’t checked if the first evaluated to true

-or

E.g. ex1 -or ex2; as with -AND, the second expression isn’t checked if the first evaluated to true

‘,’

This is the list operator where unlike the ‘-AND’ and ‘-OR’ operators both expressions are evaluated. Read the ’2 into 1 does go’ section for more information.

The example in the Smooth Operator boxout creates an m3u playlist listing all ogg files that start ‘David Gray’ (and all case-permutations)

$ find . -iname david\ gray\*ogg -type f > david_gray.m3u

This will find any files called, in one way or the other, "david gray….ogg".

This is semantically equivalent to:

$ find . -iname david\ gray\*ogg -and -type f > david_gray.m3u

It’s equivalent to:

$ find . -iname "david gray*ogg" -and -type f > david_gray.m3u

What if the ogg files themselves mightn’t have the artists name in them and are in some subdirectory of one called ‘David Gray’, how do we find them?

$ find . -ipath \*david\ gray\*ogg -type f > david_gray.m3u

The expression starts with a wildcard because its possible there’s more than one subdirectory named ‘david gray’ that might really be nothing more than symlinks for categorisations.

Here’s another example, we list the contents of the humour directory (one line per file) and do a case-insensitive search for .mp3 files with ‘yoda’ in the name of the file:

$ ls humour -1
Weird Al - Yoda.mp3
welcome_to_the_internet_helpdesk.mp3
werid al - livin' la vida yoda.mp3

$ find -ipath \*humour\*yoda\* -type f
./humour/Weird Al - Yoda.mp3
./humour/werid al - livin' la vida yoda.mp3

2 into 1 does go

As implied in the Smooth Operator boxout, it’s possible to have one invocation of find perform more than one task.

To compile two lists, one containing the names of all .php files and the other the names of all .js files use:

$ find ~ -type f \( -name \*.php -fprint php_files ,
                    -name \*.js -fprint javascript_files \)

Pruning

Suppose you have a playlist file listing all David Gray .ogg files but there are a few albums you don’t want included.

You can prevent those albums from going into the playlist by using the -prune action which works by attempting to match the names of directories against the given expression.

This example excludes the Flesh and Lost Songs albums :

$ find   \( -path  ./mp3/David_Gray/Flesh\* -o -path
"./mp3/David_Gray/Lost Songs" \* \) -prune -o -ipath \*david\ gray\*

The first thing you’ll notice here is the parentheses are escaped out so BASH doesn’t misinterpret them. Notice using -prune takes the form

"don’t look for these, look for these other ones instead". ie:

$ find (-path <don't want this> -o -path <don't want this#2>)
\-prune -o -path <global expression for what I do want>

It might take a bit longer to invoke find to use the -prune action: decide exactly what you want to do first. I find using the -prune action saves me time I can use on other tasks.

Fussy Fozzy!

There’s a host of other expressions and criteria that can be used with find.

Here is a brief rundown on the ones you’ll most likely want to use:

-nouser

file is owned by someone no longer listed in /etc/passwd

-nogroup

the group the file belongs to is no longer listed in /etc/groups

-owner <username>

file is owned by specified user.

We’ll delve into using these, and others, later on.

Print me the way you want me, baby!

Changing the output information

If you want more than just the names of the files displayed, find’s -printf action lets you have just about any type of information displayed. Looking at the man page there is a startling array of options.

These are used the most:

%p

filename, including name(s) of directory the file is in

%m

permissions of file, displayed in octal.

%f

displays the filename, no directory names are included

%g

name of the group the file belongs to.

%h

display name of directory file is in, filename isn’t included.

%u

username of the owner of the file

As an example:

$ find . -name \*.ogg -printf %f\\n

generates a list of the filenames of all .ogg files in and under the current directory.

The ‘double backslash n’ is important; ‘\n’ indicates the start of a new line. The single backslash needs to be escaped by another one so the shell doesn’t take it as one of its own.

Where to output information?

find has a set of actions that tell it to write the information to any file you wish. These are the -fprint, -fprint0 and -fprintf actions.

Thus

$ find . -iname david\ gray\*ogg -type f -fprint david_gray.m3u

is more efficient than

$ find . -iname david\ gray\*ogg -type f > david_gray.m3u

Execute!

File is an excellent tool for generating reports on basic information regarding files, but what if you want more than just reports? You could just pipe the output to some other utility:

$ find ~/oggs/ -iname \*.mp3 | xargs rm

This isn’t all that efficient though.

It is much better to use the -exec action:

$ find ~/oggs/ -iname \*.mp3 -exec rm {} \;

It mightn’t read as well, but it does mean the files are immediately deleted once found.

‘{}’ is a placeholder for the name of the file that has been found and as we want BASH to ignore the semicolon and pass it verbatim to find we have to escape it.

To be cautious, the -ok action can be used instead of -exec. The -ok action means you’ll be asked for confirmation before the command is executed.

There are many ways these can be used in ‘real life’ situations:

If you are locked out from the default Mozilla profile, this will unlock you:

$ find ~/.mozilla -name lock -exec rm {} \;

To compress .log files on an individual basis:

$ find . -name \*.log -exec bzip {} \;

Give user ken ownership of files that aren’t owned by any current user:

$ find . -nouser -exec chown ken {} \;

View all .dat files that are in the current directory with vim. Don’t search any subdirectories.

$ vim -R `find . -name \*.dat -maxdepth 1`

Look for directories called CVS which are at least four levels below the current directory:

$ find -mindepth 4 -type d -name CVS 

Time waits for no-one

You might want to search for recently created files, or grep through the last 3 days worth of log files.

Find comes into its own here: it can limit the scope of the files found according to timestamps.

Now, suppose you want to see what hidden files in your home directory changed in the last 5 days:

$ find ~ -mtime -5 -name \.\*

If you know something has changed much more recently than that, say in the last 14 minutes, and want to know what it was there’s the mmin argument:

$ find ~ -mmin 14 -name \.\*

Be aware that doing a ‘ls’ will affect the access time-stamps of the files shown by that action. If you do an ls to see what’s in a directory and try the above to see what files were accessed in the last 14 minutes all files will be listed by find.

To locate files that have been modified since some arbitrary date use this little trick:

$ touch -d "13 may 2001 17:54:19" date_marker
$ find . -newer date_marker 

To find files created before that date, use the cnewer and negation conditions:

$ find . \! -cnewer date_marker

To find a file which was modified yesterday, but less than 24 hours ago:

$ find . -daystart -atime 1 -maxdepth

The -daystart argument means the day starts at the actual beginning of the day, not 24 hours ago.

This argument has meaning for the -amin, -atime, -cmin, ctime, -mmin and -mtime options.

Finding files of a specific size

A file of character (bytes)

To locate files that have a certain amount of characters present then you can’t go far wrong with

# find files with exactly 1000 characters
$ find . -size 1000c
#find files containing between 600 to 700 characters, inclusive.
$ find . -size +599c -and -size -701c       

‘Characters’ is a misnomer: ‘c’ is find’s shorthand for bytes; thus this will only work for ASCII text not Unicode.

Consulting the man page we see

c = bytes

w = 2 byte words

k = kilobytes

b = 512-byte blocks

Thus we can use find to list files of a certain size:

$ find /usr/bin -size 48k
Empty files

You can find empty files with $ find . -size 0c

Using the -empty argument is more efficient.

To delete empty files in the current directory:

$ find . -empty -maxdepth 1 -exec rm {} \;

Users & Groupies

Users

To locate files belonging to a certain user:

# find /etc -type f \!  -user root -exec ls -l {} \;
-rw------- 1 lp sys 19731 2002-08-23 15:04 /etc/cups/cupsd.conf
-rw------- 1 lp sys    97 2002-07-26 23:38 /etc/cups/printers.conf

A subset of that same information, without having the cost of an exec:

root@ttyp0[etc]# find /etc -type f \!  -user root \
                 -printf "%h/%f %u\\n"
/etc/cups/cupsd.conf lp
/etc/cups/printers.conf lp

If you know the uid and not the username then use the -uid argument:

$ find /usr/local/htdocs/www.linux.ie/ -uid 401

-nouser means there is no user in the /etc/passwd file for the files in question.

Groupies

find can locate files that belong to a specific group – or not, depending on how you use it.

This is especially suited to tracking down files that should belong to the www group but don’t:

$ find /www/ilug/htdocs/  -type f \! -group  www

The -nogroup argument means there is no group in the /etc/group file for the files in question.

This may arise if a group is removed from the /etc/group file sometime after it’s been used.

To search for files by the numerical group ID use the -gid argument:

$ find -gid 100

Permissions

If you’ve ever had one or more shell scripts not work because their execute bits weren’t set and want to sort things out for once and for all, then you should like this little example:

knoppix@ttyp1[bin]$ ls -l ~/bin/
total 8
-rwxr-xr-x    1 knoppix  knoppix	21 2004-01-20 21:42 wl
-rw-r--r--    1 knoppix  knoppix	21 2004-01-20 21:47 ww

knoppix@ttyp1[bin]$ find ~/bin/ -maxdepth 1 -perm 644 -type f \
                    -not -name .\*
/home/knoppix/bin/ww

Find locates the file that isn’t set to execute, as we can see from the output of ls.

Types of files

The ‘-type’ argument obviously specifies what type of file find is to go looking for (remember in Linux absolutely everything is represented as some type of file).

So far I’ve been using ‘-type f’ which means search for normal files.

If we want to locate directories with ‘_of_’ in their name we’d use:

$ find . -type d -name '*_of_*'

The list generated by this won’t include symbolic links to directories.

To get a list including directories and symbolic links:

$ find . \( -type d -or -type l \) -name '*_of_*'

For a complete list of types check the man page.

Regular expressions

Thus far we’ve been using casual wildcards to specify certain groups of files. Find also support regular expressions, so we can use more advanced criteria with regards to locating files. The matching expression must apply to the entire path:

ken@gemmell:/home/library/code$ find . -regex '.*/mp[0-4].*'
./library/sql/mp3_genre_types.sql

The -regex test has a case insensitive counterpart, -iregex.

There is a little gotcha with using regular expressions: You must allow for the full path of the files found, even if find is to search the current directory:

$ cd /usr/share/doc/samba-doc/htmldocs/using_samba
$ find . -regex './ch0[1-2]_0[1-3].*'
./ch01_01.html
./ch01_02.html
./ch02_01.html
./ch02_02.html
./ch02_03.html

Limiting by filesytem

As an experiment, get a MS formatted floppy disk and mount it as root:

$ su -
# mount /floppy
# mount
/dev/sda2 on / type ext2 (rw,errors=remount-ro)
proc on /proc type proc (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/fd0 on /floppy type msdos (rw,noexec,nosuid,nodev)

Now try

$ find / -fstype msdos -maxdepth 1 

You should see only /floppy listed.

To get the reverse of this, ie a listing of directories that are not on msdos file-systems, use

$ find / -maxdepth 1 \( -fstype msdos \) -prune -or -print

This is a start on limiting the files found by system type.

Summary

I’ve covered the vast majority of ways to use the find utility, but not absolutely everything. If you’ve any questions please don’t hesitate in emailing me

Categories: Linux Commands Tags: