Photo by Gabriel Heinzer / Unsplash

[จดบันทึก] หนังสือ Efficient Linux at the Command Line ตอนที่ 1

Learning Apr 7, 2023

สวัสดีครับ วันนี้ลองจดบันทึกการเรียน Linux Command Line จากการอ่านหนังสือ Efficient Linux at the Command Line ไว้ซักหน่อย ซึ่งผมอ่านออนไลน์ ผ่าน O'Reilly Online Learning ครับ เนื้อหาก็แนวจดโน๊ตจากการอ่าน เพื่อเอาไว้ทบทวนตัวเองแบบเร็วๆ เนื้อหาไม่ได้ละเอียดมากนัก และถ้าอยากลงรายละเอียด ก็กลับไปเปิดหนังสืออ่านต่อเพิ่มเติม

Efficient Linux at the Command Line
Take your Linux skills to the next level! Whether you’re a system administrator, software developer, site reliability engineer, or enthusiastic hobbyist, this practical, hands-on book will help you work faster, … - Selection from Efficient Linux at the Command Line [Book]
หนังสือ Efficient Linux at the Command Line

ส่วนตัว ผมพอมีพื้นฐานบ้างนิดหน่อย รู้คำสั่งเบื้องต้น และก็อาจจะมีภาษาอังกฤษปนๆ กันไปนะครับ บางครั้งก็ใช้ภาษาอังกฤษไปเลย น่าจะสะดวกและเข้าใจง่ายกว่า รวมถึงอาจมีข้อผิดพลาดบ้าง ก็ขออภัยด้วยนะครับ

เครื่องที่ผมใช้เรียน และลองพิมพ์ ลองฝึกตามหนังสือ คือ Macbook Pro นะครับ ใช้ ZSH

สำหรับวันนี้ ผมจดบันทึกไว้ 2 บทครับ คือ

  1. Combining Command
  2. Introducing The Shell

Chapter 1. Combining Commands

คำสั่งเกือบทั้งหมด จะมีแค่ อ่าน input จาก keyboard, พ่น output ออกหน้าจอ หรือทั้งคู่

  • stdin - อ่านออกเสียงว่า standard input หรือ standard in
  • stdout - อ่านว่า standard output หรือ standard out
# list directory /bin ด้วย long format
ls -l /bin

ข้อเสียของ ls คือถ้าข้อมูลมันยาวๆ เกินหน้าจอ มันจะ scroll ไปล่างสุด ทำให้เราไม่เห็นข้อมูลด้านบน ต้อง scroll ขึ้นไปดู มีอีกคำสั่ง ที่ใกล้เคียงกัน คือ

less myfile
# กด q เพื่อออกจากหน้า less

หรือ combine ด้วย ls แบบนี้

ls -l /bin | less
  • | คือ pipe symbol - มันจะทำการแปลง stdout ของคำสั่งแรก เป็น stdin ของคำสั่งที่ 2 หลัง |
  • command ไหนก็ตามที่มี | (pipe) จะเรียก pipeline

command มีทั้งหมด 3 ความหมาย

  • A program - คือ executable program พวก ls, cd เป็นต้น
  • A simple program - คือชื่อ program ตามด้วย argument เช่น ls -l /bin
  • A combined command - ก็คือการรวมคำสั่ง เช่น pipeline ls -l /bin | less

Six Commands to Get You Started

หัวข้อนี้ ใช้ตัวอย่างไฟล์ animals.txt

python	Programming Python	2010	Lutz, Mark
snail	SSH, The Secure Shell	2005	Barrett, Daniel
alpaca	Intermediate Perl	2012	Schwartz, Randal
robin	MySQL High Availability	2014	Bell, Charles
horse	Linux in a Nutshell	2009	Siever, Ellen
donkey	Cisco IOS in a Nutshell	2005	Boney, James
oryx	Writing Word Macros	1999	Roman, Steven

wc - word, line, character, and byte count

  • wc - เป็นคำสั่งที่เอาไว้ prints จำนวนบรรทัด, คำ และ ตัวอักษร (lines, word, characters) ในไฟล์
wc animals.txt

7      51     325 animals.txt
  • output คือ มี 7 lines, 51 words และ 325 characters
  • ใช้ -l , -w หรือ -c เพื่อ print output แต่ละแบบได้
# หาจำนวน fiis จาก directory (นับ line)
ls -1 | wc -l

head – display first lines of a file

  • head - เอาไว้ print บรรทัดแรกของไฟล์
# print 3 บรรทัดแรก
head -n3 animals.txt

cut – cut out selected portions of each line of a file

# print book titles จาก animals.txt
cut -f2 animals.txt
  • -f - cut by field , -f2 หมายถึง cut จาก field ที่ 2
# cut และ แสดงแค่ 3 บรรทัด
cut -f2 animals.txt | head -n3
# cut หลายๆ fields
cut -f1,3 animals.txt | head -n3
# cut by characters 1-3
cut -c1-3 animals.txt
# cut by delimiter
cut -f4 animals.txt | cut -d, -f1

grep - file pattern searcher

  • grep เอาไว้ค้นหา text
  • grep -w - option เอาไว้หา full word only
# ค้นหาคำในไฟล์ animals.txt
grep Nutshell animals.txt
# combine -> list /usr/bin -> ตัด char ตัวแรก -> หาคำว่า d และ word count line
ls -l /usr/lib | cut -c1 | grep d | wc -l

sort - sort or merge records (lines) of text and binary files

เรียงลำดับข้อมูลใหม่ (default คือ asc)

# default เรียงแบบ ASC
sort animals.txt

# เรียงแบบ DESC  (-r คือ reverse)
sort -r animals.txt

ตัวอย่าง ตัดเอาแค่ ปี แล้ว sort จากน้อยไปมาก  -n คือ numeric sort

cut -f3 animals.txt | sort -n

uniq – report or filter out repeated lines in a file

เอาไว้ลบคำซ้ำๆ (ตามชื่อเลย คือให้เป็น unique)

# output unique และแสดงจำนวนที่ซ้ำ
uniq -c

md5sum - compute and check MD5 message digest

เอาไว้คำนวนและเช็ค่า MD5 digest เช่น เพื่อหาไฟล์ซ้ำ

md5sum filename
# โชว์ checksum ของไฟล์ .png ทั้งหมด
md5sum *.png | cut -c1-32 | sort

# ตัดเอาที่ซ้ำออก -c นับจำนวนที่ซ้ำ
md5sum *.png | cut -c1-32 | sort | uniq -c

Chapter 2. Introducing the Shell

Variables

เช่น HOME คือ path linux home ของเรา หรือ USER ก็คือชื่อ username เรา

printenv HOME

print env USER

ใช้ Dollar Sign ( $) เพื่อ evaluates to the string เช่น

echo $HOME

echo $USER

การ initialize / assign variables

FILES="animals.txt"

cat $FILES

Alias

Alias - เอาไว้สร้าง shortcut command

# command แบบ no arguments
alias g=grep

# ถ้ามี arguments จะ required quotes.
alias ll="ls -l"

Redirect Input / Output

ใช้ เครื่องหมาย > เช่น redirect stdout ไปไว้ที่ไฟล์ หรือก็คือ เซฟ stdout ลงไฟล์นั่นเอง

grep Perl animals.txt > result

ใช้ >> เพื่อ append แทนที่การ override  >

grep Perl animals.txt >> result
# redirect stderr
cp nonexistent.txt file.txt 2> errors
# redirect stdout และ stderr ไฟล์เดียวกัน
echo This file exists > goodfile.txt
cat goodfile.txt nonexistent.txt &> all.output

Qutes และ Whitespace

ชื่อไฟล์ที่มี space มี 3 ทางเลือก ใช้ single quote, double quote หรือ backslash.

cat 'Efficient Linux Tips.txt'
cat "Efficient Linux Tips.txt"
cat Efficient\ Linux\ Tips.txt
  • single quote - อ่านทุกค่าใน quote ไม่สนใจ special meaning
echo '$HOME'
$HOME
  • double quote - คล้ายๆ Template literal ใน JavaScript คือสนใจพวก special characters เช่น $HOME จะหมายถึง directory home ของเรา
echo "Notice that $HOME is evaluated"

escape ด้วยการใช้ bashslash (ต้องการพิมพ์ $ จริงๆ ไม่ใช่ evaluate)

echo \$HOME
$HOME

echo "This messaeg is \"sort of \" interesting"

backslash ข้อดีอีกอย่าง คือเอาไว้ทำ pipelines ให้มันอ่านง่ายขึ้น (ส่วนใหญ่ผมเห็นพวก curl command ยาว)

cut -f1 grades \
  | sort \
  | uniq -c \
  | sort -nr \
  | head -n1 \
  | cut -c9

หา location ที่ program เราอยู่ด้วย which

which cp

which brew

ใช้ type จะได้ข้อมูลมากกว่า มี location, alias, function

type cp

type brew

Config file

พวก file configuration ต่างๆ เช่น set $PATH ตั้งค่า alias กำหนด variables ต่างๆ หากเรา ตั้งชื่อไฟล์ .bashrc หรือ ถ้าใช้ zsh แบบผม ก็จะเป็น .zshrc มันจะยังไม่เห็นผล (เห็นผล shell ครั้งหน้า) ต้องทำการ restart shell ใหม่ หรือให้มัน reload ด้วยคำสั่ง

source ~/.bashrc

# หรือ
source $HOME/.bash

สำหรับใครสนใจ zsh และวิธีการทำ Terminal / Command Line ให้เท่ๆ คูลๆ ลองดูเลย ของผม ก็ใช้ ZSH + PowerLevle10k ติดตั้งและ config ง่ายมากครับ

Oh My Zsh - a delightful & open source framework for Zsh
Oh My Zsh is a delightful, open source, community-driven framework for managing your Zsh configuration. It comes bundled with several helpful functions, helpers, plugins, themes, and a few things that make you shout... OH MY ZSH!
GitHub - romkatv/powerlevel10k: A Zsh theme
A Zsh theme. Contribute to romkatv/powerlevel10k development by creating an account on GitHub.

วันนี้อ่าน 2 บทพอ ไว้เดี๋ยวมีเวลากลับมาอ่านบทต่อไป และจนบันทึกต่อ

Happy Coding ❤️

Tags

Chai Phonbopit

เป็น Web Dev ทำงานมา 10 ปีหน่อยๆ ด้วยภาษา JavaScript, Node.js, React, Vue และปัจจุบันกำลังสนใจ Web3, Crypto และ Blockchain เขียนบล็อกที่ https://devahoy.com