Table of Contents
Comparing Files
Compressing and Uncompressing Files
Truncating File Size
Combining and Splitting Files
Description
Managing files effectively is a critical skill for system administrators and DevOps engineers. In this article, we'll cover file comparison, compression, truncation, and filensplitting techniques that will enhance your command line productivity.
Comparing Files
Efficiently comparing files is crucial for version control and troubleshooting. Here are the primary commands:
- diff (Line by line comparison)
Compare two text files line by line
diff file1.txt file2.txt
- cmp (Byte by byte comparison)
Compare two files byte by byte
cmp file1.txt file2.txt
Compressing and Uncompressing Files
File compression saves space and reduces transfer time.
- tar (Tape Archive, often used for creating compressed archives)
Create an archive
man tar
- to check the function of tar
Example
tar -cvf archive.tar logfile.txt
Extract the archive
tar -xvf archive.tar
Example
tar -xvf files.tar
-
gzip (Compress files)
gzip logfile.txt
gzip -d logfile.txt.gz
# Decompressing with gzipgunzip file.txt.gz
# Alternative decompression
Truncating File Size
Reduce or extend file sizes to a specific size using the truncate command.
Truncate or extend a file to 10 bytes
truncate -s 10 testfile1
Combining and Splitting Files
- Combining Multiple Files
Combine multiple files into one
cat testfile1.txt testfile2.txt > combined.txt
-
Splitting a File
Split a file into smaller chunks:
split -l 100 split.txt childfile.txt
This command will split split.txt into 100-line chunks, creating files like childfile.txtaa, childfile.txtab so on.
These commands are essential for efficiently managing files on Linux servers. Understanding their capabilities can significantly streamline your workflow as a system administrator or DevOps engineer.
Top comments (0)