Linux - File System

  • Linux File System is like a single inverted tree of directories. Parent or main directory or top of hierarchy of all is called as root and can be accessed by /

  • Files can be of below 4 types:

image.png

  1. Static: File contents which remains the same until edited or reconfigured
  2. Dynamic: Content keeps changing based on outcome of current run processes
  3. Persistent: Value of files remains the same even after restart
  4. Runtime: Values can be used currently but would be cleared after restart

Some important directories:

  • /usr : All the installed software, shared libraries etc are saved here

  • /etc : Configuration files related to system

  • /var : Variable files or content which should be persisted between reboots

  • /home : Where various users sub directories are created and can be used to store personal data and config files related to the specific user

  • /tmp : Temporary directory to save data for time being which would be deleted automatically if file is not accessed or modified in last 10 days. /var/tmp is for same purpose and time period is for 30 days

  • /boot : System files which are needed to start the boot process

  • /dev : Contains files like drivers which are needed to connect to hardware

File Management

Basic operations around files are creating directories, files, copying, moving and deleting/removing them.

  • Create Directories :
mkdir #Command use to create or make directories
mkdir testDirectory #This command will create new testDirectory in the current location
mkdir /usr/deepak/sampleDirectory #This command will create new directory 'sampleDirectory' under /usr/deepak parent directory
mkdir -p /usr/deepak/newDirectory1/newDirectory2 #Use -p to make sure parent directories are created too if not present
mkdir dir1 dir2 #Create or make multiple directories dir1 and dir2 in current location
  • Copy :
cp #command used to copy
cp file1 file2 #copies file1 into file2, if file2 is not present it creates newly and if file2 exists it is overwritten with contents from file1
cp -r dir1 dir2 #Copies all content from dir1 to dir2, -r is used to recursively copy the file
  • Move or Rename :
mv #command used to move or rename files/directories
mv file1 file2 #if to rename file1 as file2
mv /usr/file1 /tmp/ #Moves file1 from usr directory to tmp directory
  • Remove :
rmdir #to remove or delete non-empty directories
rm file1 #to remove file1
rm -r /usr/text #removes all the files in /usr/text directory, -r is used to indicate recursive
rm -ri /usr/text #removes all the files in /usr/text directory, -i is used to prompt for user confirm before removing the file
rm -rf /usr/text #removes all the files in /usr/text directory, -f is used to force delete all the files
  • Hard link is a directory entry which references a file. This helps in saving space. If original file is deleted, content of file is available as long as one hard link exists
ln (original file path) (new file path) 
ln /usr/file1.txt /tmp/hard-link-file1.txt #To create hardlink hard-link-file1.txt for file1.txt
  • Soft links or Symbolic links are special file which links to file or directory. Soft link to a missing file is called Dangling soft link
ln -s (file path you want to point to) (new file path)
ln -s /usr/test /tmp/test
  • A hard link always points a filename to data on a storage device. A soft link always points a filename to another filename, which then points to information on a storage device.

Additional Reading or References: Red Hat Article