• understanding the filesystem is important regardless of whether one is
    • developing web services and deploying them out of a linux container or
    • doing systems work and troubleshooting servers

filesystem tree

  • two salient kinds of objects in the linux filesystem are
    • files
    • directories
  • files are dedicated to text, images, programs, HTML, zip etc
  • directory is a container for other files, directories and other object
filenames
  • files and dirs have names called filenames
  • filenames can contain any character except the slash “/

  • special chars set: “!$#()[]%&:”
  • if filename has special character(s), use quotes around the filename in shell
    • else precede the special char with a backslash “\”
  • actual filename:
    Great Filename!
    
  • using filename in shell:
    'Great Filename!' # quoting
    Great\ Filename\! # escaping
    
directory
  • dirs are nested inside each other
  • root folder is the top most dir
  • different physical drives don’t get a separate root folder
    • one root folder per linux installation
  • each file has an unambiguous path in the OS
  • forward-slash: ‘/’ represent nesting of a dir in a path
    • eg: (root)/var/log/syslog
    • same as url
    • same as fractions
  • windows uses backslash ‘\’ in path for dir nesting

working dir

  • working dir: default dir in the file system that the program looks at
  • shell and every other program has a working directory
  • pwd: to get the current working dir
    • pwd: print working dir
  • cd: changes the working dir
      cd / # changes to root 
      cd /var/log # change dir by full-path
      cd three # change into dir in current working dir
      cd ../ocean # go up one level, go to adjacent dir
      cd ../../usr # go up two levels, then into a dir there
      cd # goes to user home (same as cd ~)
    
  • cd is a useful tool for navigating in the filesystem in the shell
  • cd only works to navigate into dirs
    • cd on non-dir files will throw an error

absolute and relative path

absolute path
/ # root dir (top most dir)
  • a path that begins with slash is the full-path
  • any file in the OS filesystem may directly and unambiguously be pointed to with the full path
    • so, full path is also called absolute path
    • specifies every step from the root dir
  • they get inconvenient to work with very quickly
    • especially with deeply nested dirs
relative paths
  • so relative paths can also be used instead
  • relative paths are always with respect to the current working directory
  • using relative paths can help avoid a lot of typing in the shell
    .. # points to parent dir 
    . # points dir to itself
    ~ # user home dir
    
  • tab completion may be used to complete dir names
    • if conflicting names, stops and lists options

moving and copying files

  • mv is used for moving and renaming files
    mv source destination # moves file at source path to destination path
    mv item1 item2 ... dir # moves a bunch of files to dir
    mv old-name new-name # rename file from old-name to new-name
    mv beach.jpg Photos # moves file 'beach.jpg' into 'Photos' dir
    
  • cp syntax is the same as the mv
  • man mv and man cv describe all flags and argument possible for each
short name commands
  • more efficient
    • used for most commonly used commands
  • linux has been around for a long time
  • at one point in the past, when linux was being developed
    • command name lengths made a difference in how fast the computer processed the command

making new dirs

mkdir: makes new dirs

  • this can have either a relative or an absolute path
    mkdir notes # makes a dir in current dir
    mkdir /tmp/cache # makes nested dir per argument path 
    

removing dirs

  • rmdir: deletes dirs
  • also can have either a relative or an absolute path
  • rm works only deletes files
    • rm does not delete dirs
  • rm -r junk
    • deletes all the contents in the dir junk

globbing

  • used to match filenames
    • similar to reg-exp, but for linux filenames
  • special chars can be used to ‘select’ filenames that match a pattern
    • file operations can be applied to only this selection
  • man glob: read manual for glob command

  • glob chars

      * # matches any string of chars
      ls *html
      ls app*
      ls *s
      ls *pp*
    
      {} # matches alternatives listed within
      ls app.{css,html} 
    
      ? # one char variable 
      bea?.png # matches both 'bean.png' and 'bear.png', not 'beer.png' or 'bees.png'
      be??.png # matches both 'bean.png', 'bear.png', 'beer.png' and 'bees.png'
    
      [] # matches any one of the chars within
      be[aeiou]r.png # matches 'bear.png', 'beer.png'
    
  • filenames in linux are case-sensitive
    • consequently, applies to globbing
      ls *JPG # not the same as 
      ls *jpg
      ls *{jpg, JPG} # lists both
    

references