Linux has many commands available which can be very useful. Even it is very powerful when it gets all the available parameters along with the commands. Due to the varsity of the commands available in the Linux, it’s difficult to remember all the commands.
How to use –h or –help?
Launch the terminal by pressing Ctrl+ Alt+ T or just click on the terminal icon in the taskbar. Simply type your command whose usage you to know in the terminal with –h or –help after space and press enter. And you’ll get the complete usage of that command as shown below.
help displays brief summaries of shell builtin commands. If PATTERN is specified, gives detailed help on all commands matching PATTERN, otherwise the list of help topics is printed.
help [-dms] [PATTERN ...]
Output short description for each topic.
Display usage in pseudo-manpage format.
Output only a short usage synopsis for each topic matching.
Display a brief description of the builtin shell command echo.
Linux man command
The “man” is a short term for manual page. In unix like operating systems such as linux, man is an interface to view the system’s reference manual.
A user can request to display a man page by simply typing man followed by a space and then argument. Here its argument can be a command, utility or function. A manual page associated with each of these arguments is displayed.
If you will provide a section number in the command, then man will be directed to look into that section number of the manual and that section page will be displayed. And if not, then by default it will display the first page and you have to go through the entire sections in a pre-defined manner.
We’ll read about section number in this tutorial.
Syntax of man:
man [option(s)] keyword(s)
But generally [option(s)] are not used. Only keyword is written as an argument.
This command will display all the information about ‘ls’ command as shown in the screenshot.
info command in Linux with Examples
infocommand reads documentation in the info format. It will give detailed information for a command when compared with the man page. The pages are made using the texinfo tools because of which it can link with other pages, create menus and easy navigation.
info [OPTION]... [MENU-ITEM...]
-a, –all: It use all matching manuals.
-k, –apropos=STRING: It look up STRING in all indices of all manuals.
-d, –directory=DIR: It add DIR to INFOPATH.
-f, –file=MANUAL: It specify Info manual to visit.
-h, –help: It display this help and exit.
-n, –node=NODENAME: It specify nodes in first visited Info file.
-o, –output=FILE: It output selected nodes to FILE.
-O, –show-options, –usage: It go to command-line options node.
-v, –variable VAR=VALUE: It assign VALUE to Info variable VAR.
–version: It display version information and exit.
-w, –where, –location: It print physical location of Info file.
-a : It use all matching manuals and displays them for a particular command. info -a cvs
Linux commands and their optional parameter are very useful for the user who wants to take advance of the full feature of Linux CLI. In this post, we have discussed the most featured commands are being used extensively in day-to-day activities.
To explore more about commands you can explore all the commands available in using the Linux Inbuild self-help commands (help or man or info) available, Which will give the briefing about the options and uses by precise examples.
The alias command is a way to keep your command /commands in a temporary variable. Unix commands using a shorter name than those that are usually associated with such commands.
Syntax: alias shortName="your custom command here"
Example: alias wr=”cd /var/www/html”
APT (Advanced Package Tool) is the command line tool to interact with this packaging system. There is already dpkg commands to manage it. But apt is more friendly way to handle packaging. You can use it to find and install new packages, upgrade packages, clean the packages etc.
awk command searches files for text containing a pattern. When a line or text matches, awk performs a specific action on that line/text. The Program statement tells awk what operation to do; Program statement consists of a series of “rules” where each rule specifies one pattern to search for, and one action to perform when a particular pattern is found. A regular expression enclosed in slashes (/) is an awk pattern to match every input record whose text belongs to that set.
A portable, fast, open source program that compresses and decompresses files at a high rate, but that does not archive them.
Syntax: bzip2 option(s) filenames
Example: bzip2 -z backup.tar
A Unix/Linux command that can read, modify or concatenate text files. The cat command also displays file contents.
Syntax: cat [OPTION] [FILE]...
Example: cat /etc/passwd cat test test1
The cd command changes the current directory in Linux and can conveniently toggle between directories. The Linux cd command is similar to the CD and CHDIR commands in MS-DOS.
Syntax: cd [-L | -P [-e]] directory
Example: cd documents/work/accounting
Options L Force symbolic links to be followed. In other words, if you tell cd to move into a "directory", which is actually a symbolic link to a directory, it moves into the directory the symbolic link points to. This option is the default behavior of cd; normally, it will always act as if -L has been specified. -P Use the physical directory structure without following symbolic links. In other words, only change into the specified directory if it actually exists as named; symbolic links will not be followed. This option is the opposite of the -L option, and if they are both specified, this option will be ignored. -e If the -P option is specified, and the current working directory cannot be determined, this option tells cd to exit with an error. If -P is not specified along with this option, this option has no function.
The chmod command changes the permissions of one or more files. Only the file owner or a privileged user can change the access mode.
[reference] Reference Class Description u owner file's owner g group users who are members of the file's group o others users who are neither the file's owner nor members of the file's group a all All three of the above, same as ugo
[operator] Operator Description + Adds the specified modes to the specified classes - Removes the specified modes from the specified classes = The modes specified are to be made the exact modes for the specified classes
[mode] Mode Permission r Permission to read the file. w Permission to write (or delete) the file. x Permission to execute the file, or, in the case of a directory, search it.
[mode] 4 Permission to read the file. 2 Permission to write (or delete) the file. 1 Permission to execute the file, or, in the case of a directory, search it. 0 No Permission
So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1(read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute).
chmod u=rwx,g=rx,o=r myfile chmod 754 myfile
The chown prompt changes file or group ownership. It gives admins the option to change ownership of all the objects within a directory tree, as well as the ability to view information on the objects processed.
The cmp utility compares two files of any type and writes the results to the standard output. By default, cmp is silent if the files are the same. If they differ, cmp reports the byte and line number where the first difference occurred.
[OPTION] -b --print-bytes Print differing bytes. -i SKIP --ignore-initial=SKIP Skip the first SKIP bytes of input. -i SKIP1:SKIP2 --ignore-initial=SKIP1:SKIP2 Skip the first SKIP1 bytes of FILE1 and the first SKIP2 bytes of FILE2. -l --verbose Output byte numbers and values of all differing bytes. -n LIMIT --bytes=LIMIT Compare at most LIMIT bytes. -s --quiet --silent Output nothing; yield exit status only. -v --version Output version info. --help Output this help. SKIP1 and SKIP2 are the number of bytes to skip in each file. SKIP values may be followed by the following multiplicative suffixes: kB 1000, K 1024, MB 1,000,000, M 1,048,576, GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y. If a FILE is '-' or missing, read standard input.
The cpio command copies files into or out of a cpio or tar archive. A tar archive is a file that contains other files, plus information about them, such as their file name, owner, timestamps and access permissions. The archive can be another file on the disk, a magnetic tape or a pipe. It also has three operating modes: copy-out, copy-in and copy-pass. It is also a more efficient alternative to tar.
GNUcpio is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files.
The following archive formats are supported: binary, old ASCII, new ASCII, CRC, HPUXbinary, HPUX old ASCII, old tar, and POSIX.1 tar. The tar format is provided for compatibility with the tar program. By default, cpio creates binary format archives, for compatibility with older cpio programs. When extracting from archives, cpio automatically recognizes which kind of archive it is reading and can read archives created on machines with a different byte-order.
Copy-Out Mode Syntax:
In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input and writes the archive onto the standard output. A typical way to generate the list of filenames is with the find command; you should give find the -depth option to minimize problems with permissions on directories that are unreadable. Copy-Out mode syntax:
In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input. Any non-option command lineargumentsare shellglobbing patterns; only files in the archive whose names match one or more of those patterns are copied from the archive. Unlike in the shell, an initial ‘.‘ in a filename does match a wildcard at the start of a pattern, and a ‘/‘ in a filename can match wildcards. If no patterns are given, all files are extracted. Copy-In mode syntax:
In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument. Copy-Pass mode syntax:
Read a list of filenames terminated by a null character, instead of a newline, so that files whose names contain newlines can be archived. GNU find is one way to produce a list of null-terminated filenames. This option may be used in copy-out and copy-pass modes.
Reset the access times of files after reading them, so that it does not look like they have just been read.
Append to an existing archive. Only works in copy-out mode. The archive must be a disk file specified with the -O or -F (-file) option.
Swap both halfwords of words and bytes of halfwords in the data. Equivalent to -sS. This option may be used in copy-in mode. Use this option to convert 32-bitintegers between big-endian and little-endian machines.
Set the I/Oblock size to 5120 bytes. Initially the block size is 512 bytes.
Set the I/O block size to BLOCK-SIZE * 512 bytes.
Identical to ‘-H newc‘; uses the new (SVR4) portable format. If you want the old portable (ASCII) archive format, use ‘-H odc‘ instead.
Set the I/O block size to IO-SIZE bytes.
Create leading directories where needed.
Read additional patterns specifying filenames to extract or list from FILE. The lines of FILE are treated as if they had been non-option arguments to cpio. This option is used in copy-in mode.
Only copy files that do not match any of the given patterns.
Archive filename to use instead of standard input or output. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
With -F, -I, or -O, take the archive file name to be a local file even if it contains a colon, which would ordinarily indicate a remote host name.
Use archive format FORMAT. The valid formats are listed below; the same names are also recognized in all-caps. The default in copy-in mode is to automatically detect the archive format, and in copy-out mode is ‘bin‘.
bin: The obsolete binary format.
odc: The old (POSIX .1) portable format.
newc: The new (SVR4) portable format, which supports file systems having more than 65536 inodes.
crc: The new (SVR4) portable format with a checksum added.
tar: The old tar format.
ustar: The POSIX .1 tar format. Also, recognizes GNU tar archives, which are similar but not identical.
hpbin: The obsolete binary format used by HPUX’s cpio (which stores device files differently).
hpodc: The portable format used by HPUX’s cpio (which stores device files differently).
Archive filename to use instead of standard input. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
Ignored; for compatibility with other versions of cpio.
Link files instead of copying them, when possible.
Copy the file that a symbolic link points to, rather than the symbolic link itself.
Retain previous file modification times when creating files.
Print MESSAGE when the end of a volume of the backup media (such as a tape or a floppy disk) is reached, to prompt the user to insert a new volume. If MESSAGE contains the string ‘%d‘, it is replaced by the current volume number (starting at 1).
Show numeric UID and GID instead of translating them into names when using the ‘–verbose‘ option.
Create all files relative to the current directory in copy-in mode, even if they have an absolute file name in the archive.
This is the default: tell cpio not to strip leading file name components that contain ‘..‘ and leading slashes from file names in copy-in mode.
Do not change the ownership of the files; leave them owned by the user extracting them. This is the default for non-root users, so that users on System V don’t inadvertently give away files. This option can be used in copy-in mode and copy-pass mode.
Archive filename to use instead of standard output. To use a tape drive on another machine as the archive, use a filename that starts with ‘HOSTNAME:‘. The hostname can be preceded by a username and an ‘@‘ to access the remote tape drive as that user, if you have permission to do so (typically an entry in that user’s ‘~/.rhosts‘ file).
Verify the CRC of each file in the archive, when reading a CRC format archive. Do not actually extract the files.
Set the ownership of all files created to the specified user and/or group in copy-out and copy-pass modes. Either the user, the group, or both, must be present. If the group is omitted but the ‘:‘ or ‘.‘ separator is given, use the given user’s login group. Only the super-user can change files’ ownership.
Notifies cpio that is should use COMMAND to communicate with remote devices.
Swap the bytes of each halfword (pair of bytes) in the files. This option can be used in copy-in mode.
Swap the halfwords of each word (4 bytes) in the files. This option may be used in copy-in mode.
Write files with large blocks of zeros as sparse files. This option is used in copy-in and copy-pass modes.
Print a table of contents of the input.
Extract files to standard output. This option may be used in copy-in mode.
Replace all files, without asking whether to replace existing newer files with older files.
List the files processed, or with ‘-t‘, give an ‘ls -l‘ style table of contents listing. In a verbose table of contents of a ustararchive, user and group names in the archive that do not exist on the local system are replaced by the names that correspond locally to the numeric UID and GID stored in the archive.
Print a ‘.‘ for each file processed.
Print the cpio program version number and exit.
When creating an archive, cpio takes the list of files to be processed from the standard input, and then sends the archive to the standard output, or to the device defined by the ‘-F‘ option. Usually find or ls is used to provide this list to the standard input. In the following example you can see the possibilities for archiving the contents of a single directory:
% ls | cpio -ov > directory.cpio
The ‘-o‘ option creates the archive, and the ‘-v‘ option prints the names of the files archived as they are added. Notice that the options can be put together after a single ‘–‘ or can be placed separately on the command line. The ‘>‘ redirects the cpio output to the file ‘directory.cpio‘.
If you wanted to archive an entire directory tree, the find command can provide the file list to cpio:
% find . -print -depth | cpio -ov > tree.cpio
This will take all the files in the current directory, the directories below and place them in the archive tree.cpio. Again the ‘-o‘ creates an archive, and the ‘-v‘ option shows you the name of the files as they are archived (see ‘Copy-out mode‘). Using the ‘.‘ in the find statement will give you more flexibility when doing restores, as it will save file names with a relative path via a hard-wired, absolute path. The ‘-depth‘ option forces ‘find‘ to print of the entries in a directory before printing the directory itself. This limits the effects of restrictive directory permissions by printing the directory entries in a directory before the directory name itself.
Extracting an archive requires a bit more thought because cpio will not create directories by default. Another characteristic, is it will not overwrite existing files unless specified.
% cpio -iv < directory.cpio
This will retrieve the files archived in the file directory.cpio and place them in the present directory. The ‘-i‘ option extracts the archive and the ‘-v‘ shows the file names as they are extracted. If you are dealing with an archived directory tree, you need to use the ‘-d‘ option to create directories as necessary, something like:
% cpio -idv < tree.cpio
This will take the contents of the archive tree.cpio and extract it to the current directory. If you try to extract the files on top of files of the same name that already exist (and have the same or later modification time) cpio will not extract the file unless told to do so by the -u option (see ‘Copy-in mode‘).
In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input; the directory into which it will copy them is given as a non-option argument (see ‘Copy-pass mode‘).
The example shows copying the files of the present directory, and sub-directories to a new directory called new-dir. Some new options are the ‘-print0‘ available with GNU find, combined with the ‘–null‘ option of cpio. These two options act together to send file names between find and cpio, even if special characters are embedded in the file names. Another is ‘-p‘, which tells cpio to pass the files it finds to the directory ‘new-dir‘.
find . -print | cpio -ocv > /dev/fd0
Above, using the find command would list all files and directories and using the cpiocommand copy those files listed to the floppy drive.
find . -print | cpio -dumpv /home/users/hope
In the above example, the find command would list all files and subdirectories of the current directory, and pipe them to the cpio command, which copies those files to the hope user account.
cpio -icuvd < /dev/fd0
The above command would restore the files back from the floppy.
CRON is a Linux system process that executes a program at a preset time. To use a CRON script, admins must prepare a text file that describes the program and when they want CRON to execute it. Then, the crontab program loads the text file and executes the program at the specified time.
System Cron jobs exist as entries in the /etc/crontab file. Each job is described on a single line by defining a time interval, a user to run the command as, and the command to run. Cron can run any kind of script, command, or executable.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report / cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report / cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Below is the default system crontab file from Debian 9:
This means at 17 minutes past each hour, change directory to /, the root of the filesystem. Then, as the root user, run the run-parts binary to execute all jobs in /etc/cron.hourly.
Time intervals are denoted by numbers and operators filled in place of each asterisk in a Cron job’s crontab line. From left to right, the asterisks represent:
Minutes specified as a number from 0 to 59.
Hours specified as numbers from 0 to 23.
Days of the month, specified as numbers from 1 to 31.
Months specified as numbers from 1 to 12.
Days of the week, specified as numbers from 0 to 7, with Sunday represented as either/both 0 and 7.
Admins use cURL to transfer a URL. It is useful for determining if an application can reach another service and how healthy the service is.
The declare command states variables, gives them attributes or modifies the properties of variables.
This command displays the amount of disk space available on the file system containing each file name argument. With no file name, the df command shows the available space on all the currently mounted file systems.
his command displays the amount of disk space available on the file system containing each file name argument. With no file name, the df command shows the available space on all the currently mounted file systems.
Use echo to repeat a string variable to standard output.
The enable command stops or starts printers and classes.
The env command runs a program in a modified environment or displays the current environment and its variables.
The eval command analyzes several arguments, concatenates them into a single command and reports on that argument’s status.
This function replaces the parent process with any subsequently typed command. The exec command treats its arguments as the specification of one or more subprocesses to execute.
The exit command terminates a script and returns a value to the parent script.
The expect command talks to other interactive programs via a script and waits for a response, often from any string that matches a given pattern.
The export command converts a file into a different format than its current format. Once a file is exported, it can be accessed by any application that uses the new format.
find The find command searches the directory tree to locate particular groups of files that meet specified conditions, including -name, -type, -exec, -size, -mtime and -user.
The for and while commands execute or loop items repeatedly as long as certain conditions are met.
With the free command, admins can see the total amount of free and used physical memory and swap space in the system, as well as the buffers and cache used by the kernel.
gawk See AWK.
The grep command searches files for a given character string or pattern and can replace the string with another. This is one method of searching for files within Linux.
This is the GNU Project’s open source program for file compression that compresses webpages on the server end for decompression in the browser. This is popular for streaming media compression and can simultaneously concatenate and compress several streams.
The history function shows all the commands used since the start of the current session.
The iconfig command configures kernel-resident network interfaces at boot time. It is usually only needed when debugging or during system tuning.
With ifup, admins can configure a network interface and enable a network connection.
The ifdown command shuts down a network interface and disables a network connection.
iptablesThe iptables command allows or blocks traffic on a Linux host and can prevent certain applications from receiving or transmitting a request.
With kill signals, admins can send a specific signal to a process. It is most often used to safely shut down processes or applications.
The less command lets an admin scroll through configuration and error log files, displaying text files one screen at a time with backward or forward navigation available.
The locate command reads one or more databases and writes file names to match certain output patterns.
The lft command determines connection routes and provides information to debug connections or find a box/system location. It also displays route packets and file types.
The ln command creates a new name for a file using hard linking, which allows multiple users to share one file.
The ls command lists files and directories within the current working directory, which allows admins to see when configuration files were last edited.
Admins use lsof to list all the open files. They can add -u to find the number of open files by username.
The lsmod command displays a module’s status within the kernel, which helps troubleshoot server function issues.
The man command allows admins to format and display the user manual that’s built into Linux distributions, which documents commands and other system aspects.
The man command allows admins to format and display the user manual that’s built into Linux distributions, which documents commands and other system aspects.
Similar to less, more pages through text one screen at a time, but has limitations on file navigation.
This command mounts file systems on servers. It also lists the current file systems and their mount locations, which is useful to locate a defunct drive or install a new one.
Linux mkdir generates a new directory with a name path.
A Gnome GUI tool that allows admins to specify the information needed to set up a network card.
Admins can use netconfig to configure a network, enable network products and display a series of screens that ask for configuration information.
This command provides information and statistics about protocols in use and current TCP/IP network connections. It is a helpful forensic tool for figuring out which processes and programs are active on a computer and are involved in network communications.
A user can enter a host name and find the corresponding IP address with nslookup. It can also help find the host name.
The od command dumps binary files in octal — or hex/binary — format to standard output.
passwd Admins use passwd to update a user’s current password.
The ping command verifies that a particular IP address exists and can accept requests. It can test connectivity and determine response time, as well as ensure an operating user’s host computer is working.
Admins use ps to report the statuses of current processes in a system.
The read command interprets lines of text from standard input and assigns values of each field in the input line to shell variables for further processing.
This command syncs data from one disk or file to another across a network connection. It is similar to rcp, but has more options.
The GNU screen utility is a terminal multiplexor where a user can use a single terminal window to run multiple terminal applications or windows.
Admins use sdiff to compare two files and produce a side-by-side listing indicating lines that are dissimilar. The command then merges the files and outputs the results to the outfile.
The sed utility is a stream editor that filters text in a pipeline, distinguishing it from other editors. It takes text input, performs operations on it and outputs the modified text. This command is typically used to extract part of a file using pattern matching or to substitute multiple occurrences of a string within a file.
This command is the quickest way to start or stop a service, such as networking.
The shutdown command turns off the computer and can be combined with variables such as -h for halt after shutdown or -r for reboot after shutdown.
Like locate, slocate, or secure locate, provides a way to index and quickly search for files, but it can also securely store file permissions and ownership to hide information from unauthorized users.
This command sorts lines of text alphabetically or numerically according to the fields. Users can input multiple sort keys.
The sudo command lets a system admin give certain users the ability to run some — or all — commands at the root level and logs all the commands and arguments.
SSH is a command interface for secure remote computer access and is used by network admins to remotely control servers.
The tar command lets users create archives from a number of specified files or to extract files from a specific archive.
The tail command displays the last few lines of the file. This is particularly helpful for troubleshooting code because admins don’t often need all the possible logs to determine code errors.
TOP is a set of protocols for networks that performs distributed information processing and displays the tasks on the system that take up the most memory. TOP can sort tasks by CPU usage, memory usage and runtime.
Admins can create a blank file within Linux with the touch command.
This command translates or deletes characters from a text stream. It writes to a standard output, but it does not accept file names as arguments — it only accepts input from standard input.
The traceroute function determines and records a route through the internet between two computers and is useful for troubleshooting network/router issues. If the domain does not work or is not available, admins can use traceroute to track the IP.
With uniq, admins can compare adjacent lines in a file and remove or identify any duplicate lines.
The vi environment is a text editor that allows a user to control the system with just the keyboard instead of both mouse selections and keystrokes.
vmstat The vmstat command snapshots everything in a system and reports information on such items as processes, memory, paging and CPU activity. This is a good method for admins to use to determine where issues/slowdown may occur in a system.
This is a network utility that retrieves web files that support HTTP, HTTPS and FTP protocols. The wget command works non-interactively in the background when a user is logged off. It can create local versions of remote websites and recreate original site directories.
The whoami command prints or writes the user login associated with the current user ID to the standard output.
Admins use xargs to read, build and execute arguments from standard input. Each input is separated by blanks.
Gzip or .gz is used to compress a file in order to reduce disk space, it is quite popular in Linux and UNIX operating systems for this reason. Gzip has been around since May 1996 and is still widely used today.
The below examples showing the process of compressing gzip and unzipping .The compression technique can help you save a lot of memory by compressing .
Any Linux environment should be installed in your system.
System should have the gzip package installed, this is usually already installed by default, however you can install it now if required.
Find the correct Linux version installed in your system.The command are below mentioned for Red hot Linux or Debian.
yum install gzip
apt-get install gzip
Example Gzip Commands
1. Compress a single file
This will compress file.txt and create file.txt.gz, note that this will remove the original file.txt file.gzip file.txt
2. Compress multiple files at once
gzip file1.txt file2.txt file3.txt
This will compress all files specified in the command, note again that this will remove the original files specified by turning file1.txt, file2.txt and file3.txt into file1.txt.gz, file2.txt.gz and file3.txt.gzgzip file1.txt file2.txt file3.txt To instead compress all files within a directory, see example 8 below.
3. Compress a single file and keep the original
gzip -c file.txt > file.txt.gz
You can instead keep the original file and create a compressed copy.gzip -c file.txt > file.txt.gz The -c flag outputs the compressed copy of file.txt to stdout, this is then sent to file.txt.gz, keeping the original file.txt file in place. Newer versions of gzip may also have -k or –keep available, which could be used instead with “gzip -k file.txt”.
4. Compress all files recursively
All files within the directory and all sub directories can be compressed recursively with the -r flag
In the above example there are 3 .txt files in the test directory which is our current working directory, there is also an example sub directory which contains example.txt. Upon running gzip with the -r flag over everything, all files were recursively compressed. This can be reversed by running “gzip -dr *”, where -d is used to decompress and -r performs this on all of the files recursively.
5. Decompress a gzip compressed file To reverse the compression process and get the original file back that you have compressed, you can use the gzip command itself or gunzip which is also part of the gzip package.
gzip -d file.txt.gz
Both of these commands will produce the same result, decompressing file.txt.gz to file.txt, removing the compressed file.txt.gz file.
Similar to example 3, it is possible to decompress a file and keep the original .gz file as below.
gunzip -c file.txt.gz > file.txt
As mentioned in step 4, -d can be combined with -r to decompress all files recursively.
6. List compression information With the -l or –list flag we can see useful information regarding a compressed .gz file such as the compressed and uncompressed size of the file as well as the compression ratio, which shows us how much space our compression is saving.
# ls -lah -rw-r–r–. 1 root root 554M Jul 28 17:24 linux-3.18.19.tar -rw-r–r–. 1 root root 121M Jul 28 17:25 linux-3.18.19.tar.gz . In this example, a gzipped copy of the Linux kernel has compressed to 78.3% of its original size, taking up 121MB of space rather than 554MB.
7. Adjust compression level The level of compression applied to a file using gzip can be specified as a value between 1 (less compression) and 9 (best compression). Using option 1 will complete faster, but space saved from the compression will not be optimal. Using option 9 will take longer to complete, however you will have the largest amount of space saved.
The below example compares the differences between -1 and -9, as shown while -1 finishes much faster it compresses around 5% less (approximately 30mb more space required).
[[email protected] ~]# time gzip -1 linux-3.18.19.tar real 0m13.602s user 0m12.908s sys 0m0.662s
-1 can also be specified with the flag –fast, while option -9 can also be specified with the flag –best. By default gzip uses a compression level of -6, which is slightly biased towards higher compression at the expense of speed. When selecting a value between 1 and 9 it is important to consider what is more important to you, the amount of space saved or the amount of time spent compressing, the default -6 option provides a fair trade off. 8. Compress a directory With the help of the tar command, we can create a tar file of a whole directory and gzip the result. We can perform the whole lot in one step, as the tar command allows us to specify a compression method to use. This example creates a compressed etc.tar.gz file of the entire /etc/ directory. The tar flags are as follows, ‘c’ creates a new tar archive, ‘z’ specifies that we want to compress with gzip, ‘v’ provides verbose information, and ‘f’ specifies the file to create. The resulting etc.tar.gz file contains all files within /etc/ compressed using gzip.
tar czvf etc.tar.gz /etc/
9. Integrity test The -t or –test flag can be used to check the integrity of a compressed file.
On a normal file, the result will be listed as OK, shown below.
The files.gz now contains the contents of both file1.txt and file2.txt, if you decompress files.gz you will get a file named ‘files’ which contains the content of both .txt files. The output is similar to running ‘cat file1.txt file2.txt’. If instead you want to create a single file that contains multiple files you can use the tar command which supports gzip compression, as covered above in example 8. 11. Additional commands included with gzip The gzip package provides some very useful commands for working with compressed files, such as zcat, zgrep and zless/zmore.
As you can probably tell by the names of the commands, these are essentially the cat, grep, and less/more commands, however they work directly on compressed data. This means that you can easily view or search the contents of a compressed file without having to decompress it and then view or search it in a second step.