Skip to main content

0102 | Files and Filesystem

Files and Filesystem

Linux Filesystem Hierarchy Standard

  • description of the filesystem hierarchy
    • hier

System Log File

  • /var/log/syslog

Devices, Partitions, and Mounting

mount; df; du

  • device /dev/sda
    • s --> sata drive
    • a --> first drive
  • example
    • 2 storage devices: /dev/sda/ and /dev/sr0
      • sda --> 2 primary partitions --> 3 secondary partitions
      • each partition has it's own id and is mounted at a different location in the file system
  • partition mount information
    • less /etc/fstab
  • show the currently mounted devices
    • mount
  • report file system disk space usage
    • df -h
  • estimate file space usage
    • du -sh ~

Absolute and Relative Paths

  • absolute path
    • if you need a path that is valid from anywhere from the file system
    • assumes same global directory structure
    • longer than relative paths
    • can be shortened with environmental variables -- contains a base directory on which paths can be built
  • relative path
    • assumes the same local directory structure
    • shorter
    • more portable across systems -- it is easier to ensure that the local folder structure matches

Files and Directories

  • update the last modification time of a file to the current time
    • touch file.txt
  • copy files
    • cp
  • move files
    • mv
  • delete files
    • rm
  • directories
    • creating directories: mkdir
    • removing empty directories: rmdir
    • removing non-empty directories: rm -rf non-empty-dir/
    • moving directories: mv project project01

Spaces in Paths and Filenames

  • bash shell uses spaces as separators for command line arguments
  • escape space with a backslash or place the name in quotes ("x" (preferred) or 'x')

File and Path Expansion

  • using wildcards to specify multiple files at once
    • also called as globbing
  • globbing only works in a single segment
    • (/x/ -- between slashes)
  • asterisk (*) | zero or more non separator characters
    • ls file*.txt
  • question-mark wildcard (?) | one character
    • ls file?.txt
  • include files from sub directories
    • ls **/*.txt
    • if it does not work --> set the glob star option
      • shopt -s globstar
  • specify specific range | square brackets ([])
    • ls file[123].txt -- 1 or 2 or 3
    • ls file[1-3].txt -- 1 or 2 or 3
    • ls file[a-zA-Z].txt -- letters from a to z AND from A to Z

Even More Looking at Text Files

head;tail

  • specify the number of lines
    • head -n 5 wordlist.txt or head -5 wordlist.txt
    • tail -n 5 wordlist.txt or tail -5 wordlist.txt
  • follow the changes with tail
    • tail -f /var/log/auth.log
    • use sudo in an other window and look out for the changes
  • use diff for finding small differences
    • diff file1.txt file2.txt

ln

  • a link is just a reference or pointer to a file/directory to somewhere on the file system
  • points to the physical location of the file on storage
  • not possible to create a hard link to a directory -- to avoid a circular loop
  • generally phased out -- not common
  • even if the original file is moved/deleted, the link will continue to work
  • the file will only be completely removed, when there are no more hard links to the files
  • example
    # create the hard link
    ln hello.txt hello-hard-link.txt
    # remove original file
    rm hello.txt
    cat hello-hard-link.txt
    # recreate the original from the hard link
    ln hello-hard-link.txt hello.txt
  • references a file/directory on the file system -- not on the storage
  • if the resource is moved/deleted -- it won't work anymore
  • when creating soft links -- use absolute path for the target
  • it is possible to create a soft link to a directory
  • soft links can cross file systems -- referencing between different partitions
  • example
    # create a soft link
    ln -s ./hello.txt hello-soft-link.txt
    ls -l
    # moving the origin file
    mv hello.txt hello-new-name.txt
    # the soft link will become useless
    cat hello-soft-link.txt

Compressing and Archiving Files

zip;unzip;tar;gunzip

  • perform 2 different functions

    • archiving multiple files into a single one
    • compressing the files -- less disk space
  • example | zip and unzip

    zip tmp/backup-files.zip file1.txt file2.txt file3.txt
    # list the contents of the zip file without extracting anything
    unzip -l tmp/backup-files.zip
    # compress multiple dirs -- without including their files
    zip tmp/backup-dirs.zip dir1 dir2
    unzip -l tmp/backup-dirs.zip

    # include the files in the dirs
    zip -r tmp/backup-dirs.zip dir1 dir2
    unzip -l tmp/backup-dirs.zip
  • example |tar -- (tape archive -- to archive files) -- originally without compression

    # create an achive for the files and dirs
    # c:create; v:verbosity; f:file
    tar cvf backup.tar file?.txt dir? shakespeare.txt
    # verify success -- t:confirm
    tar tvf backup.tar

    # extract the contents
    tar xvf ../backup.tar
  • example | tar -- with compression

    # choose compression algorithm -- gzip; bzip2
    # compress a tar file
    gzip backup.tar
    ls -lh backup.tar.gz
    # decompress the file
    gunzip backup.tar.gz

    # compress and archive at once with tar
    # z:specify the compression algorithm -- note the .gz extension
    tar cvfz backup2.tar.gz file?.txt dir? shakespeare.txt
    ls -lh backup2.tar.gz

Searching the Filesystem

find;locate:which;whereis

  • example | find
    # search based on name
    find . -name 'file*.txt'
    # search based on name -- case insensitive
    find . -iname 'file*.txt'
  • example | locate
    • uses a database to find files
    • updated via cron job (daily)
    # no need to specify starting point
    locate file.txt
    # search with patterns
    locate file*.txt
  • display the full path of the command with the associated name | which
    • which ls
  • locate executable; source code; man pages | whereis
    • whereis ls