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

From Beneschtech Myco Wiki
No edit summary
imported>Claude
Wiki rebuild: cleanup and new cultivation articles
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
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.
Linux is a growing and popular operating system, especially among us citizen mycologists. Unknown to a lot of first-time users, it comes with an extensive set of tools that can do things that feel like black magic on Windows, or even to the casual Mac 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.
As a macOS user, you have access to most of these commands too (and surprisingly, without paying an exorbitant fee for them). macOS descends from BSD (the Berkeley Software Distribution), an offshoot of the original AT&T Unix, so most of the core shell commands are at your disposal.


== File Compression / Extraction ==
== Pipes, Redirection, and Sub-shells ==


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.
=== Standard streams ===
Every process in a *nix shell starts with three open streams, called '''file descriptors''': one for input and two for output. Windows implements the same idea.


zip/unzip - Compatible with Micro$oft Windows since Windows XP
{| class="wikitable"
tar - Compatible with Linux, or any Unix-compatible operating system, including macOS.
|+ Standard file descriptors
|-
! Name
! FD
! Description
|-
| stdin
| 0
| Standard input: what the command reads, either typed at the terminal or fed from another command.
|-
| stdout
| 1
| Standard output: the command's normal output. This is what normally gets printed to the terminal.
|-
| stderr
| 2
| Standard error: error messages and anything the program wants the user to see even when its output is being redirected or piped.
|}
 
=== Pipes ===
The pipe symbol <code>|</code> (usually Shift + backslash) sends the stdout of the command on the left into the stdin of the command on the right. You can chain as many as you like, which makes for very powerful text processing.
 
Example: list any files from December
 
<code>ls -l | grep -iw Dec</code>
 
Example: count how many of those there are
 
<code>ls -l | grep -iw Dec | wc -l</code>
 
=== Redirection ===
Redirection connects a stream to a file instead of the terminal.
 
{| class="wikitable"
! Syntax !! What it does
|-
| <code>command > file</code> || Write stdout to ''file'', replacing whatever was in it
|-
| <code>command >> file</code> || Append stdout to the end of ''file''
|-
| <code>command < file</code> || Read stdin from ''file''
|-
| <code>command 2> file</code> || Write stderr to ''file''
|-
| <code>command > file 2>&1</code> || Write both stdout and stderr to ''file''
|-
| <code>command > /dev/null</code> || Throw the output away
|}
 
Example: save a list of every photo in your grow log folder
 
<code>ls ~/GrowLog/*.jpg > photos.txt</code>
 
Note the order in <code>> file 2>&1</code>: it means "send stdout to the file, then send stderr wherever stdout is going." Written the other way around, stderr still goes to the terminal.
 
=== Sub-shells ===
A sub-shell runs commands in a separate copy of the shell.
 
'''Command substitution''' runs a command and drops its output into another command:
 
<code>echo "Today is $(date +%A)"</code>
 
<code>mkdir "grow-$(date +%Y-%m-%d)"</code> creates a folder named for today's date.
 
You may also see the older backtick form, <code>`date`</code>; <code>$( )</code> is easier to read and can be nested.
 
'''Grouping''' with parentheses runs a set of commands together in a sub-shell. Changes such as <code>cd</code> don't affect your current shell:
 
<code>(cd ~/GrowLog && ls)</code> lists that folder and leaves you where you started.
 
You can also redirect a whole group at once:
 
<code>(date; ls ~/GrowLog) > log.txt</code>
 
== File Compression and Extraction ==
There are two main utilities for packing a whole directory into a single file, so you can move it to another computer at home or over the internet and unpack it there. Which one to use mostly depends on the computers on each end.
 
* '''zip/unzip''': built into Microsoft Windows since Windows XP, as well as macOS and most Linux systems
* '''tar''': standard on Linux and every Unix-like system, including macOS. Recent Windows 10 and 11 releases include it too.


=== zip/unzip ===
=== zip/unzip ===
==== Compression ====
==== Compression ====
The <code>zip</code> command packs files into a zip archive, which can be read on the host machine pretty much universally.
The <code>zip</code> command packs files into a zip archive, which can be opened almost anywhere.
The general format is  
The general format is


<code>
<code>zip [flags] destination[.zip] file list</code>
zip [flags] destination[.zip] file list
</code>


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


<code>
<code>zip filename.zip file1 file2 file3 ...</code>
zip filename.zip file1 file2 file3 ...
</code>


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.
To zip an entire directory structure, use the <code>-r</code> (recursive) flag. It descends through the whole structure, adding everything it finds:


<code>
<code>zip -r docs.zip Documents</code>
zip -r docs.zip Documents
</code>


==== Extraction ====
==== Extraction ====
The <code>unzip</code> 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
The <code>unzip</code> command does the opposite and extracts files. It has flags for special situations, but there's really only one you need day to day:


<code>-l</code>
<code>-l</code> lists the contents instead of extracting.


The -l flag says to list the contents instead of actually extracting the files.
<code>unzip -l zipfile.zip</code> lists all of the files in the archive.


<code>unzip -l zipfile.zip</code> Will list all of the files in the archive
<code>unzip zipfile.zip</code> extracts the files into the current directory.
 
<code>unzip zipfile.zip</code> Will extract the files from the archive into the current directory


=== tar ===
=== tar ===
 
The <code>tar</code> command works much like zip but supports several kinds of compression. The name stands for "Tape ARchive," from the early days when computers literally used reel-to-reel tape.
The <code>tar</code> 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.


<code>tar [flags] archive [file1 file2 file3]</code>
<code>tar [flags] archive [file1 file2 file3]</code>


The most commonly used flags are:
The most commonly used flags are:
* <code>c</code> Create the archive
* <code>c</code> create an archive
* <code>x</code> Extract the archive
* <code>x</code> extract an archive
* <code>v</code> Be verbose (display the files extracted/added)
* <code>t</code> list the contents (add <code>v</code> for details)
* <code>f filename</code> Filename to create or extract from
* <code>v</code> verbose: show each file as it's added or extracted
* <code>z</code> Compress using zlib (fast, decent compression)
* <code>f filename</code> the archive file to create or read
* <code>j</code> Compress using bzip2 (medium speed, better compression)
* <code>z</code> compress with gzip (fast, decent compression)
* <code>J</code> Compress using xz (medium speed, good compression)
* <code>j</code> compress with bzip2 (medium speed, better compression)
* <code>J</code> compress with xz (slower, best compression)
 
Examples:
 
<code>tar czvf growlog.tar.gz GrowLog</code> creates a gzip-compressed archive of the GrowLog folder.


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.
<code>tar xzvf growlog.tar.gz</code> extracts it.
 
<code>tar tzvf growlog.tar.gz</code> lists what's inside.
 
Always include a compression flag when creating an archive. Plain tar doesn't compress at all; it just bundles files together, another holdover from its tape origins.


=== Working with a single file ===
=== Working with a single file ===
 
Sometimes you only need to compress one file to shrink it, for example to send a video over the internet. As a rule of thumb, the better the compression, the longer it takes. Each tool has its own options, but the basic form is <code>tool filename</code>, which replaces the file with a compressed version ending in the tool's extension.
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 <code>(tool) filename</code> which will create filename.exstention .


{| class="wikitable"
{| class="wikitable"
|+ Common Compression Utilities
|+ Common compression utilities
|-
|-
! Name
! Name
! Compress
! Decompress
! Speed
! Compression
! Compression
! Decompression
! Speed
! Compression Amount
! Extension
! Extension
|-
|-
! GZip (Zlib)
! gzip
| gzip
| gzip
| gunzip
| gunzip
| Fast
| Fast
| Medium/Low
| Moderate
| .gz
| .gz
|-
|-
! Bzip2
! bzip2
| bzip2
| bzip2
| bunzip2
| bunzip2
| Slow
| Slow
| High
| High
| .bz / .bz2
| .bz2
|-
|-
! Xz/LZW
! xz (LZMA)
| xz
| xz
| unxz
| unxz
| Medium/Slow
| Slowest
| Exceptional
| Highest
| .xz
| .xz
|}
|}
[[Category:Computers]]

Latest revision as of 13:00, 22 October 2026

Linux is a growing and popular operating system, especially among us citizen mycologists. Unknown to a lot of first-time users, it comes with an extensive set of tools that can do things that feel like black magic on Windows, or even to the casual Mac user.

As a macOS user, you have access to most of these commands too (and surprisingly, without paying an exorbitant fee for them). macOS descends from BSD (the Berkeley Software Distribution), an offshoot of the original AT&T Unix, so most of the core shell commands are at your disposal.

Pipes, Redirection, and Sub-shells

Standard streams

Every process in a *nix shell starts with three open streams, called file descriptors: one for input and two for output. Windows implements the same idea.

Standard file descriptors
Name FD Description
stdin 0 Standard input: what the command reads, either typed at the terminal or fed from another command.
stdout 1 Standard output: the command's normal output. This is what normally gets printed to the terminal.
stderr 2 Standard error: error messages and anything the program wants the user to see even when its output is being redirected or piped.

Pipes

The pipe symbol | (usually Shift + backslash) sends the stdout of the command on the left into the stdin of the command on the right. You can chain as many as you like, which makes for very powerful text processing.

Example: list any files from December

ls -l | grep -iw Dec

Example: count how many of those there are

ls -l | grep -iw Dec | wc -l

Redirection

Redirection connects a stream to a file instead of the terminal.

Syntax What it does
command > file Write stdout to file, replacing whatever was in it
command >> file Append stdout to the end of file
command < file Read stdin from file
command 2> file Write stderr to file
command > file 2>&1 Write both stdout and stderr to file
command > /dev/null Throw the output away

Example: save a list of every photo in your grow log folder

ls ~/GrowLog/*.jpg > photos.txt

Note the order in > file 2>&1: it means "send stdout to the file, then send stderr wherever stdout is going." Written the other way around, stderr still goes to the terminal.

Sub-shells

A sub-shell runs commands in a separate copy of the shell.

Command substitution runs a command and drops its output into another command:

echo "Today is $(date +%A)"

mkdir "grow-$(date +%Y-%m-%d)" creates a folder named for today's date.

You may also see the older backtick form, `date`; $( ) is easier to read and can be nested.

Grouping with parentheses runs a set of commands together in a sub-shell. Changes such as cd don't affect your current shell:

(cd ~/GrowLog && ls) lists that folder and leaves you where you started.

You can also redirect a whole group at once:

(date; ls ~/GrowLog) > log.txt

File Compression and Extraction

There are two main utilities for packing a whole directory into a single file, so you can move it to another computer at home or over the internet and unpack it there. Which one to use mostly depends on the computers on each end.

  • zip/unzip: built into Microsoft Windows since Windows XP, as well as macOS and most Linux systems
  • tar: standard on Linux and every Unix-like system, including macOS. Recent Windows 10 and 11 releases include it too.

zip/unzip

Compression

The zip command packs files into a zip archive, which can be opened almost anywhere. The general format is

zip [flags] destination[.zip] file list

The .zip extension is optional. To zip a list of files in the same directory:

zip filename.zip file1 file2 file3 ...

To zip an entire directory structure, use the -r (recursive) flag. It descends through the whole structure, adding everything it finds:

zip -r docs.zip Documents

Extraction

The unzip command does the opposite and extracts files. It has flags for special situations, but there's really only one you need day to day:

-l lists the contents instead of extracting.

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

unzip zipfile.zip extracts the files into the current directory.

tar

The tar command works much like zip but supports several kinds of compression. The name stands for "Tape ARchive," from the early days when computers literally used reel-to-reel tape.

tar [flags] archive [file1 file2 file3]

The most commonly used flags are:

  • c create an archive
  • x extract an archive
  • t list the contents (add v for details)
  • v verbose: show each file as it's added or extracted
  • f filename the archive file to create or read
  • z compress with gzip (fast, decent compression)
  • j compress with bzip2 (medium speed, better compression)
  • J compress with xz (slower, best compression)

Examples:

tar czvf growlog.tar.gz GrowLog creates a gzip-compressed archive of the GrowLog folder.

tar xzvf growlog.tar.gz extracts it.

tar tzvf growlog.tar.gz lists what's inside.

Always include a compression flag when creating an archive. Plain tar doesn't compress at all; it just bundles files together, another holdover from its tape origins.

Working with a single file

Sometimes you only need to compress one file to shrink it, for example to send a video over the internet. As a rule of thumb, the better the compression, the longer it takes. Each tool has its own options, but the basic form is tool filename, which replaces the file with a compressed version ending in the tool's extension.

Common compression utilities
Name Compress Decompress Speed Compression Extension
gzip gzip gunzip Fast Moderate .gz
bzip2 bzip2 bunzip2 Slow High .bz2
xz (LZMA) xz unxz Slowest Highest .xz