πŸ“Ž linkedin learning-linux-command-line


1. Linux Command Line

01_01 - File system structure

image

/boot

The boot directory contains important files needed by the boot loader.

/dev

The /dev directory contains special, virtual files representing hardware components like a mouse, keyboard, storage devices, etc., connected to your system.

/etc

The /etc directory contains vital system configuration files such as startup scripts, networking files, user account-related files, etc.

/bin

The /bin directory contains system commands and other executable programs. The ls command that you use to list out the subdirectories along with many other useful commands is located within the /bin directory.

/opt

The /opt directory contains optional software packages to facilitate better compatibility of certain applications. When you install a third-party application that is not available in the official distribution repository, its software code gets stored in the /opt directory.

/proc

The /proc directory is a pseudo-filesystem containing information about processes and kernel parameters. It is populated with data during boot-up and is cleaned when you shut down your Linux machine. The /proc directory is also home to system information such as memory usage, processor information, and so on.

/usr

The /usr directory contains most of the files, libraries, programs, and system utilities. The /bin folder is symbolically linked to /usr/bin. The same goes for the /sbin and /lib directories.

/var

The /var directory is the storage space for system-generated variable files, and it includes logs, caches, and spool files. The data in /var isn’t automatically deleted, so sysadmins can collect and investigate system logs if need be.

/media

When you connect any removable media device such as a USB thumb drive, CD, or DVD, Linux creates a subdirectory under /media where the contents of the device are laid out. This is usually done automatically by the system as soon as you plug the device in. When you remove the device, the system deletes the corresponding subdirectory.

/mnt

The /mnt directory is used to mount storage devices in the system temporarily. However, some Linux distributions also use /mnt as a permanent storage solution. Unlike /media, the storage device isn’t automatically mounted at /mnt by the system. Sysadmins have to manually mount a storage device and populate the file system table accordingly.

/lib

A library is a collection of pre-compiled code that executable binaries can use. In Linux, the /lib directory serves as the storage space for all libraries needed by the binaries in the /bin directory.

/sys

The /sys directory contains information about the various system components and drivers. It’s akin to /proc but structured differently. Sysadmins use /proc and /sys inter-changeably to collect data.

/run

The /run directory logs system information since boot time. You can find information about the daemons that are running, logged-in users, and more. The data stored in the /run directory can give you an idea of how the system resources are being utilized since startup.


02_04 - Finding help for commands

man ls

ls --help

apropos list

03_01 - The Linux file system

ls -l

file Documents

stat Documents

03_02 - The glob command

POSIX

|:–:|:–:| |[:alpha:] |Any letter, [A-Za-z]| |[:upper:] |Any uppercase letter, [A-Z]| |[:lower:] |Any lowercase letter, [a-z]| |[:digit:] |Any digit, [0-9]| |[:alnum:] |Any alphanumeric character, [A-Za-z0-9]| |[:xdigit:] |Any hexadecimal digit, [0-9A-Fa-f]| |[:space:] |A tab, new line, vertical tab, form feed, carriage return, or space| |[:blank:] |A space or a tab.| |[:print:] |Any printable character| |[:punct:] |Any punctuation character: ! β€˜ # S % & β€˜ ( ) * + , - . / : ; < = > ? @ [ / ] ^ _ { | } ~| |[:graph:] |Any character defined as a printable character except those defined as part of the space character class| |[:word:] |Continuous string of alphanumeric characters and underscores.| |[:ascii:] |ASCII characters, in the range: 0-127| |[:cntrl:] |Any character not part of the character classes: [:upper:], [:lower:], [:alpha:], [:digit:], [:punct:], [:graph:], [:print:], [:xdigit:]|

ls *[[:space:]]* # having space
> 'file txt'  'photo_1054_02-25 2021.png'
ls photo[[:punct:]]_* # having punctuation character
> 'photo__1076_03-17-2021.jpg'  'photo!_1085_01-06-2021.jpg'  'photo?_1079_03-20-2021.jpg'  photo}_1076_03-17-2021.jpg
ls photo[!'!']*107*.jpg # not having !(exclamation mark)
> 'photo?_1079_03-20-2021.jpg'   photo_1073_03-14-2021.jpg      photo__1078_03-19-2021.jpg
ls -d !(@(photo|video)*@(.jpg|.png)) # not havig machted pattern (!or^)
> Photo_1000_01-01-2021.jpg   photo.txt                  video_1025_01-26-2021.mpg
ls photo_10[67]?_*.jpg # zero or one (?)
> photo_1060_02-31-2021.jpg  photo_1066_03-07-2021.jpg  photo_1072_03-13-2021.jpg
ls photo_+(1)_*.jpg # one or more consecutively (+) 
> photo_11_04-11-2021.jpg  photo_1_01-11-2021.jpg
> which do not include 'photo_1101_01-12-2021.jpg'
ls photo_10@(1)[0-2]*.jpg # exactly one (@) which is deffrent from regx! regx dont specify '@'
> photo_1010_01-11-2021.jpg  photo_1011_01-12-2021.jpg  photo_1012_01-13-2021.jpg

03_03 - Escape characters and quotes

echo My       name is $USER # skip a white space 
> My name is nueees
echo "My       name is $USER"
> My       name is nueees
echo 'My name is $USER' # present character literally (pass to a command unaltered by the shell)
> My name is $USER

echo "My name is \$USER" # escape one special character 
> My name is $USER
echo '\' # escape a back slash character 
> \
echo My       name is $USER # skip a white space 
> My name is nueees

03_04 - Brace expansion

brace expansion은 new files λ§Œλ“€ λ•Œ
globλŠ” existing file 찾을 λ•Œ
just expanding text and passing it to the command to use it as an argument.
brace λ¨Όμ €μ“°κ³  glob λ§ˆμ§€λ§‰μ— 써야 함 (근데 또 μš”μ¦˜ 됨..) brace μ“Έ λ•Œ echo μ³μ„œ ν™•μΈν•˜λŠ” μŠ΅κ΄€, braceλŠ” quote ν•˜μ§€λ§κΈ°

touch photo{.jpg,.png}
ls photo{.jpg,.png}
> photo.jpg  photo.png
echo {1..100..2}
> 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
echo {a..z..2}
> a c e g i k m o q s u w y

use case

mkdir -p bracefiles/20{22..31}/{01..12}
tree bracefiles/
> β”œβ”€β”€ 2022
  β”‚   β”œβ”€β”€ 01
  β”‚   β”œβ”€β”€ 02
  β”‚   β”œβ”€β”€ 03
  β”‚   β”œβ”€β”€ 04
  β”‚   β”œβ”€β”€ 05
  β”‚   β”œβ”€β”€ 06
  β”‚   β”œβ”€β”€ 07
  β”‚   β”œβ”€β”€ 08
  β”‚   β”œβ”€β”€ 09
  β”‚   β”œβ”€β”€ 10
  β”‚   β”œβ”€β”€ 11
  β”‚   └── 12
  β”œβ”€β”€ 2023
  β”‚   β”œβ”€β”€ 01
  β”‚   β”œβ”€β”€ 02
  β”‚   β”œβ”€β”€ 03
  β”‚   β”œβ”€β”€ 04
  β”‚   β”œβ”€β”€ 05
  β”‚   β”œβ”€β”€ 06
  β”‚   β”œβ”€β”€ 07
  β”‚   β”œβ”€β”€ 08
  β”‚   β”œβ”€β”€ 09
  β”‚   β”œβ”€β”€ 10
  β”‚   β”œβ”€β”€ 11
  β”‚   └── 12
  β”œβ”€β”€ 2024
  β”‚   β”œβ”€β”€ 01
  ...

03_05 - Command and variable substitution

echo "My shell is $SHELL" # env λ³€μˆ˜ 좜λ ₯
> My shell is /bin/bash
env | grep SHELL # env λ³€μˆ˜μž„
> SHELL=/bin/bash

echo "Who am I $whoami" # env μ•„λ‹Œ ν•¨μˆ˜λΌμ„œ 좜λ ₯ μ•ˆλ¨
> Who am I 
which whoami # bin에 μžˆλŠ” ν•¨μˆ˜λ‘œ 처리됨
> /usr/bin/whoam

# env μ•„λ‹Œ ν•¨μˆ˜ 좜λ ₯ 방법
echo "Who am I $(whoami)" # dollar sign$ + parentheses() 
> Who am I nueees
echo "Who am I `whoami`"  # backtick`μ”Œμš°κΈ° 
> Who am I nueees

# nested command substitution
echo "Permissions for find are $(ls -l $(which find))"
> Permissions for find are -rwxr-xr-x 1 root root 320160 Feb 18  2020 /usr/bin/find

03_07 - Find files and Xargs

find . -name "do*"

echo "file1 file2 file3" | xargs touch 
ls
> file1  file2  file3
# touch file1 file2 file3 ν•˜λŠ” 것과 κ°™μŒ

# μ‹€ν–‰ 전에 터미널에 λͺ…령을 μΈμ‡„ν•˜λ €λ©΄ -t(--verbose) μ˜΅μ…˜
echo "file1 file2 file3" | xargs -t touch 

# μ‹€ν–‰ 전에 μ—¬λΆ€λ₯Ό λ¬»λŠ” λ©”μ‹œμ§€λ₯Ό ν‘œμ‹œν•˜λ €λ©΄ -p(--interactive) μ˜΅μ…˜
echo  "file1 file2 file3" | xargs -p touch
> touch file1 
> touch file2
> touch file3

# μ§€μ •λœ λͺ…령에 전달할 인수 수λ₯Ό μ§€μ •ν•˜λ €λ©΄ -n(--max-args) μ˜΅μ…˜
echo  "file1 file2 file3" |  xargs -n 1 -t touch

# μ—¬λŸ¬ λͺ…령을 μ‹€ν–‰ν•˜λ €λ©΄ -I(--replace) μ˜΅μ…˜ (λŒ€μ²΄λ¬Έμžμ—΄: %)
find . -name "file*" -print0 | xargs -t -I % sh -c '{ ls -l %; }'
> -rwxrwxrwx 1 nueees nueees 0 Jul 17 13:49 file1
> -rwxrwxrwx 1 nueees nueees 0 Jul 17 13:49 file2
> -rwxrwxrwx 1 nueees nueees 0 Jul 17 13:49 file3

03_08 - Understand user roles and sudo

ls /root # λΉ„λ²ˆμ³μ•Ό λ‚˜μ˜΄

sudo -k # invalidate the user's cached credentials λΉ„λ²ˆ λ‹€μ‹œ 쳐야함

sudo -s # runs a shell with root privileges

sudo -i # runs a shell with user privileges in root environment

exit

03_10 - Modify file permissions

image

stat test.sh
> File: test.sh
  Size: 66              Blocks: 0          IO Block: 4096   regular file
Device: 45h/69d Inode: 4785074604160518  Links: 1
Access: (0777/-rwxrwxrwx)  Uid: ( 1000/  nueees)   Gid: ( 1000/  nueees)
Access: 2022-07-17 13:24:31.892137700 +0200
Modify: 2022-07-17 13:24:31.892137700 +0200
Change: 2022-07-17 15:17:56.347758200 +0200
 Birth: -

chmod 754 test.sh  # owner rwx, group r-x, others r--

# make symbolic links instead of hard links -s μ˜΅μ…˜
ln -s poems.txt writing.txt 
ls -l
> -rwxrwxrwx 2 nueees nueees   1529 Jul 16 15:59 poems.txt
  lrwxrwxrwx 1 nueees nueees      9 Jul 17 15:22 writing.txt -> poems.txt


# hard links
ln poems.txt words.txt
ls -l
> -rwxrwxrwx 2 nueees nueees   1529 Jul 16 15:59 poems.txt
  -rwxrwxrwx 2 nueees nueees   1529 Jul 16 15:59 words.txt

04_02 - Use pipes to connect commands together


# wordcount
echo "Hello" | wc  # 1 line, 1 word, 6 characters
>       1       1       6


echo "Hello world from the command line" | wc # 1 line, 6 word, 36 characters
>       1       6      34

04_03 - View text files with cat, head, tail, and less


# poems.txtμ—μ„œ, -n 라인 μ˜΅μ…˜μ€€ 결과둜, λ§ˆμ§€λ§‰ 5μ€„λ§Œ
cat poems.txt | cat -n | tail -n5 
>   51
    52  Tyger Tyger burning bright,
    53  In the forests of the night:
    54  What immortal hand or eye,
    55  Dare frame thy fearful symmetry?

# poems.txtμ—μ„œ, λ§ˆμ§€λ§‰ 5쀄 κ°€μ Έμ™€μ„œ, -n 라인 ν‘œμ‹œ
cat poems.txt | tail -n5 | cat -n
>    1
     2  Tyger Tyger burning bright,
     3  In the forests of the night:
     4  What immortal hand or eye,
     5  Dare frame thy fearful symmetry?

# 화면에 λ§žμΆ°μ„œ navigating
less poems.txt

04_04 - Search for text in files and streams with grep

grep "the" poems.txt
> Stand in the desert. Near them on the sand,

# 라인 좜λ ₯ μ˜΅μ…˜ -n
grep -n "the" poems.txt 
> 7:Stand in the desert. Near them on the sand,

# case insensitive μ˜΅μ…˜ -i
grep -in "The" poems.txt
> The hand that mock'd them and the heart that fed.

# ignore μ˜΅μ…˜ "the"λΉΌκ³  좜λ ₯
grep -vi "the" poems.txt


# regx둜 ν•„ν„°ν•  λ•Œ
grep -E "[hijk]" poems.txt
> Percy Shelley
  Ozymandias
  I met a traveller from an antique land
  Who said: Two vast and trunkless legs of stone
  Stand in the desert. Near them on the sand,

# word 6 이상인거
grep -E "\w{6,}" poems.txt
> Percy Shelley
  Ozymandias
  I met a traveller from an antique land
  Who said: Two vast and trunkless legs of stone
  Stand in the desert. Near them on the sand,

04_05 - Manipulate text with awk, sed, and sort

awk: 쑰건 ν•„ν„°ν•΄μ„œ 좜λ ₯

cat simple_data.txt
> Name    ID      Team
  Scott   314     Purple
  Ananti  991     Orange
  Jian    3127    Purple
  Miguel  671     Green
  Wes     1337    Orange
  Anne    556     Green

# λ‘λ²ˆμ§Έ 컬럼만 좜λ ₯
awk '{print $2}' simple_data.txt
> ID
  314
  991
  3127
  671
  1337
  556


# λ‘λ²ˆμ§Έ 컬럼과 첫번재 컬럼 좜λ ₯ μ‹œ κ°€μš΄λ° νƒ­μœΌλ‘œ ꡬ뢄
awk '{print $2 "\t" $1}' simple_data.txt
> ID      Name
  314     Scott
  991     Ananti
  3127    Jian
  671     Miguel
  1337    Wes
  556     Anne

# numeric-sort μ˜΅μ…˜ -n으둜 string numeric μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 좜λ ₯
awk '{print $2 "\t" $1}' simple_data.txt | sort -n
> ID      Name
  314     Scott
  556     Anne
  671     Miguel
  991     Ananti
  1337    Wes
  3127    Jian

sed: λ°”κΎΈκΈ°

cat simple_data.txt
> Name    ID      Team
  Scott   314     Purple
  Ananti  991     Orange
  Jian    3127    Purple
  Miguel  671     Green
  Wes     1337    Orange
  Anne    556     Green

# replace string, searchμ•½μž s둜 μ°Ύμ•„μ„œ λ°”κΏˆ
sed s/Orange/Red/ simple_data.txt
> Name    ID      Team
  Scott   314     Purple
  Ananti  991     Red
  Jian    3127    Purple
  Miguel  671     Green
  Wes     1337    Red
  Anne    556     Green

sed '3 s/Orange/Red/' simple_data.txt
> Name    ID      Team
  Scott   314     Purple
  Ananti  991     Red
  Jian    3127    Purple
  Miguel  671     Green
  Wes     1337    Orange
  Anne    556     Green
  

sort: μˆœμ„œμ§€μ •

# κΈ°λ³Έ string μ˜€λ¦„μ°¨μˆœ, header μ•ˆλ³΄μž„
sort simple_data.txt
> Ananti  991     Orange
  Anne    556     Green
  Jian    3127    Purple
  Miguel  671     Green
  Name    ID      Team
  Scott   314     Purple
  Wes     1337    Orange

# ν‚€ μ§€μ •ν•΄μ„œ sorting 
sort -k2 simple_data.txt
> Wes     1337    Orange
  Scott   314     Purple
  Anne    556     Green
  Name    ID      Team
  Jian    3127    Purple
  Miguel  671     Green
  Ananti  991     Orange

# ν‚€ μ§€μ •ν•΄μ„œ sorting ν• λ•Œ 숫자면 μ˜΅μ…˜ -n으둜 numeric ν‘œμ‹œν•΄μ•Ό μœ„ μ˜ˆμ‹œ 처럼 character κΈ°μ€€μœΌλ‘œ μ•ˆλ¨
sort -k2 -n simple_data.txt
> Name    ID      Team
  Scott   314     Purple
  Anne    556     Green
  Miguel  671     Green
  Ananti  991     Orange
  Wes     1337    Orange
  Jian    3127    Purple


# uniqueν•œ value만 λ³Ό λ•Œ
cat dupes.txt
> a
  a
  a
sort -u dupes.txt
> a

grep tcp /etc/services | awk '{print $1}' | sort | less
> acr-nema
  afbackup
  afmbackup
  afpovertcp
  afs3-bos
  afs3-callback
  afs3-errors
  afs3-fileserver
  afs3-kaserver
  afs3-prserver
  afs3-rmtsys
  afs3-update
  afs3-vlserver
  afs3-volser
  ...

04_08 - Working with tar and zip archives

# μ˜΅μ…˜ -create: create a new archive
# μ˜΅μ…˜ -verbose: verbosely list files processed
# μ˜΅μ…˜ -file=ARCHIVE: use archive file or device ARCHIVE
tar -cvf myfiles.tar Exercise\ Files/
> -rwxrwxrwx 1 nueees nueees 160K Jul 17 16:19  myfiles.tar

# μ˜΅μ…˜ -auto-compress: use archive suffix to determine the compression
tar -caf myfiles.tar.gz Exercise\ Files/
> -rwxrwxrwx 1 nueees nueees 107K Jul 17 16:23  myfiles.tar.gz
tar -caf myfiles.tar.bz2 Exercise\ Files/
> -rwxrwxrwx 1 nueees nueees 111K Jul 17 16:24  myfiles.tar.bz2

# μ˜΅μ…˜ -x, -extract, -get: extract files from an archive (<-> -c)
# μ˜΅μ…˜ -file=ARCHIVE: use archive file or device ARCHIVE
tar -xf myfiles.tar.bz2
> drwxrwxrwx 1 nueees nueees   4096 Jul 17 15:23 Exercise Files

04_11 - Output redirection

# export by stdout

ls 1> filelist.txt
> ν•΄λ‹Ή μ»€λ§¨λ“œ μ €μž₯됨 overwrite
ls 1>> filelist.txt
> ν•΄λ‹Ή μ»€λ§¨λ“œ append μ €μž₯됨

ls notreal 1> filelist2.txt
> μ—λŸ¬ λ°œμƒ

ls notreal 2> filelist2.txt
> μ—λŸ¬ 없이 μ—λŸ¬λ‚œ κ±° μ €μž₯됨

find &> alloutput.txt
> stdout + stderr λ‘˜λ‹€ μ €μž₯됨

use case

ls notreal 1> filelist3.txt 2> filelist4.txt
> μ—λŸ¬ μ•ˆλ‚œκ±΄ filelist3.txt에 μ €μž₯ν•˜κ³  filelist4.txt μ—λŸ¬ λ‚œκ²ƒλ§Œ μ €μž₯됨
cat filelist4.txt
> ls: cannot access 'notreal': No such file or directory

04_12 - Input redirection

# import by stdin
mysql -u user1 p db_name < db.sql

use case

mysql -u user1 p db_name < db.sql > out.txt 2> outerr.txt

tee

tee command reads the standard input and writes it to both the standard output and one or more files.

df -h | tee disk_usage1.txt disk_usage2.txt # output to both a file and the console
> Filesystem      Size  Used Avail Use% Mounted on
  /dev/sdd        251G  2.2G  237G   1% /
ls
> disk_usage1.txt    disk_usage2.txt

04_13 - 2>\&1 meaning

ν‘œμ€€μ—λŸ¬(2)μ—μ„œ ν‘œμ€€μΆœλ ₯(1)으둜 λ¦¬λ‹€μ΄λ ‰μ…˜ ν•˜κ² λ‹€λŠ” 뜻

File Descriptor

0 stdin ν‘œμ€€μž…λ ₯(keyboard)
1 stdout ν‘œμ€€μΆœλ ₯(screen)
2 stderr μ—λŸ¬μΆœλ ₯(screen)

\/dev\/null

echo $SHELL > /dev/null
> 좜λ ₯μ•ˆν•˜κ³  κ²°κ³Ό 버림

2>\&1

/tmp/ErrorMsg.sh > /dev/null 2>&1

ampersand(&)λŠ” λ’€μ˜ 숫자 1이 File DescriptorλΌλŠ” κ±Έ ν‘œν˜„ν•œ symbol, 이 symbol이 μ—†μœΌλ©΄ 1을 νŒŒμΌμ΄λ¦„μœΌλ‘œ 인식.
/tmp/ErrorMsg.sh 의 μ‹€ν–‰κ²°κ³Όλ₯Ό /dev/null/둜 λ¦¬λ‹€μ΄λ ‰μ…˜ν•΄ 화면에 좜λ ₯ν•˜μ§€ μ•Šμ„ 것이며, ν‘œμ€€μ—λŸ¬ μ—­μ‹œ ν‘œμ€€μΆœλ ₯κ³Ό 같은 κ³³(/dev/null)으둜 보내 화면에 좜λ ₯ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 뜻

/tmp/ErrorMsg.sh > /dev/null # stderr 좜λ ₯, μ‹€ν–‰ 쀑 μ—λŸ¬κ°€ λ°œμƒν•˜λ©΄ 좜λ ₯
> -bash: /tmp/ErrorMsg.sh: No such file or directory

/tmp/ErrorMsg.sh 2> /dev/null # stderrλ₯Ό /dev/null둜 보내 화면에 좜λ ₯λ˜μ§€ μ•ŠμŒ
> 
/tmp/ErrorMsg.sh > /dev/null 2>&1 # stdoutκ³Ό stderr λ‘˜λ‹€ 좜λ ₯λ˜μ§€ μ•ŠμŒ
> 
echo $SHELL 2> /dev/null # stdout 좜λ ₯
> /bin/bash

05_02 - Find system hardware and disk information

free -h
> total        used        free      shared  buff/cache   available
  Mem:          7.6Gi       692Mi       5.9Gi       1.0Mi       1.1Gi       6.7Gi
  Swap:         2.0Gi          0B       2.0Gi

cat /proc/cpuinfo
> processor       : 0
  vendor_id       : GenuineIntel
  cpu family      : 6
  model           : 140
  model name      : 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  stepping        : 1
  microcode       : 0xffffffff
  cpu MHz         : 2803.203
  cache size      : 12288 KB
  physical id     : 0
  
lscpu # cpuinfo μžμ„Έν•˜κ²Œ λ³΄μ—¬μ€Œ
> Architecture:                    x86_64
  CPU op-mode(s):                  32-bit, 64-bit
  Byte Order:                      Little Endian
  Address sizes:                   39 bits physical, 48 bits virtual
  CPU(s):                          8
  On-line CPU(s) list:             0-7
  Thread(s) per core:              2
  Core(s) per socket:              4
  Socket(s):                       1
  Vendor ID:                       GenuineIntel
  
df -h

sudo du -hd1 /

sudo lshw | less # λΆ™μ–΄μžˆλŠ”κ±° 확인

ip a # ip 정보

05_03 - Install and update software with a package manager

apt search tree

apt show tree

tree

sudo apt update

sudo apt install tree

tree

man tree

sudo apt update

sudo apt upgrade