0% found this document useful (0 votes)
52 views44 pages

Tar

The du command displays disk usage information and allows customizing the output. The df command displays free disk space and file system type information. The tar command creates archives of multiple files into a single file and supports compression. The zip command creates compressed archives for file distribution and archiving.

Uploaded by

Tripti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views44 pages

Tar

The du command displays disk usage information and allows customizing the output. The df command displays free disk space and file system type information. The tar command creates archives of multiple files into a single file and supports compression. The zip command creates compressed archives for file distribution and archiving.

Uploaded by

Tripti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Linux

basic
commands
( du
( df
( tar
( zip
( gzip
( du
On any operating system, the files on your hard
disk take up a certain amount of space. In Linux
specifically, you can view how much space that
these files take up in the command line terminal
by using the du command. The du command (the
name is shortened from “disk usage”), as the
name implies, will simply display, in its output, the
amount of disk space being used by a specified
file or directory.
( du
It allows a user to gain disk usage information
quickly. It is best applied to specific directories
and allows many variations for customizing the
output to meet your needs. As with most
commands, the user can take advantage of many
options or flags.
Options :

-0,--null : end each output line with NULL


-a, --all : write count of all files, not just directories
--apparent-size : print apparent sizes, rather than disk
usage.
-B, --block-size=SIZE : scale sizes to SIZE before
printing on console
-c, --total : produce grand total
-h, --human-readable : print sizes in human readable
format
-S, -separate-dirs : for directories, don’t include size
of subdirectories
-s, --summarize : display only total for each directory
–time : show time of last modification of any file or
directory.
--exclude=PATTERN : exclude files that match
PATTERN
du [OPTION]... [FILE]...
If we want to print sizes in human readable
format(K, M, G), use -h option. This is done
by using the ‘-h’ flag with the ‘du’ command.
The Total Size for Each Directory
To determine the total size of the whole
directory instead of showing the sizes of the
files present inside them.
$ du -s /home/keen
humanreadable and total size du -hs
all files folders -a size
will show the total of all the text files present in
your system.
$ du -c -h /home/aqsayasin/*.txt
File Timing of Last Modifications
$ du --time -h /home/aqsayasin/data?.txt
How to check the size of all the sub-
directories in their current location?
# du -h --max-depth=1 linuxteck
OR # du -
h -d1 linuxteck
How to exclude a particular type of file while
calculating the disk size?
# du -h --exclude="*.php" linuxteck
( df
df (disk free) command is used to display disk
usage of the file system. By default df command
shows the file system usage in 1K blocks for all
the current mounted file system, if you want to
display the output of df command in human
readable format , use -h option like “df -h”. This
command is mainly used by all System Admins,
as it is one of their main tasks to monitor the
server/storage space.
Mandatory arguments to long options are mandatory
for short options too.
Options for df command :
-a, –all : includes pseudo, duplicate and inaccessible
file systems.
-B, –block-size=SIZE : scales sizes by SIZE before
printing them.
-h, –human-readable : print sizes in power of 1024
-H, –si: print sizes in power of 1000
-i, –inodes : list inode information instead of block
usage
-l, –local : limit listing to local file systems
-P, –portability : use POSIX output format
–sync : invoke sync before getting usage info
–total : elide all entries insignificant to
available space, and produce grand total
-t, –type=TYPE : limit listing to file systems of
type TYPE
-T, –print-type : print file system type

df [Options]…[File]…
$ df --help
to look at the free space of all the file
systems regardless of whether they are
mounted or unmounted, accessible or
inaccessible. In that case, you can use the
following variation of the “df” command:
$ df –a
Your output may have more entries. The columns should
be self-explanatory:
Filesystem – This is the name of each particular drive.
This includes physical hard drives, logical (partitioned)
drives, and virtual or temporary drives.
Size – The size of the filesystem.
Used – Amount of space used on each filesystem.
Avail – The amount of unused (free) space on the
filesystem.
Use% – Shows the percent of the disk used.
Mounted on – This is the directory where the file system is
located. This is also sometimes called a mount point.
“-T” option in the terminal, it will print the
machine’s file system type in output.
$ df -T
to get a total figure of the space of the file
systems in Linux, then you can execute the
following command:
$ df --total
if we want to print the disk space information of
the file system “devtmpfs.” So, the command
would be
$ df -t devtmpfs
An inode is a data structure in Unix and
Linux file systems, which contains
information about a file or directory such as
its size, owner, device node, socket, pipe,
etc.
When invoked with the -i option, the df
command prints information about the
filesystem inodes usage.
The “-i” option is used to show Inode information
in the terminal instead of disk space information.
When you run this option, it will show the “Inode”
column along with “IUsed” and “IFree” columns.
$ df –i
Print only Local file system space usage info.
Suppose network file system also mounted on
linux box and but we want to display local file
system information only, this can be achieved by
using ‘-l‘ option in df command.
$ df -Thl
( tar
The tar command creates the tar file type is
used to combine multiple files into a single
archive. Tar actually means “tape archive,”
because tar’s original purpose was to be used
on tape backups – that should tell you how old
this format is. Linux systems still use the tar
format, and it continues to enjoy widespread use
to this day.
Tar files, with the extension .tar, are often
called “tarballs.” These files will preserve
the Linux file permissions and can
combine any number of files into the
single archive, but they don’t apply any
compression or space savings. However,
compression can be easily applied to the
tar file, resulting in extensions like .tar.gz
in the case of gzip compression, or .tar.xz
for xz compression.
The tar command creates tar files by converting a
group of files into an archive. It also can extract tar
archives, display a list of the files included in the
archive, add additional files to an existing archive, and
various other kinds of operations.
Tar was originally designed for creating archives to
store files on magnetic tape, which is why it has its
name “Tape ARchive”.
This article shows how to use the tar command to
extract, list, and create tar archives through practical
examples and detailed explanations of the most
common tar options.
Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar
file
Syntax:
tar [options] [archive-file] [file or directory to be
archived]
An Archive file is a file that is composed of one or
more files along with metadata. Archive files are
used to collect multiple data files together into a
single file for easier portability and storage, or
simply to compress files to use less storage
space.
Create an archive file
The tar utility allows you to create archive files using
various compression algorithms such as xz, gzip, and
bzip2. The accepted convention is to suffix the archive
file with the compression suffix.
For example, when using the gzip compression (using
the -z option), the file should bear the .tar.gz suffix.
For example, the command below creates an archive
file called sales.tar.gz from the three PDF files.
$ tar -czvf sales.tar.gz sales1.pdf sales2.pdf sales3.pdf
List the contents of an archive file
Using the -t option, you can have a peek
or list the contents of an archive file
without extracting it as shown.
$ tar -tf sales.tar.gz
Extract an archive file in the current directory
To extract an archive file in the current
working directory, use the -x option as shown
below. In the example below, we are
uncompressing or extracting the
documents.tar.gz archive, which contains
three text files.
$ tar -xvf documents.tar.gz
Extract an archive file in a separate directory
To extract an archive file to a different
directory, the -C option is followed by the
destination path, as shown in the example
below.
$ tar -xvf documents.tar.gz -C /tmp/files
Add a file to a .tar archive
To add or append a .tar archive file, use
the -r option as shown. Here, we are
adding the file file3.txt to the archives.tar
archive.
$ tar -rvf archives.tar file3.txt
Remove a file from a .tar archive
To remove a file from a .tar archive, use the –
delete option as shown. Here, we are doing the
complete opposite and removing the file file3.txt
instead.
$ tar --delete -f archives.tar file3.txt
( zip
Zip is the most widely used archive file format that
supports lossless data compression.
A Zip file is a data container containing one or
more compressed files or directories. Compressed
(zipped) files take up less disk space and can be
transferred from one to another machine more
quickly than uncompressed files. Zip files can be
easily extracted in Windows, macOS, and Linux
using the utilities available for all operating
systems.
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.
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.
Options
-u: Updates the file in the zip archive. This option
can be used to update the specified list of files or
add new files to the existing zip file.
-m: Deletes the original files after zipping. 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.
-d: Removes the file from the zip archive. After
creating a zip file, you can remove a file from the
archive using the -d option.
-r: To zip a directory recursively, use the -r option
with the zip command and it will recursively zips the
files in a directory.
-x: Exclude the files in creating the zip. Let say you
are zipping all the files in the current directory and
want to exclude some unwanted files.
-v: Verbose mode or print diagnostic version info.
How to view the contents of a
zipped file (-l)
Before you are unzipping a file
or directory, you can view the
archive’s contents using the -l
option
unzip -l archive.zip
How to view detailed contents of a
zipped file (-Z)
To view more detailed information about
the file such as file permissions and the
total size of the files in the archive, use
the -Z option as shown:
$ unzip -Z data.zip
How to unzip/decompress a zipped
file (-d)
In its basic and simplest syntax,
unzipping a zipped file takes the
syntax below and occurs in the current
working directory.
$ unzip archive.zip
For example:
$ unzip data.zip
( gzip
Gzip is one of the most popular
compression algorithms that allow you
to reduce the size of a file and keep
the original file mode, ownership, and
timestamp.
Gzip also refers to the .gz file format
and the gzip utility which is used to
compress and decompress files.
A tar.gz file contains several compressed
files to save storage space, as well as
bandwidth during the downloading
process. The .tar file acts as a portable
container for other files and is sometimes
called a tarball. The .gz part of the
extension, stands for gzip, a commonly-
used compression utility.
-options description
-h, --help Print help screen
-d, --decompress Decompress files
-v, --verbose Report percent
reduction in file size
tar -zcvf output_file_name
directory_to_compress
Explain the options used in the above
command to you:
z – tells tar that it is dealing with gzip file
c – tells tar to create the archive file
v – verbose mode showing what files are
being processed
f – output is a file
Listing the contents of the gz file
We usually list the file contents, or files and
directories, before unzipping or extracting
the gz file. To view the list of contents of a
gz file, you can use the gzip command. The
-l option is available, as seen below:
$ gzip –l test.txt.gz

You might also like