User Tools

Site Tools


linux:basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
linux:basics [2019/04/12 14:09]
mstraub [Filtering & Selecting]
linux:basics [2019/09/03 10:57]
mstraub [Compressing files]
Line 1: Line 1:
 This page covers basic usage of / working with Linux. This page covers basic usage of / working with Linux.
 +
 +A great resource are the man pages but also the [[https://​www.gnu.org/​software/​coreutils/​manual/​html_node/​index.html|official documentation of the GNU Coreutils]]
  
  
Line 479: Line 481:
  
 ==== Compressing files ==== ==== Compressing files ====
-For compressig ​files the zip formats gzip (.gz) and bzip2 (.bz2) are commonly used - especially ​the former.+For compressing ​files the zip formats gzip (.gz)bzip2 (.bz2) and xz (.xz, which uses LZMA compression) are commonly used.  
 + 
 +Gzip is the most commonly used one, but according to [[https://​www.rootusers.com/​gzip-vs-bzip2-vs-xz-performance-comparison|this in-depth comparison]] .xz is the superior format - it is fast to decompress, achieves high compression rates and also has reasonable compression times until level 2 or 3. 
 + 
 +The following commands (un)compress a single file and then remove the old (un)compressed file.
 <code bash> <code bash>
 gzip my.txt ​                         # creates the gzipped file my.txt.gz and remove my.txt gzip my.txt ​                         # creates the gzipped file my.txt.gz and remove my.txt
-gzip -d my.txt.gz ​                   # recreates my.txt and removes my.txt.gz; same as gunzip +gunzip ​my.txt.gz ​                    ​# recreates my.txt and removes my.txt.gz; same as gzip -d
-bzip2 my.txt ​                        # similar to gzip, but using bzip2 compression +
-bzip2 -d my.txt.gz ​                  # similar to gunzip; same as bunzip2+
 </​code>​ </​code>​
-All these commands ​(un)zip a single file and then remove ​the old (un)zipped file.+The commands ​''​bzip2''​ + ''​bunzip2'' ​and ''​xz''​ + ''​unxz''​ work in the same fashion.
  
 +For all zip formats there is a ''​cat''​-style alias which allows for the creation of an uncompressed data stream:
 <code bash> <code bash>
-gunzip ​              #​unzip,​ same as gzip -d +zcat                 # ​stream uncompressed gzip file to stdout, same as gzip -cd 
-zcat                 #unzip to stdout, same as gzip -cd+bzcat 
 +xzcat                # same as xz --decompress --stdout
 </​code>​ </​code>​
-The programs ''​gzip''​ and ''​bzip2''​ have two "​aliases":​ ''​gunzip''​ / ''​bunzip2''​ and ''​zcat''​ / ''​bzcat''​. These extra commands provide a short way to typically used functions.+
  
 ==== Compressed Archives ==== ==== Compressed Archives ====
Line 645: Line 651:
 ''​date''​ is a versatile tool for formatting dates / times and setting the system time ([[linux:​installation#​date_timezone|see also]]). ''​date''​ is a versatile tool for formatting dates / times and setting the system time ([[linux:​installation#​date_timezone|see also]]).
  
-Get the current ​system ​time+Print the current time in different formats
 <code bash> <code bash>
-date                      # print current system time (human readable)+date                      # default - a human readable ​format 
 +date --rfc-3339=date ​     # only the date - similar to ISO 8601 
 +date --rfc-3339=seconds ​  # date and time - similar to ISO 8601 
 +date +%Y-%m-%dT%H%M ​      # custom format useful for scripts, e.g. 2019-08-14T1129
 </​code>​ </​code>​
 +
 +Convert unix timestamps
 +<code bash>
 +date -d @1278923870 ​      # print the unix timestamp in a human readable format
 +</​code>​
 +
 +
 +
 ====== Working with Text ====== ====== Working with Text ======
  
Line 785: Line 802:
 </​code>​ </​code>​
  
-Two good trick to create readable regexes ​is to (1) escape the whole regex with single quotes and (2) use a different separator than ''/''​ when appropriate. See for yourself in this example of replacing all backslashes in a line with slashes.+Two good tricks ​to create readable regexes ​are to (1) escape the whole regex with single quotes and (2) use a different separator than ''/''​ when appropriate. See for yourself in this example of replacing all backslashes in a line with slashes.
  
 <code bash> <code bash>
Line 796: Line 813:
 <code bash> <code bash>
 sed -n 1~2p file.txt ​        #only print odd lines (=print line 1 and then print each line at step 2) sed -n 1~2p file.txt ​        #only print odd lines (=print line 1 and then print each line at step 2)
 +</​code>​
 +
 +By default ''​sed''​ regular expressions are limited, e.g. do not support matching groups. Use the switch ''​-E''​ to activate extended regular expressions and enjoy matching groups:
 +
 +<code bash>
 +sed -E '​s#​(.*),​(.*)#​\1;​\2#​g'​ #replace a single comma with a semicolon
 +</​code>​
 +
 +However, even with extended expressions ''​sed''​ does not support non-greedy expressions as it is possible with PCRE (perl compatible regular expressions). A good resort is to simply use ''​perl''​ itself:
 +
 +<code bash>
 +perl -pe '​s/​.*thevalue="​(.*?​)"​.*/​\1/​g'​ file.txt
 </​code>​ </​code>​
  
linux/basics.txt · Last modified: 2020/12/21 09:51 by mstraub