Linux

Linux

Linux is a family of free and open-source software operating systems built around the Linux kernel. Typically, Linux is packaged in a form known as a Linux distribution for both desktop and server use
Joshua Yates

Joshua Yates

1653102540

Linux dnf Command Explained with Examples

Linux Crash Course - The dnf Command

There's a handful of popular package managers that are used on various  Linux distributions, and dnf is among the most popular. It's the default package manager on distros such as Red Hat, CentOS, AlmaLinux, Rocky Linux, Fedora, among others. In this video, we'll explore the dnf command and see examples of basic usage.

## Individual sections
00:00 - Intro
01:04 - Sponsor for this video: Linode
02:30 - What is "dnf"?
03:14 - Installing a single package with dnf
07:45 - Using dnf to search for a package to install
09:45 - Listing installed packages
11:10 - Listing packages that have been recently updated
12:06 - Installing more than one package at a time
15:48 - Uninstalling packages with dnf
17:39 - Updating a package to the latest version
18:47 - Updating all installed packages to the latest version
20:41 - Setting up automatic updates with dnf-automatic
24:00 - Reinstalling a package with dnf
25:29 - Removing unused dependencies

#linux 

Linux dnf Command Explained with Examples
郝 玉华

郝 玉华

1653066000

Linux 指令教學 - cp

要將一個檔案複製到一個目錄,我們使用 cp 命令。 cp 命令的第一個引數是要複製的檔名,第二個引數是需要複製的檔案所在目錄的絕對路徑或相對路徑。 它將當前工作目錄中的檔案 abc.txt 複製到當前工作目錄 test 的資料夾中。 如果要將特定目錄下的檔案複製到與父檔案不同的目錄下,我們可以將檔案的名稱指定為。

複製資料夾

cp -r path_to_source/ path_to_destination/

-r -R 代表 recursive 遞迴,

如果 path_to_destination 不存在,會自動建立 ;

如果存在,則直接使用。

只想複製資料夾底下的全部內容,

cp -r dir_1/. dir_2
cp -r dir_1/. .

. 代表資料夾內的東西,也可以代表目前所在的地方。

有時候會希望複製時可以保存當時的權限,所以會加上 -p

cp -r --preserve=all path_to_source/ path_to_destination/

-p --preserve 代表一同複製當下的權限以及擁有者之類的。

其他的參數說明(參數可以多個一起使用),

互動模式 , CLI 會詢問你是否 overwriting files

cp -i source_file path_to_destination/

不詢問 , 直接 overwriting files

cp -n source_file path_to_destination/

只更新來源資料夾和目的地不同的檔案

cp -u source_file path_to_destination/

印出資訊

cp -v source_file path_to_destination/

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note

#linux 

Linux 指令教學 - cp
郝 玉华

郝 玉华

1653033600

Linux 指令教學 - rm

rm -r :recursive遞迴刪除,這個參數要小心使用,因為刪掉是整個資料夾一起砍

 rm -d :直接刪除目錄,目錄裡面不能有檔案或是資料夾。

rm -i :刪除之前會詢問,如果要刪除建議使用這個參數,可以避免誤刪。

rm -f :強制刪除檔案或目錄,不會有警告,請小心使用。

刪除檔案

rm file.md

刪除資料夾

rm -rf mydir

-r 代表使用 recursive 遞迴刪除。 ( 會將目錄內所有檔案刪除 )

-f 代表強制刪除 ( 不會跳出警告 )。

或是使用 rmdir 指令,

rmdir mydir_name

不過要注意,被移除的資料夾裡面必須是空的,否則回無法移除。

刪除特定的副檔名,

rm -f *.zip

也可以這樣

rm -f *demo.zip

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note

#linux 

Linux 指令教學 - rm
郝 玉华

郝 玉华

1653014649

Linux 指令教學 - mv

mv 是在Linux (或其他Unix 系統) 最常用的指令之一, 它是用作移動/更名檔案或目錄的指令。 mv 的常用參數: -f, –force: 如果目標檔案已經存在,不會提出詢問,而會直接強制覆蓋檔案。 -i, –interactive: 如果目標檔案已經存在,會先提出詢問,不會直接強制覆蓋檔案。

move ( rename ) files,移動檔案或是重新命名檔案

修改 資料夾 or 檔案 檔名

mv folder folder-new
mv README.md README_MV.md

移動檔案

mv README.md /examples
mv file.md example/

其他的參數說明(參數可以多個一起使用),

互動模式 , CLI 會詢問你是否 overwriting files

mv -i source_file path_to_destination/

只更新來源資料夾和目的地不同的檔案

mv -u source_file path_to_destination/

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note

#linux 

Linux 指令教學 - mv
郝 玉华

郝 玉华

1653003720

Linux 指令教學 - ls

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

這裡敘述了 Linux 系統中 ls 指令的各種用法,並蒐集了各式各樣的常用範例。ls 是 Linux 系統上最常被使用的指令之一,通常我們開啟終端機之後,第一個會執行的指令就是 ls。我們可能幾乎天天都在使用它,但是有些參數可能還是不熟悉如何使用,以下我們介紹各種 ls 指令的用法,並且提供一些常用範例作為參考。
 

列出檔案

ls -l

-l 顯示詳細的資訊 ( 檔案權限 )。

也等於直接輸入 (L 的小寫)

ll

在 Linux 中,檔案都擁有四種權限

可讀取 ( r,Readable ),用數字 4 表示。

可寫入 ( w,writable ),用數字 2 表示。

可執行 ( x,eXecute ),用數字 1 表示。

無權限 ( - ),用數字 0 表示。

為了更清楚,我把它整理成表格😋

字元權限分數
r (read)4
w (write)2
x (execute)1
- 無權限0

如下圖所示,

alt tag

接著說明裡面每一欄的意思,

alt tag

  • 第一欄 ( 圖上編號 1 ),使用者權限。

由 10 個字元組成,

第一個字元代表檔案型態 (- 為檔案,d 為目錄,l 為連結檔案 )。

第二、三、四個字元 表示檔案擁有者的存取權限。

第五、六、七個字元 表示檔案擁有者所屬群組成員的存取權限。

第八、九、十個字元 表示其他使用者的存取權限。

來看一個例子,drwxr-xr-x,

代表它是一個目錄,

擁有者具備讀、寫、執行權限,

所屬群組只擁有讀、執行權限,

其他使用者只擁有讀、執行權限。

為了更清楚,我把它整理成表格😋

 擁有者所屬群組其他使用者
drwxr-xr-x
代表是一個目錄具備讀、寫、執行權限只擁有讀、執行權限只擁有讀、執行權限

它的權限分數是 755

身份權限分數
ownerrwx4+2+1 =7
groupr-x4+0+1 =5
othersr-x4+0+1 =5

第二欄 ( 圖上編號 2 ),檔案數量。

第三欄 ( 圖上編號 3 ),擁有者。

第四欄 ( 圖上編號 4 ),群組。

第五欄 ( 圖上編號 5 ),檔案大小。

第六欄 ( 圖上編號 6 ),檔案建立時間。

第七欄 ( 圖上編號 7 ),檔案名稱。

ls 使用時間排序

ls -t

列出特定檔案 ( 列出為 .py 的檔案 )

ls *.py

-h 參數,使用 KB、MB、GB 單位顯示檔案或目錄大小。

ls -l -h

顯示全部的檔案 (包含隱藏檔)

ls -a

也可以使用

ls -al

可以直接列出資料夾內的內容

ls Downloads

像是在 home 底下, 直接列出 Downloads 內容

alt tag

sort

ls -S

將輸出結果 stdout 寫到文件裡, 可使用 redirect > (螢幕上不會顯示)

ls -lS > file.txt

計算路徑底下有多少個檔案

ls | wc -l

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note

 #linux 

Linux 指令教學 - ls
郝 玉华

郝 玉华

1652992560

Windows to Linux 優缺點 - 4 個月體驗

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

從 Windows 切換到 Linux (日常作業 ubuntu 18.04) 也已經 4 個月了,

還有個 VM 環境是 Debian 10 (KDE), 開發專用, Debian 真的穩到爆😆

來分享一下從 Windows -> Linux 的優缺點以及使用心得😄

先說切換到 Linux 的優點

  1. 自由度(客製化)超級高, 尤其是那個 DE, 真的超級好玩😄

你喜歡/不喜歡什麼軟體都可以找其他的來取代, 快捷鍵想怎麼設定就怎麼設定,

有一種完全的掌握系統的感覺, 想幹嘛就幹嘛, 自由度極高。

延伸閱讀 Linux 桌面環境 Desktop Environment

  1. Linux 使用資源相對 Windows 少很多, 尤其是在 8GB Ram (又無法擴充的環境下),

儘管我是使用 GNOME 這個比較吃資源的 DE, 使用起來還是比 Windows 輕便且有更多的資源可使用,

如果你有比較舊的電腦(筆電), 都可以考慮安裝 Linux (安裝比較輕量的 distro)。

延伸閱讀 認識 Linux 發行版

  1. Linux 穩定又可靠, 且不會強迫你要更新系統, 尤其是 Windows 都強迫自動更新, 然後 Linux 更新

除非是很重大的核心更新,不然基本上更新完是不需要重開機的, 也就是說你的電腦完全不用關機,

隨時隨地都可以馬上進入工作, 不會像 Windows 等你回到座位, 你就發現他被重開機了😓

但為了愛地球, 長時間不使用還是建議關機😄

Linux 不管在哪裡都可以安裝, 尤其是在 server 中, 穩定性以及可靠性高絕對是首選😏

Linux 是 free 而且開源的, 也就代表幾乎不需要付費, 也因為是開源的, 可能會比較多人去檢查 code,

也可能會比 Windows(封閉) 相對安全。

  1. Linux 不需要裝防毒軟體 (以前 Windows 我也只使用內建的防毒軟體), 但不代表 Linux 不會中毒

(雖然我還沒發生過XD), 而且 Linux 應該比 Windows 安全。

  1. 對軟體工程師來說, 很多事情變簡單了😄 有些東西在 Linux 上很簡單的一件事, 在 Windows

上超級無敵霹靂奇怪😠 像是 k8s 的安裝, 又或是 docker 在 Windows 上的安裝(一定要專業版),

有時候真的會讓自己很煩😞

Linux(Ubuntu) 有 LTS, 也就是說有長期支援, 如果沒特別的需求, 可以穩穩用5年。

Linux 上的 terminal (zsh+tmux) 好用非常多

延伸閱讀 Ubuntu 如何安裝 Oh My ZSH + tmux

  1. Linux 不用碎片整理, 相信大家在 Windows 上都聽過磁碟重整(碎片整理), 會需要這個是因為

Windows 是使用 NTFS, 而現在大多數的 Linux 都是使用 ext4 (他不需要碎片整理)。

很多好用的工具只有在 Linux 才有😆

不用擔心遇到問題找不到答案, 因為 Linux 社群是非常活躍的😆

Linux 讓電腦變得更有趣了😆 讓使用電腦變成一種學習

切換到 Linux 的缺點

  1. 需要中文輸入法的人會覺得在 Linux 上中文輸入法比較不好用 (被 Windows 寵壞了😅)

延伸閱讀 Linux 中更像微軟更好用的中文輸入法 hime

  1. 玩特定遊戲變得稍微比較麻煩, 通常都要額外的設定 (但對我而言, 換 Linux 就是要戒掉遊戲)😝

當然也有一些 distro 是比較適合玩遊戲的. 如果你都玩 Steam 上的遊戲, 在 Linux 應該是完全沒問題的😄

如果常常需要使用金融卡, 像是線上 ATM 那類的, 蠻多銀行不支援 Linux , 真的有點麻煩😣

驅動程式偶爾需要自己另外去抓, 因為 Linux 安裝驅動的方式和 Windows 不太一樣, Windows 會包山包海一次

幫你全裝, 但 LINUX 也已經支援很多了, 像是 linux-firmware 這個 package.

如果你是 Microsoft Office 重度使用者可能會有點麻煩, 在 Linux 上只有 LibreOffice 或線上版可以使用。

這邊應該不算缺點, 算是小小的抱怨😂 遠端機器的部份我使用過 AnyDesk, TeamViewer, VNC Server-RealVNC

(都是個人使用), 但 Linux 上如果安裝 VNC Server-RealVNC 似乎會變成要收費? ( Windows 上卻是免費), 但也沒關係,

因為 Linux 上 server 端或 client 端的遠端軟體非常的多, 但多數都有一個限制, 就是必須要在內網裡(或是有固定ip).

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note/tree/master/linux-is-better-than-windows

 #linux #windows 

Windows to Linux 優缺點 - 4 個月體驗
Elian  Harber

Elian Harber

1652955900

Ergo Proxy: The Reverse Proxy Agent for Local Domain Management

Ergo 

Ergo Proxy - The reverse proxy agent for local domain management.

The management of multiple apps running over different ports made easy through custom local domains.

Demo

See more on examples

Summary

Philosophy

Ergo's goal is to be a simple reverse proxy that follows the Unix philosophy of doing only one thing and doing it well. Simplicity means no magic involved. Just a flexible reverse proxy which extends the well-known /etc/hosts declaration.

Feedback

This project is constantly undergoing development, however, it's ready to use. Feel free to provide feedback as well as open issues. All suggestions and contributions are welcome. :)

For help and feedback you can find us at #ergo-proxy channel on https://gopher.slack.com

Why?

Dealing with multiple apps locally, and having to remember each port representing each microservice is frustrating. I wanted a simple way to assign each service a proper local domain. Ergo solves this problem.

Installation

Important These are the only official ways to install ergo.

OSX

brew tap cristianoliveira/tap
brew install ergo

Linux

curl -s https://raw.githubusercontent.com/cristianoliveira/ergo/master/install.sh | sh

Windows

From powershell run:

Invoke-WebRequest https://raw.githubusercontent.com/cristianoliveira/ergo/master/install.ps1 -out ./install.ps1; ./install.ps1

You can also find the Windows executables in release.

Disclaimer: I use Unix-based systems on a daily basis, so I am not able to test each build alone. :(

Go

go install github.com/cristianoliveira/ergo

Make sure you have $GOPATH/bin in your path: export PATH=$PATH:$GOPATH/bin

Usage

Ergo looks for a .ergo file inside the current directory. It must contain the names and URL of the services following the same format as /etc/hosts (domain+space+url). The main difference is it also considers the specified port.

Simplest Setup

You need to set the http://127.0.0.1:2000/proxy.pac configuration on your system network config.

Ergo comes with a setup command that can configure it for you. The current systems supported are:

  • osx
  • linux-gnome
  • windows
ergo setup <operation-system>

In case of errors / it doesn't work, please look at the detailed config session below.

Adding Services and Running

OS X / Linux

echo "ergoproxy http://localhost:3000" > .ergo
ergo run

Now you should be able to access: http://ergoproxy.dev. Ergo redirects anything ending with .dev to the configured url.

Windows

You should not use the default .dev domain, we suggest .test instead (see #58) unless your service supports https out of the box and you have already a certificate

set ERGO_DOMAIN=.test
echo ergoproxy http://localhost:3000 > .ergo
ergo list # you shouldn't see any quotas in the output
ergo run

Now you should be able to access: http://ergoproxy.test. Ergo redirects anything ending with .test to the configured url.

Simple, right? No magic involved.

Do you want to add more services? It's easy, just add more lines in .ergo:

echo "otherservice http://localhost:5000" >> .ergo
ergo list
ergo run

Restart the ergo server and access: http://otherservice.dev

ergo add otherservice http://localhost:5000 is a shorthand for appending lines to ./.ergo

Ergo's configuration

Ergo accepts different configurations like run in different port (default: 2000) and change domain (default: dev). You can find all this configs on ergo's help running ergo -h.

Configuration

In order to use Ergo domains you need to set it as a proxy. Set the http://127.0.0.1:2000/proxy.pac on:

Networking Web Proxy

OS X

Network Preferences > Advanced > Proxies > Automatic Proxy Configuration

Windows

Settings > Network and Internet > Proxy > Use setup script

Linux

On Ubuntu

System Settings > Network > Network Proxy > Automatic

For other distributions, check your network manager and look for proxy configuration. Use browser configuration as an alternative.

Browser configuration

Browsers can be configured to use a specific proxy. Use this method as an alternative to system-wide configuration.

Keep in mind that if you requested the site before setting the proxy properly, you have to reset the cache of the browser or change the name of the service. In incognito windows cache is disabled by default, so you can use them if you don't wish to delete the cache

Also you should not use the default .dev domain, we suggest .test instead (see #58) unless your service supports https out of the box and you have already a certificate

Chrome

Exit Chrome and start it using the following option:

# Linux
$ google-chrome --proxy-pac-url=http://localhost:2000/proxy.pac

# OS X
$ open -a "Google Chrome" --args --proxy-pac-url=http://localhost:2000/proxy.pac

Firefox

through menus and mouse

  1. Click the hamburger button otherwise click on "Edit" Menu
  2. then "Preferences"
  3. then "Settings" button at the bottom of the page ("General" active in sidebar) with title "Network Settings"
  4. check Automatic Proxy configuration URL and enter value http://localhost:2000/proxy.pac below
  5. hit "ok"

from about:config

network.proxy.autoconfig_url -> http://localhost:2000/proxy.pac

Using on terminal

In order to use ergo as your web proxy on terminal you must set the http_proxy variable. (Only for linux/osx)

export http_proxy="http://localhost:2000"

Ephemeral Setup

As an alternative you can see the scripts inside /resources for running an ephemeral setup. Those scripts set the proxy only while ergo is running.

Contributing

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request, they are welcome!
  • Please include unit tests in your pull requests

Development

Minimal required golang version go1.17.6. We recommend using GVM for managing your go versions.

Then simply run:

gvm use $(cat .gvmrc)

Building

  make all

Testing

make test
make test-integration # Requires admin permission so use it carefully.

Author: Cristianoliveira
Source Code: https://github.com/cristianoliveira/ergo 
License: MIT license

#go #golang #windows #linux 

Ergo Proxy: The Reverse Proxy Agent for Local Domain Management

A Simple Package to Execute Shell Commands on Linux, Windows & Osx

cmd package

A simple package to execute shell commands on linux, darwin and windows.

Installation

$ go get -u github.com/commander-cli/cmd@v1.0.0

Usage

c := cmd.NewCommand("echo hello")

err := c.Execute()
if err != nil {
    panic(err.Error())    
}

fmt.Println(c.Stdout())
fmt.Println(c.Stderr())

Configure the command

To configure the command a option function will be passed which receives the command object as an argument passed by reference.

Default option functions:

  • cmd.WithStandardStreams
  • cmd.WithCustomStdout(...io.Writers)
  • cmd.WithCustomStderr(...io.Writers)
  • cmd.WithTimeout(time.Duration)
  • cmd.WithoutTimeout
  • cmd.WithWorkingDir(string)
  • cmd.WithEnvironmentVariables(cmd.EnvVars)
  • cmd.WithInheritedEnvironment(cmd.EnvVars)

Example

c := cmd.NewCommand("echo hello", cmd.WithStandardStreams)
c.Execute()

Set custom options

setWorkingDir := func (c *Command) {
    c.WorkingDir = "/tmp/test"
}

c := cmd.NewCommand("pwd", setWorkingDir)
c.Execute()

Testing

You can catch output streams to stdout and stderr with cmd.CaptureStandardOut.

// caputred is the captured output from all executed source code
// fnResult contains the result of the executed function
captured, fnResult := cmd.CaptureStandardOut(func() interface{} {
    c := NewCommand("echo hello", cmd.WithStandardStream)
    err := c.Execute()
    return err
})

// prints "hello"
fmt.Println(captured)

Development

Running tests

make test

ToDo

  • os.Stdout and os.Stderr output access after execution via c.Stdout() and c.Stderr()

Author: Commander-cli
Source Code: https://github.com/commander-cli/cmd 
License: MIT license

#go #golang #cli #windows #linux #shell 

A Simple Package to Execute Shell Commands on Linux, Windows & Osx
Gordon  Murray

Gordon Murray

1652934360

Path_provider_linux: The Linux Implementation Of Path_provider

path_provider_linux

The linux implementation of path_provider.

Usage

This package is endorsed, which means you can simply use path_provider normally. This package will be automatically included in your app when you do.

Use this package as a library

Depend on it

Run this command:

With Flutter:

 $ flutter pub add path_provider_linux

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  path_provider_linux: ^2.1.6

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import 'package:path_provider_linux/path_provider_linux.dart';

example/lib/main.dart

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider_linux/path_provider_linux.dart';

void main() {
  runApp(const MyApp());
}

/// Sample app
class MyApp extends StatefulWidget {
  /// Default Constructor
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String? _tempDirectory = 'Unknown';
  String? _downloadsDirectory = 'Unknown';
  String? _appSupportDirectory = 'Unknown';
  String? _documentsDirectory = 'Unknown';
  final PathProviderLinux _provider = PathProviderLinux();

  @override
  void initState() {
    super.initState();
    initDirectories();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initDirectories() async {
    String? tempDirectory;
    String? downloadsDirectory;
    String? appSupportDirectory;
    String? documentsDirectory;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      tempDirectory = await _provider.getTemporaryPath();
    } on PlatformException catch (e, stackTrace) {
      tempDirectory = 'Failed to get temp directory.';
      print('$tempDirectory $e $stackTrace');
    }
    try {
      downloadsDirectory = await _provider.getDownloadsPath();
    } on PlatformException catch (e, stackTrace) {
      downloadsDirectory = 'Failed to get downloads directory.';
      print('$downloadsDirectory $e $stackTrace');
    }

    try {
      documentsDirectory = await _provider.getApplicationDocumentsPath();
    } on PlatformException catch (e, stackTrace) {
      documentsDirectory = 'Failed to get documents directory.';
      print('$documentsDirectory $e $stackTrace');
    }

    try {
      appSupportDirectory = await _provider.getApplicationSupportPath();
    } on PlatformException catch (e, stackTrace) {
      appSupportDirectory = 'Failed to get documents directory.';
      print('$appSupportDirectory $e $stackTrace');
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) {
      return;
    }

    setState(() {
      _tempDirectory = tempDirectory;
      _downloadsDirectory = downloadsDirectory;
      _appSupportDirectory = appSupportDirectory;
      _documentsDirectory = documentsDirectory;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Path Provider Linux example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text('Temp Directory: $_tempDirectory\n'),
              Text('Documents Directory: $_documentsDirectory\n'),
              Text('Downloads Directory: $_downloadsDirectory\n'),
              Text('Application Support Directory: $_appSupportDirectory\n'),
            ],
          ),
        ),
      ),
    );
  }
}

Author: Flutter
Source Code: https://github.com/flutter/plugins/tree/main/packages/path_provider/path_provider_linux 
License: BSD-3-Clause license

#dart #flutter #linux 

Path_provider_linux: The Linux Implementation Of Path_provider

Cross platform (macOS/Linux/Windows/Android) clipboard package in Go

clipboard

Cross platform (macOS/Linux/Windows/Android/iOS) clipboard package in Go

import "golang.design/x/clipboard"

Features

  • Cross platform supports: macOS, Linux (X11), Windows, iOS, and Android
  • Copy/paste UTF-8 text
  • Copy/paste PNG encoded images (Desktop-only)
  • Command gclip as a demo application
  • Mobile app gclip-gui as a demo application

API Usage

Package clipboard provides cross platform clipboard access and supports macOS/Linux/Windows/Android/iOS platform. Before interacting with the clipboard, one must call Init to assert if it is possible to use this package:

// Init returns an error if the package is not ready for use.
err := clipboard.Init()
if err != nil {
      panic(err)
}

The most common operations are Read and Write. To use them:

// write/read text format data of the clipboard, and
// the byte buffer regarding the text are UTF8 encoded.
clipboard.Write(clipboard.FmtText, []byte("text data"))
clipboard.Read(clipboard.FmtText)

// write/read image format data of the clipboard, and
// the byte buffer regarding the image are PNG encoded.
clipboard.Write(clipboard.FmtImage, []byte("image data"))
clipboard.Read(clipboard.FmtImage)

Note that read/write regarding image format assumes that the bytes are PNG encoded since it serves the alpha blending purpose that might be used in other graphical software.

In addition, clipboard.Write returns a channel that can receive an empty struct as a signal, which indicates the corresponding write call to the clipboard is outdated, meaning the clipboard has been overwritten by others and the previously written data is lost. For instance:

changed := clipboard.Write(clipboard.FmtText, []byte("text data"))

select {
case <-changed:
      println(`"text data" is no longer available from clipboard.`)
}

You can ignore the returning channel if you don't need this type of notification. Furthermore, when you need more than just knowing whether clipboard data is changed, use the watcher API:

ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
for data := range ch {
      // print out clipboard data whenever it is changed
      println(string(data))
}

Demos

  • A command line tool gclip for command line clipboard accesses, see document here.
  • A GUI application gclip-gui for functionality verifications on mobile systems, see a document here.

Command Usage

gclip command offers the ability to interact with the system clipboard from the shell. To install:

$ go install golang.design/x/clipboard/cmd/gclip@latest
$ gclip
gclip is a command that provides clipboard interaction.

usage: gclip [-copy|-paste] [-f <file>]

options:
  -copy
        copy data to clipboard
  -f string
        source or destination to a given file path
  -paste
        paste data from clipboard

examples:
gclip -paste                    paste from clipboard and prints the content
gclip -paste -f x.txt           paste from clipboard and save as text to x.txt
gclip -paste -f x.png           paste from clipboard and save as image to x.png

cat x.txt | gclip -copy         copy content from x.txt to clipboard
gclip -copy -f x.txt            copy content from x.txt to clipboard
gclip -copy -f x.png            copy x.png as image data to clipboard

If -copy is used, the command will exit when the data is no longer available from the clipboard. You can always send the command to the background using a shell & operator, for example:

$ cat x.txt | gclip -copy &

Platform Specific Details

This package spent efforts to provide cross platform abstraction regarding accessing system clipboards, but here are a few details you might need to know.

Dependency

  • macOS: require Cgo, no dependency
  • Linux: require X11 dev package. For instance, install libx11-dev or xorg-dev or libX11-devel to access X window system.
  • Windows: no Cgo, no dependency
  • iOS/Android: collaborate with gomobile

Screenshot

In general, when you need test your implementation regarding images, There are system level shortcuts to put screenshot image into your system clipboard:

  • On macOS, use Ctrl+Shift+Cmd+4
  • On Linux/Ubuntu, use Ctrl+Shift+PrintScreen
  • On Windows, use Shift+Win+s

As described in the API documentation, the package supports read/write UTF8 encoded plain text or PNG encoded image data. Thus, the other types of data are not supported yet, i.e. undefined behavior.

Who is using this package?

The main purpose of building this package is to support the midgard project, which offers clipboard-based features like universal clipboard service that syncs clipboard content across multiple systems, allocating public accessible for clipboard content, etc.

To know more projects, check our wiki page.

Author: Golang-design
Source Code: https://github.com/golang-design/clipboard 
License: MIT license

#go #golang #android #linux #ios #windows 

Cross platform (macOS/Linux/Windows/Android) clipboard package in Go
郝 玉华

郝 玉华

1652655900

在 Docker Compose 中如何善用 Environment Variables

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

在 docker compose 中如何善用 Environment variables✏️

Environment variables in Compose

Environment variables 可以設定在很多地方, 假如同時出現多個環境變數, 優先順序如下,

  1. Compose file
  2. Shell environment variables
  3. Environment file
  4. Dockerfile
  5. Variable is not defined

先來看 Shell environment variables

export ODOO_TAG=12.0
export POSTGRES_TAG=10.9

alt tag

本影片 GitHub repo ▶ https://github.com/twtrubiks/docker-tutorial/tree/master/docker-env-tutorial

#docker #linux 

在 Docker Compose 中如何善用 Environment Variables
郝 玉华

郝 玉华

1652655660

Linux 教學 - hime 超好用中文輸入法

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

剛換到 Ubuntu 時最不習慣的就是 Ubuntu 的注音輸入法了,經過了一段時間的研究,找到了 Hime 輸入系統的詞音輸入法,用起來最接近 Windows 的微軟注音,以下介紹在 Ubuntu 18.04 中如何安裝及設定 Hime 輸入法,Ubuntu 16.04 的設定方式也是類似的。

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note

#linux 

Linux 教學 - hime 超好用中文輸入法
郝 玉华

郝 玉华

1652634300

Linux 教學 - Git 忽略 File Mode (Chmod) 改變

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

因為小弟覺得這東西蠻有趣的,所以就簡單寫個教學文,順便記錄一下📝,希望能幫助想學的人😄

如果教學有誤再請糾正😅

基本使用指令以及安裝可參考小弟之前拍的影片 github基本教學 - 從無到有

影片教學包含如何產生 SSH key

如果步驟正確且沒出錯誤,可以在路徑下找到 .ssh資料夾,裡面有 id_rsa 以及 id_rsa.pub 兩個檔案,

這兩個就是 SSH Key, id_rsa是私鑰 ,不能洩露出去, id_rsa.pub是公鑰 ,可以很放心的告訴任何人。

本影片 GitHub repo ▶ https://github.com/twtrubiks/Git-Tutorials

#python #linux #git 

Linux 教學 - Git 忽略 File Mode (Chmod) 改變
郝 玉华

郝 玉华

1652626140

Linux 教學 - TLP 進階電源管理工具 Linux Advanced Power Management

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

TLP - Linux Advanced Power Management

TLP 是一款進階電源管理的工具, 如果沒特別需求, 你甚至可以在安裝完它之後,

直接忘記他的存在, 然後你會發現你的電池續航力提昇了不少😆

但有幾個地方要注意❗❗

首先是沒有 GUI 界面可以調整, 再來是筆電比較適合使用😄

安裝文件可參考 tlp-installation,

Ubuntu 安裝方法(其他 distro 請參考上方連結),

先加入 Package Repository (建議執行此步驟, 確保安裝版本是最新的)

sudo add-apt-repository ppa:linrunner/tlp
sudo apt update

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note/tree/master/linux-tlp-tutorial

#python #linux 

Linux 教學 - TLP 進階電源管理工具 Linux Advanced Power Management
郝 玉华

郝 玉华

1652615160

Linux 指令教學 - Chmod (Change Mode)

熱愛 Python 以及 Linux, 希望透過寫程式為社會貢獻,喜歡拍程式教學影片幫助想學習的人。

chmod命令来自于英文词组”change mode“的缩写,其功能是用于改变文件或目录权限的命令。默认只有文件的所有者和管理员可以设置文件权限,普通用户只能管理自己文件的权限属性。

设置权限时可以使用数字法,亦可使用字母表达式,对于目录文件建议加入-R参数进行递归操作,意味着不仅对于目录本身,也对目录内的子文件/目录都进行新权限的设定。

chmod 為 change mode 的縮寫.

改變檔案權限

chmod XXX filename

舉個例子,將權限設為 rw-rw-r--,

身份權限分數
ownerrw-4+2+0 =6
grouprw-4+2+0 =6
othersr--4+0+0 =4
chmod 664 README.md

常用修改權限的指令

# 只有擁有者 owner 有讀和寫的權限
sudo chmod 600 ×××
# 擁有者 owner 有讀和寫的權限,group,others 只有讀的權限
sudo chmod 644 ×××
# 擁有者 owner 有讀和寫以及執行的權限
sudo chmod 700 ×××
# 擁有者 owner,group,others 都有讀和寫的權限
sudo chmod 666 ×××
# 擁有者 owner,group,others 都有讀和寫以及執行的權限,基本上就是全開
sudo chmod -R 777 xxx

本影片 GitHub repo ▶ https://github.com/twtrubiks/linux-note
 

#python #linux 

Linux 指令教學 - Chmod (Change Mode)