Linux filesystem and core commands — Bird's Linux Private Kitchen notes

Contents

  1. 1. Linux
    1. 1.1. Free software
    2. 1.2. Concepts
      1. 1.2.1. Disks
      2. 1.2.2. inode and block
      3. 1.2.3. Supported filesystem types
  2. 2. cmd
    1. 2.1. Basics
      1. 2.1.1. man
      2. 2.1.2. info
      3. 2.1.3. chown
      4. 2.1.4. chmod
      5. 2.1.5. pwd
      6. 2.1.6. mkdir
      7. 2.1.7. cp
      8. 2.1.8. mv
      9. 2.1.9. od
      10. 2.1.10. chattr
      11. 2.1.11. find
      12. 2.1.12. ln
    2. 2.2. Disks
      1. 2.2.1. dd
      2. 2.2.2. cpio
      3. 2.2.3. mount
      4. 2.2.4. df+du
      5. 2.2.5. dumpe2fs
      6. 2.2.6. e2label
      7. 2.2.7. tune2fs
    3. 2.3. Compression
      1. 2.3.1. gzip
      2. 2.3.2. bzip2
      3. 2.3.3. tar
    4. 2.4. vim
      1. 2.4.1. vim-cmd
      2. 2.4.2. unix2dos
      3. 2.4.3. dos2unix
      4. 2.4.4. iconv
      5. 2.4.5. tr

Notes from Bird’s Linux Private Kitchen (3rd edition), kept for quick review.

Linux

Free software

Stallman:
“Free software” is a matter of liberty, not price. To understand the concept, you should think of “free speech”, not “free beer”. “Free software” refers to the users’ freedom to run, copy, distribute, study, change, and improve the software

Concepts

Linus Torvalds: Linux

Kernel releases split into stable even minor versions (e.g. 2.6.x) for production and home use, and development odd minors (e.g. 2.5.x) when you need bleeding-edge features.

Disks

IDE: /dev/hd[a-d][1-63]
SATA, SCSI, USB, flash: /dev/sd[a-p][1-15]

sector → cylinder → filesystem

First sector (512 bytes): MBR (446) + partition table (64)

Four primary partitions by default; extended partitions hold logical ones.

inode and block

inode (128 bytes) stores metadata and block pointers for a file; one pointer is 4 bytes.
Max file size with 1K blocks: 12 + 256 + 256256 + 256256*256 (KB) = 16GB

superblock holds overall filesystem info, including inode/block usage.

Supported filesystem types

  • ls -l /lib/modules/$(uname -r)/kernel/fs

cmd

Basics

man

  • h help
  • /string search down (n); ?string search up (N)
  • space next page; u previous page
  • man -k (apropos) keyword search; man -f (whatis) short description

info

  • tab move between nodes

chown

  • change owner and group: chown user:group fileName
  • group only: chown .group fileName (same as chgrp)

chmod

  • u g o a [+(add) -(remove) =(set)] r w x # a = all
  • w matters: lets a user delete, update, or create files in a directory even when they do not own every file

pwd

  • pwd -P print physical path (resolve symlinks)

mkdir

  • -m set mode: mkdir -m 711 test2
  • -p create parent directories

cp

  • -a preserve attributes
  • -u copy only when source is newer
  • -d copy symlink, not target
  • -s create symlink to source

mv

  • -b backup before overwrite; backup name gets a ~ suffix

    -b takes no argument; mv reads VERSION_CONTROL for backup policy.
    –backup chooses what to do when the target exists:

    1. CONTROL=none or off: no backup.
    2. CONTROL=numbered or t: numbered backup
    3. CONTROL=existing or nil: if numbered backups exist, continue numbering m+1…n:
      If log2.txt.1 already exists, the next run creates log2.txt2, and so on. With no numbered backup yet, use simple backup below.
    4. CONTROL=simple or never: one simple backup; a later overwrite replaces that backup too.
  • -f force; overwrite without asking
  • -i prompt before overwrite
  • -u update only when source is newer

od

  • od -t oCc /etc/issue octal and ASCII side by side

chattr

  • i cannot delete, modify, or link
  • a append only
  • special bits
    • 4-SUID on binaries: run with owner’s privileges
    • 2-SGID on files or dirs: run with group of the file (handy for shared directories)
    • 1-SBIT on directories: users with w may only delete their own files (/tmp: drwxrwxrwt)
    • file shows details

find

find /path -mtime 0 # modified within the last 24 hours

  • n means exactly n days ago
  • -n within the last n days
  • +n n or more days ago (>= n)

find /bin /sbin -perm +6000 -exec ls -l {} ; # files with SUID or SGID set

  • -perm -mode # all bits in mode must be set

find /etc -size +50k -a -size -60k -exec ls -l {} ; # files between 50K and 60K under /etc, list with ls -l

  • -a # and
  • ! # negate the next test
  • -o # or

ln

  • ln # hard link to the same inode
  • ln -s # symbolic link

Disks

dd

  • dd if=/dev/zero of=/boot/test.img bs=1M count=10 # 10M test file
  • dd if=/etc/passwd of=/tmp/passwd.back # backup
  • default record size 512 bytes; can copy whole partitions/disks at sector level

cpio

  • cpio -ovcB > [file|device] # backup

  • cpio -ivcdu < [file|device] # restore

  • cpio -ivct < [file|device] # list contents

mount

  • mount –bind # bind-mount a directory onto another path
  • typical workflow: partition (fdisk), format (mkfs), mount (mount)

df+du

  • du -sh ./* # totals per entry; subdirectory sizes roll up into parents
  • du -Sh ./* # do not add child sizes into parent subtotal

dumpe2fs

  • dumpe2fs /dev/sda1 -h # major filesystem info

e2label

  • e2label device new_label

tune2fs

  • l : like dumpe2fs -h — read superblock summary
  • j : ext2 → ext3
  • L : change filesystem label (like e2label)

Compression

gzip

  • gzip -9 -c man.config > man.config.gz # keep original
  • gzip -v man.config # compress with ratio
  • gzip -d man.config.gz # decompress
  • zcat man.config.gz # read compressed file

bzip2

  • usually better ratio than gzip; same flags
  • bzcat man.config.bz2

tar

  • j: bzip2; z: gzip
  • compress: tar -jcv -f filename.tar.bz2 paths…
  • list: tar -jtv -f filename.tar.bz2
  • extract: tar -jxv -f filename.tar.bz2 [member] -C target_dir
  • -p preserve permissions
  • –exclude=file # skip paths
  • tar -jcv -f /root/etc.newer.tar.bz2 –newer-mtime=”2008/09/29” /etc/* # incremental backup
  • tar -cvf - /etc | tar -xvf - # stream pack and unpack to current directory

vim

vim-cmd

Vim keyboard shortcut reference
Vim command-mode cheat sheet

unix2dos

  • k : keep original mtime (updates ctime)
  • n : write to a new file: unix2dos -n old new

dos2unix

  • same flags, opposite conversion

iconv

  • iconv -f from_encoding -t to_encoding filename [-o newfile]

tr

  • echo “A”|tr “A-Z” “a-z” # to lowercase
  • tr -s ‘\n’ # squeeze repeated characters
  • tr -cd [:digit:] # delete non-digits
Questions? Leave a comment below or email: newbvirgil@gmail.com