- 1. Linux Command Line
- 01_01 - File system structure
- 02_04 - Finding help for commands
- 03_01 - The Linux file system
- 03_02 - The glob command
- 03_03 - Escape characters and quotes
- 03_04 - Brace expansion
- 03_05 - Command and variable substitution
- 03_07 - Find files and Xargs
- 03_08 - Understand user roles and sudo
- 03_10 - Modify file permissions
- 03_11 - Create hard and symbolic links
- 04_02 - Use pipes to connect commands together
- 04_03 - View text files with cat, head, tail, and less
- 04_04 - Search for text in files and streams with grep
- 04_05 - Manipulate text with awk, sed, and sort
- 04_08 - Working with tar and zip archives
- 04_11 - Output redirection
- 04_12 - Input redirection
- 04_13 - 2>\&1 meaning
- 05_02 - Find system hardware and disk information
- 05_03 - Install and update software with a package manager
π linkedin learning-linux-command-line
1. Linux Command Line
File system structure
01_01 -/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
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--
03_11 - Create hard and symbolic links
# 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
2>\&1 meaning
04_13 -νμ€μλ¬(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