Tips-And-Tricks/Computer/Linux/Shell-Commands: Difference between revisions

From Pikes Peak Myco Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 3: Line 3:
As a MacOS user, you too have access to most of these commands as well (surprisingly without having to pay an exhorberant fee to use them too!). Mac OS is based on an offshoot of the original AT&T Unix called BSD (Berkeley Software Distribution), and as such has most of the core shell commands at your disposal.
As a MacOS user, you too have access to most of these commands as well (surprisingly without having to pay an exhorberant fee to use them too!). Mac OS is based on an offshoot of the original AT&T Unix called BSD (Berkeley Software Distribution), and as such has most of the core shell commands at your disposal.


== File Compression / Extraction ==
== Pipes, Redirection, and Sub-shells ==
 
=== Pipes ===
 
Every process in a *nix command shell, and part of this is even implemented in Windows as well has three main input channels.
 
{| class="wikitable"
|+ Standard File Descriptors
|-
! Name
! FD
! Description
|-
| stdin
| 0
| Standard In represents the input to a command, either read from the users terminal, or from the output of another command.
|-
| stdout
| 1
| Standard Out represents the normal output of a command. This is what normally gets printed to the terminal and is not an error or specialty message
|-
| stderr
| 2
| Standard error represents error conditions or things the program wants to display to the user despite if the command is being redirected or piped.
|}
 
Processes can be joined together using the symbol <code>|</code> (usually shift backslash). What this does is send the output of the left command into the input of the right command.  This can be done an arbitrary number of times, building very powerful and complex text processing.
 
Example: List any files from December
<code>ls -l | grep -iw Dec</code>
 
=== Redirection ===
=== Sub-Shells ===
 
== File Compression and Extraction ==


There are two main compression utilities out there that can take a directory and compress it all into a single file, allowing you to move it to another computer, either in your home or on the internet, and then extract it there. Depending on the source or destination computer may make the decision as to the best.
There are two main compression utilities out there that can take a directory and compress it all into a single file, allowing you to move it to another computer, either in your home or on the internet, and then extract it there. Depending on the source or destination computer may make the decision as to the best.

Latest revision as of 15:44, 28 February 2026

Linux is a growing and popular operating system, especially for us citizen mycologists, and unknown to a lot of first-time users, it has an extensive set of wonderful tools that can do things that are nearly black magic in Windows or even to the casual Macintosh user.

As a MacOS user, you too have access to most of these commands as well (surprisingly without having to pay an exhorberant fee to use them too!). Mac OS is based on an offshoot of the original AT&T Unix called BSD (Berkeley Software Distribution), and as such has most of the core shell commands at your disposal.

Pipes, Redirection, and Sub-shells

Pipes

Every process in a *nix command shell, and part of this is even implemented in Windows as well has three main input channels.

Standard File Descriptors
Name FD Description
stdin 0 Standard In represents the input to a command, either read from the users terminal, or from the output of another command.
stdout 1 Standard Out represents the normal output of a command. This is what normally gets printed to the terminal and is not an error or specialty message
stderr 2 Standard error represents error conditions or things the program wants to display to the user despite if the command is being redirected or piped.

Processes can be joined together using the symbol | (usually shift backslash). What this does is send the output of the left command into the input of the right command. This can be done an arbitrary number of times, building very powerful and complex text processing.

Example: List any files from December ls -l | grep -iw Dec

Redirection

Sub-Shells

File Compression and Extraction

There are two main compression utilities out there that can take a directory and compress it all into a single file, allowing you to move it to another computer, either in your home or on the internet, and then extract it there. Depending on the source or destination computer may make the decision as to the best.

zip/unzip - Compatible with Micro$oft Windows since Windows XP tar - Compatible with Linux, or any Unix-compatible operating system, including macOS.

zip/unzip

Compression

The zip command packs files into a zip archive, which can be read on the host machine pretty much universally. The general format is

zip [flags] destination[.zip] file list

Where the .zip extension is optional. To zip a list of files in the SAME directory, you can use

zip filename.zip file1 file2 file3 ...

To zip an entire directory structure you can use the "r" flag and it will descend through the whole structure adding everything found to the archive.

zip -r docs.zip Documents

Extraction

The unzip command does the opposite of zip and extracts files. It does have a set of flags you can use in special sitautions, but there is really only one important one in every day use

-l

The -l flag says to list the contents instead of actually extracting the files.

unzip -l zipfile.zip Will list all of the files in the archive

unzip zipfile.zip Will extract the files from the archive into the current directory

tar

The tar command basically operates the same as the zip command but can provide multiple types of compression. It stands for "Tape ARchive" and heralds from the early days when computers literally had reel-to-reel tapes.

tar [flags] archive [file1 file2 file3]

The most commonly used flags are:

  • c Create the archive
  • x Extract the archive
  • v Be verbose (display the files extracted/added)
  • f filename Filename to create or extract from
  • z Compress using zlib (fast, decent compression)
  • j Compress using bzip2 (medium speed, better compression)
  • J Compress using xz (medium speed, good compression)

Please make sure to include a compression type when creating a tar archive. With no compression, it can take a huge amount of space with standard tar. Again, this is due to its origin in creating data for reel-to-reel tapes.

Working with a single file

Sometimes you may need to compress only a single file to reduce its size, like to transfer a video over the internet or something. There are several tools to do this with. As a general rule of thumb, the better the compression, the longer it takes to run. Although each command has uninque parameters that can be passed to it, in general the format is (tool) filename which will create filename.exstention .

Common Compression Utilities
Name Compression Decompression Speed Compression Amount Extension
GZip (Zlib) gzip gunzip Fast Medium/Low .gz
Bzip2 bzip2 bunzip2 Slow High .bz / .bz2
Xz/LZW xz unxz Medium/Slow Exceptional .xz