PermalinkCOMPRESS AND UN-COMPRESS FILES:
In Linux, you can compress and uncompress files using various commands and tools. The most commonly used commands are tar
, gzip
, gunzip
, zip
, and unzip
. Let me provide you with definitions, usage, and examples for each of these commands:
tar: The
tar
command is used to create a single archive file also known as a "tarball" that can contain multiple files and directories while preserving their directory structure.In the
tar
command, thec, z, v, f
options are separate and each letter represents a separate option with its own meaning. Here's the breakdown:-c
: Creates a new archive.-z
: Compresses the archive using gzip.-v
: Enables verbose output (optional).-f
: Specifies the output filename.To compress files/directories using
tar
:$ tar -czvf archive.tar.gz file1.txt directory/
To uncompress files/directories using
tar
:$ tar -xzvf archive.tar.gz
Here,
x
stands for extract,z
indicates gzip format,v
enables verbose output (optional), andf
specifies the input filename.
gzip/gunzip: The
gzip
command is used to compress files using the gzip algorithm, whilegunzip
is used to uncompress gzip-compressed files.To compress a file using
gzip
:$ gzip file.txt
This will create a compressed file named
file.txt.gz
.To uncompress a gzip file using
gunzip
:$ gunzip file.txt.gz
The command will extract the original file named
file.txt
from the compressedfile.txt.gz
file.
zip/unzip: The
zip
command is used to compress files and directories into a zip archive, whileunzip
is used to extract files from a zip archive.To compress files/directories using
zip
:$ zip archive.zip file1.txt directory/
This will create a zip archive named
archive.zip
containingfile1.txt
anddirectory/
.To extract files from a zip archive using
unzip
:$ unzip archive.zip
The command will extract all the files from
archive.zip
into the current directory.
These are the basic commands for compressing and uncompressing files in Linux. Remember to adjust the command options and filenames based on your specific requirements.
PermalinkTRUNCATE FILE SIZE:
To truncate or reduce the file size in Linux, you can use the truncate
command. The truncate
command allows you to either shrink or extend the size of a file. If you want to specifically reduce the file size, you can use the --size
option followed by the desired size.
Here's the general syntax
of the truncate
command to truncate a file to a specific size:
$ truncate --size=<new_size> <file_path>
Replace <new_size>
with the desired size to which you want to truncate the file, and <file_path>
with the path to the file you want to truncate.
Here's an example to truncate a file named example.txt
to 1000 bytes:
$ truncate --size=1000 example.txt
This command will reduce the file example.txt
to 1000 bytes. If the original file size was greater than 1000 bytes, the excess data will be removed, effectively reducing the file size. However, if the original file size was smaller than 1000 bytes, the file will remain unchanged.
Make sure you have appropriate permissions to modify the file in question, as you may need to use sudo
or have the necessary write permissions for the file.
PermalinkCOMBINING AND SPLITTING FILES:
In Linux, you can combine or concatenate files using the cat
command and you can split files into smaller parts using the split
command. Let's look at each command and its usage with examples:
Combining/Concatenating Files with
cat
: Thecat
command is used to concatenate files and output the result to the standard output (usually the terminal). You can redirect this output to a new file if needed.To combine files using
cat
:$ cat file1.txt file2.txt > combined.txt
This command concatenates the contents of
file1.txt
andfile2.txt
and saves the output incombined.txt
.To append the content of one file to another:
$ cat file1.txt >> file2.txt
This command appends the content of
file1.txt
to the end offile2.txt
without creating a new file.
Splitting Files with
split
: Thesplit
command allows you to split a file into smaller parts based on size or number of lines.To split a file into smaller parts by size:
$ split -b <size> file.txt prefix
Replace
<size>
with the desired size of each split part (e.g., 1M for 1 megabyte),file.txt
with the file you want to split, andprefix
with the desired prefix for the resulting split files.Example:
$ split -b 1M bigfile.txt splitfile
This command splits
bigfile.txt
into smaller parts with a size of 1 megabyte each, and the resulting split files will have the prefixsplitfile
.To split a file into smaller parts by the number of lines:
$ split -l <lines> file.txt prefix
Replace
<lines>
with the desired number of lines per split part,file.txt
with the file you want to split, andprefix
with the desired prefix for the resulting split files.Example:
$ split -l 100 file.txt splitfile
This command splits
file.txt
into smaller parts, each containing 100 lines, and the resulting split files will have the prefixsplitfile
.
These commands provide you with the ability to combine or split files in Linux according to your requirements.
PermalinkLINUX vs WINDOWS Commands:
PermalinkCONCLUSION:
It’s been great to have you all on my Blog!
Hope it helps you in your journey of learning the Operating System and I am glad that I am one of the part. Do follow for more and I am sure that I will engage you with profound insights.
Thank you!
#happylearning