Useful Linux Commands — Network and Compression

John Au-Yeung - Feb 13 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

export

The export command let us export variables to child processes. 

For instance, we run:

export TEST="test"
Enter fullscreen mode Exit fullscreen mode

to set the TEST variable to 'test' .

We can also reference another variable by using the $ in the expression on the right side.

For example, we run:

export PATH=$PATH:/new/path
Enter fullscreen mode Exit fullscreen mode

to reference the existing value of the PATH environment variable on the right side.

tar

We can run the tar command to archive files.

To create a tar archive, we run:

tar -cf archive.tar file1 file2
Enter fullscreen mode Exit fullscreen mode

to create a tar archive with the file1 and file2 files in the tar archive.

We can extract files from an archive in the current folder by running:

tar -xf archive.tar
Enter fullscreen mode Exit fullscreen mode

And we can extract an archive into a specific directory with:

tar -xf archive.tar -C dir
Enter fullscreen mode Exit fullscreen mode

x stands for extract and f means write files.

c means create.

The z option lets us create a gzip archive.

For instance, we run:

tar -czf archive.tar.gz file1 file2
Enter fullscreen mode Exit fullscreen mode

to create a gzip archive with file1 and file2 inside.

And we can gunzip a gzip archive with:

tar -xf archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

traceroute

The traceroute command lists all the nodes traversed to reach a host.

For instance, we run:

traceroute google.com
Enter fullscreen mode Exit fullscreen mode

to see where the packets go to reach Google.

ping

The ping command lets us ping a network host to see if it responds.

For instance, we run:

ping google.com
Enter fullscreen mode Exit fullscreen mode

to see if Google is up.

We can continually ping it with the -c switch:

ping -c google.com
Enter fullscreen mode Exit fullscreen mode

It stops pinging only when we press ctrl+c.

gunzip

The gunzip command lets us unzip a zip archive.

For instance, we can run:

gunzip filename.gz
Enter fullscreen mode Exit fullscreen mode

to extract the files in the filename.gz file.

We can use the -c switch extract a file to a different filename:

gunzip -c filename.gz > bar
Enter fullscreen mode Exit fullscreen mode

Then we extract filename.gz to the bar file.

gzip

The gzip command lets us create a compressed file.

For instance, we run:

gzip filename
Enter fullscreen mode Exit fullscreen mode

to compress the filename file.

We can rename the gzipped file with the -c switch:

gzip -c filename > filename.gz
Enter fullscreen mode Exit fullscreen mode

Then we compress the file into the filename.gz file.

The -k option also lets us change the name of the compressed file:

gzip -k filename
Enter fullscreen mode Exit fullscreen mode

We can set the compression level with the -<number> switch.

For instance, we run:

gzip -1 filename
Enter fullscreen mode Exit fullscreen mode

to compress filename with level 1 compression.

We can compress multiple file by adding the file names separated with spaces:

gzip file1 file2
Enter fullscreen mode Exit fullscreen mode

We compress file1 and file2 with one gzip command.

Also, we can compress all files in a directory recursively with the -r switch:

gzip -r folder
Enter fullscreen mode Exit fullscreen mode

The -v switch prints the compression percentage info.

The -d switch decompresses a file:

gzip -d filename.gz
Enter fullscreen mode Exit fullscreen mode

Conclusion

We can compress files and folders, and do basic network tasks with some basic Linux commands.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .