├── .gitignore ├── README.md ├── bash ├── README.md └── downloading.sh ├── images └── interesting_topics │ ├── colabcode │ ├── 2021-06-25-15-53-07.png │ ├── 2021-06-25-15-55-09.png │ ├── 2021-06-25-16-01-04.png │ ├── 2021-06-25-16-09-01.png │ ├── 2021-06-25-16-13-03.png │ ├── 2021-06-25-16-28-34.png │ ├── 2021-06-25-16-36-17.png │ ├── 2021-06-25-16-37-47.png │ └── 2021-06-25-16-38-47.png │ └── window_terminal │ ├── wt_01.jpg │ └── wt_02.PNG ├── interesting_topics ├── colabcode.md └── window_terminal.md └── python └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | tests -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
😄 This repository houses a collection of handy code snippets that cater to various situations
3 | 4 | 5 | ## Some kind of snippets: 6 | 7 | - [Bash snippets](bash/README.md) 8 | - [Python snippets](python/README.md) 9 | 10 | ## Some interesting topics 11 | 12 | - [Use Colab/Kaggle in VS Code](interesting_topics/colabcode.md) 13 | - [Connect to Linux server easily with Window Terminal](interesting_topics/window_terminal.md) 14 | 15 | ## Citation 16 | 17 | The code can be stolen from this repo without any permission and all contributors are welcome but you should refer the link to this repo in your code likes the following. 18 | 19 | ```bash 20 | ### in your ~/.bashrc file ### 21 | # the code got from https://github.com/leminhnguyen/useful_snippets 22 | sizeof(){ 23 | # example: sizeof BOOK --> result: 895M BOOK/ 24 | du -h --max-depth=0 "$1" 25 | } 26 | ``` 27 | -------------------------------------------------------------------------------- /bash/README.md: -------------------------------------------------------------------------------- 1 | # Contains my personal snippets 2 | 3 | ## 1. Count the number of files in a folder 4 | For example: `fcount /home/nguyenlm/wavs --> 27` 5 | 6 | ```bash 7 | fcount() { ls -1q "$1" | wc -l; } 8 | ``` 9 | 10 | 11 | ## 2. Fix cuda remove completely problem 12 | 13 | ```sh 14 | sudo apt-get -o Dpkg::Options::="--force-overwrite" install --fix-broken 15 | ``` 16 | 17 | ## 3. Count the number of non-blank lines in the files 18 | For example: `nbl-count file.txt --> 10` 19 | 20 | ```bash 21 | nbl-count() = { grep -cve '^\s*$' "$1" } 22 | ``` 23 | 24 | ## 4. Run docker without sudo 25 | 26 | ```bash 27 | sudo chmod 666 /var/run/docker.sock 28 | ``` 29 | 30 | ## 5. Fix 2nd screen cannot be detected when reinstalling ubuntu 31 | 32 | ```bash 33 | sudo apt-get purge 'nvidia*' 34 | sudo add-apt-repository ppa:graphics-drivers 35 | sudo apt-get update 36 | sudo ubuntu-drivers autoinstall 37 | ``` 38 | 39 | ## 6. Fix Invalid MIT-MAGIC-COOKIE-1 keyUnable to init server and javafx 40 | Caused by: *java.lang.UnsupportedOperationException: Unable to open DISPLAY😳 Chạy Kaggle hoặc Colab trên Vscode
3 | 4 | 5 | Cơ chế hoạt động của cái này là sẽ dùng bên thứ 3 `ngrok` để connect `VS Code` và `Kaggle` (hoặc `Colab`). Ở hướng dẫn này thì mình dùng `Kaggle`. 6 | 7 | 8 | ### Step1: Tạo tài khoản trên ngrok tại địa chỉ https://dashboard.ngrok.com/signup sau đó copy cái authtoken ở trang Setup & Installation 9 | 10 |  11 | 12 | ### Step2: Tạo file notebook mới trên Kaggle sau đó cài thư viện colabcode (Google Colab tương tự), nhớ turn on cả GPU để server VS Code dùng được GPU 13 | 14 | ```sh 15 | !pip install colabcode 16 | ``` 17 | 18 |  19 | 20 | ### Step3: Kết nối tới server của ngrok 21 | 22 | ```python 23 | from colabcode import ColabCode 24 | ColabCode( 25 | port=10000, 26 | password="abcdef", 27 | authtoken="1uPXZm2sN4DJlzJpob6fso2r5Jy_4NAksdacaHkfmFGLxEZvG" 28 | ) 29 | ``` 30 | Thông tin tham số: 31 | - `port`: cổng kết nối tới ngrok 32 | - `password` (optional): password để connect tới server vscode 33 | - `authtoken`: token lấy từ ngrok tại B1, mục đích của cái token là giúp không bị limit connection tới ngrok 34 | 35 |  36 | 37 | Click vào cái đường dẫn `NgrokTunnel` sau đó vào vscode server (nếu có password thì hệ thống sẽ yêu cầu thêm nhập password) 38 | 39 |  40 | 41 | ### Step4: Thử nghiệm 42 | 43 | Mở folder bất kỳ, tạo thử file `test.python` (ở đây mình dùng folder `/home/nguyenlm/test` tự tạo bằng `mkdir -p /home/nguyenlm/test`). Ở đây check thành công sử dụng được GPU nhưng theo giới hạn của Kaggle thì chỉ dùng được 25-30h/tuần. 44 | 45 |  46 | 47 | Cài thử `nvtop`, ở đây là server Ubuntu 18.04 và có quyền root nên khi cài thì không cần `sudo` 48 | 49 | ```sh 50 | # install C dependencies 51 | apt install cmake libncurses5-dev libncursesw5-dev git 52 | 53 | # build nvtop 54 | git clone https://github.com/Syllo/nvtop.git 55 | mkdir -p nvtop/build && cd nvtop/build 56 | cmake .. 57 | make 58 | make install 59 | ``` 60 | 61 | Chạy thử `nvtop` 62 | 63 |  64 | 65 | Chạy thử `htop` 66 | 67 |  68 | 69 | Chạy thử `df -h` để check bộ nhớ 70 | 71 |  -------------------------------------------------------------------------------- /interesting_topics/window_terminal.md: -------------------------------------------------------------------------------- 1 |😎 Connect to Linux server with Window Terminal
3 | 4 | 5 | ### Download window terminal 6 | 7 | With window terminal we can use some common commands in Linux like `ls`, `cd`, `scp`, `ssh`... If you're familiar with Linux and want to use more commands, please install [WSL (Window Subsystem for Linux)](https://docs.microsoft.com/en-us/windows/wsl/install-win10?ranMID=46131&ranEAID=a1LgFw09t88&ranSiteID=a1LgFw09t88-wsO_vuOpJsLH4s92WtO5iw&epi=a1LgFw09t88-wsO_vuOpJsLH4s92WtO5iw&irgwc=1&OCID=AID2200057_aff_7806_1243925&tduid=%28ir__bl2gtjnvwgkfqzuf0iq621u0ze2xrkty3g0rltd200%29%287806%29%281243925%29%28a1LgFw09t88-wsO_vuOpJsLH4s92WtO5iw%29%28%29&irclickid=_bl2gtjnvwgkfqzuf0iq621u0ze2xrkty3g0rltd200) and itergrate with window terminal. 8 | 9 | Link download: [window terminal download](https://www.microsoft.com/en-us/p/windows-terminal/9n0dx20hk701?ranMID=46131&ranEAID=a1LgFw09t88&ranSiteID=a1LgFw09t88-DnkwG9hHnodeH5k2AklK2Q&epi=a1LgFw09t88-DnkwG9hHnodeH5k2AklK2Q&irgwc=1&OCID=AID2200057_aff_7806_1243925&tduid=%28ir__bl2gtjnvwgkfqzuf0iq621u0ze2xrkt13s0rltd200%29%287806%29%281243925%29%28a1LgFw09t88-DnkwG9hHnodeH5k2AklK2Q%29%28%29&irclickid=_bl2gtjnvwgkfqzuf0iq621u0ze2xrkt13s0rltd200&activetab=pivot:overviewtab) 10 | 11 | Screenshot from my computer: 12 | 13 |  14 | 15 | Screenshot get from official page: 16 | 17 |  18 | 19 | ### Connect to Linux server without password 20 | 21 | Create public key with `ssh-keygen`, this command will create a private key and a public key, places the public key to anywhere that you want connecting to and never shares your private key to someone. By default the private key will be in `C:\Users\username\.ssh\id_rsa` and the public key will be in `C:\Users\username\.ssh\id_rsa.pub`. Run the following command. 22 | 23 | ````sh 24 | ssh-keygen.exe 25 | ### output ### 26 | # Generating public/private rsa key pair. 27 | # Enter file in which to save the key (C:\Users\nguyenlm/.ssh/id_rsa): 28 | ```` 29 | 30 | Copy content of `C:\Users\username\.ssh\id_rsa.pub` to `~/.ssh/authorized_keys` file on the Linux server, if this file didn't exist just create the new one. See the following example: 31 | 32 | ````sh 33 | ### ~/.ssh/authorized_keys file 34 | ssh-rsa AAAAB3NzaC1...nguyenlm@DESKTOP-3OQO7KC 35 | 36 | ### we can place mutiple public keys to this file 37 | ssh-rsa key2...nguyenlm@DESKTOP-3OQO7KC 38 | ssh-rsa key3...nguyenlm@DESKTOP-3OQO7KC 39 | ... 40 | ```` 41 | 42 | Try to connect to Linux server, if you don't have the ssh key or something went wrong with your key, you should have to enter the password 43 | 44 | ````sh 45 | ssh -p port username@server_url 46 | ```` 47 | 48 | If all steps above were successfully completed, then you have never typed password anymore (also when connecting by) -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | # The table of contents 2 | [1. Multiprocessing with `p_tqdm` library](#multiprocessing-with-p_tqdm-library)