Chapter 6 Linux File System 1
File System File System management how to store informations on storage devices The Hierarchical Structure Types of file Common File system Tasks 2
The Hierarchical Structure of the Linux File System / home etc usr bin boot dev lib root local bin 3
/ The Hierarchical Structure of the /bin /etc /boot /dev /usr /home Linux File System - first of mount point in linux - contains shells (bash) and file system management utilities - keep linux text-based configuration files - keep important linux booting files - keep all device files - keep all user binary and X library - keep user home directory /proc - is pseudo file system for tracking running process and state of linux system 4
The Hierarchical Structure of the Linux File System /var - keeping variable data, log file and printer spooling /lib - contain shared library that is required by system program /tmp - contain system temporary file /root - the root user s home directory /sbin - contains important system management and admin files (fdisk, fsck, ifconfig, init, mkfs, shutdown, halt) /media - system use to mount externel devices (CD/DVD, floppy drives) (or /mnt) 5
The Hierarchical Structure of the Linux File System Files in /etc directory 6
The Hierarchical Structure of the Linux File System FHS (Filesystem Hierarchy Standard) provide a standard directory structure for the file system, ensuring consistency between systems and distributions. http://www.pathname.com/fhs 7
Types of Files Regular files Links FIFOs Sockets 8
Navigating the File System Path absolute path Current directory & relative path Home directory (~) pwd, cd, ls pwd (Present Working Directory) Display the current directory $ pwd cd Change the current directory $ cd /var/log $ cd.. $ cd ~ (or press cd ) 9
Navigating the File System pwd, cd, ls ls List the files and subdirectories that exist within a directory $ ls / $ ls /bin $ ls l / $ ls a /var/run $ ls R /var/run $ ls 10
Navigating the File System $ ls l ~ 1 file type 2 Owner s /Group/Other Permissions 3 Number of link 4 Owner s name 5 Group s name 6 Size 7 Last Update datetime 11 8 File/Dir name
Navigating the File System File type normal file - Block device file b Character device file c Directory d Link file l Permissions on file /directory Read r Write w Execute x 12
Creating Files and Directories Creating a new file : touch or vi touch command touch myfile.txt // size = 0 Using a Text editor vi newfile.txt vi /home/user1/file.txt Creating a directory : mkdir mkdir mydir mkdir /home/ti/mydir mkdir -p /home/ti/aa/bb 13
Viewing text file content cat, more, less, head, tail cat filename more more filename less filename Auto stop when full screen. Using spacebar, page up, page down, arrow keys when viewing head filename Display the first couple of lines of a text file tail filename Display the last couple of lines of a text file tail f filename : display new content to be added to the end of the file 14
Copying, moving, deleting cp, mv, rm, rmdir cp /tmp/schedule.txt ~ //copy file cp R ~/mydir ~/backup //copy dir. mv /tmp/mylog.txt /var/log //move file mv myfile.txt mynewfile.txt //rename rm filename //delete file rm -r dirname //delete directory rm -i filename //prompt rmdir dirname //delete empty directory 15
Using wildcard charaters *,?, [ab], [a-z] Example : there are these files in current directory vd.txt, vda.txt, vdb.txt, vdabc.txt, vd1.txt, vd22.txt, vd3.txt $ cp *.txt /home/an //copy 7 files $ cp vd[1-5].txt /home/an // copy 2 file $ cp vd[ab].txt /home/an // copy 2 file 16
Link files Hard link and Symbolic link (soft link) Pointers a different file/directory in the file system Symbolic link : similar shortcut in Windows OS ln pointee_file pointer_file ln -s pointee_file pointer_file (pointee_file = source file/dir pointer_file = hard link/ symbolic link file) $ ln -s /usr/share/doc/ ~/docs 17
Running executable file Linux OS searches through the directories listed in your PATH environment variable for the executable file you specified. Adding a new path to the $PATH PATH=$PATH:newpath Determine resident of exec file which $ which vi /usr/bin/vi $ which ls /bin/ls 18
Searching the File System find, locate, grep Find utility searchs for files of a specified name, or owner, or size Using *,? when specified name find path -name filename find path -user username find path -size size find path -size +size find path -size -size find / -name *.log find / -user root find / -size +100 19
Searching the File System Locate utility builds an index of the files. When searching files, locate runs a query of the index, doesn t search the file system directly. Locate runs much faster than find To use locate, must first install the findutilslocate package 20
Searching the File System Using grep to search for content within a file grep search_text file grep r help /*.txt grep help *.txt 21
Manage Disk Partitions Using fdisk to create disk partitions Building a file system with mkfs Mounting a partition with mount Checking the file system with fsck 22
Storage Devices In Linux system, storage devices is referenced as device files that stored in /dev directory. SCSI Hard disks, USB disks sda sdb sdc /dev/sda /dev/sdb /dev/sdc IDE Hard disks hda hdb hdc /dev/hda /dev/hdb /dev/hdc Floppy disks fd0 fd1 /dev/fd0 /dev/fd1 CDRom SCSI sr0 sr1 /dev/sr0 /dev/sr1 23
Partitions Primary and Extended partitions On the IDE hard disk, up to four primary partitions One of the primary partitions can be extended partition Include many logical partitions M B R 1 2 3 4 primary M B R 1 2 5 6 3 7 4 8 extended logical 24
Partitions Each Partition is referenced as one device, and have a device file in /dev Partitions as sda1, sda2, sda3, have corresponding device driver files as /dev/sda1, /dev/sda2, /dev/sda3, Ex. fdisk /dev/sda mount /dev/sda2 25
File system A file system style is the way that data is organized to store in the storage device Linux supports many of fs styles ext2, ext3, ext4, reiser, // Linux vfat, fat32, ntfs // Windows iso9660, //cdrom File system term also refer to a partition has been formatted with a file system 26
File system Linux uses a Virtual File System (VFS) that creates a single hierarchy that encompasses all partitions on all storage devices in the system Hình: Hệ thống tập tin ảo 27
Mount Mount the partition into an existing directory in your file system, before can use it (read/write data). sda sda1 / sda2 home etc usr bin sda3 sda4 local bin Storage device Logic file system 28
Unmount Unmount the partition into a directory sda sda1 / sda2 home etc usr bin sda3 sda4 local bin hda1 Storage device logic file system Storage device 29
Unmount T1 : / ñược mount với sda1 $ ls /usr/local vd1.txt T2: /usr ñược mount với sda2 $ vi /usr/local/vd2.txt $ ls /usr/local??? T3: umount /usr với sda2 $ ls /usr/local??? 30
Using fdisk to create disk partitions Syntax : $ fdisk /dev/sdb =>press m to open menu Choose p to view partition table Choose n to make new partition Choose d to delete a partition Choose w to write table and exit, or q to exit without saving 31
Using fdisk to create disk partitions To make a new partition Choose making a primary partition or extended partition? Size of partition (MB or start and end cylinder) Have to create at least two partitions on a primary hard disk : swap partition and root partition a swap partition OS uses it as virtual memory it s size is at least twice as large as your installed RAM 32
Using fdisk to create disk partitions 33
Using fdisk to create disk partitions To view the partition table $ fdisk -l 34
Building a file system with mkfs mkfs Used to make an ext2 or ext3 (or FAT) file system on a partiton mkfs -t ext3 /dev/sda2 mkreiserfs Used to make a Reiser file system on a partition mkreiserfs /dev/sdc3 Other utilities : mkswap, mk2fs, mkfs.msdos, mkfs.vfat 35
Mounting a partition with mount mount command syntax: mount t type device target-dir type : ext2, ext3, reiserfs, device : /dev/hda1, /dev/hdb1, target-dir : /, /var, /home, /mnt/cdrom, 36
Mounting a partition with mount Using mount command / hda3, ext2 hda1,ext2 /var /home hda4,ext2 $ mount t ext2 /dev/hda3 / $ mount t ext2 /dev/hda4 /home $ mount t ext2 /dev/hda1 /var 37
Mounting a partition with mount After mounting the partition, you can use the mount command with no options to view all mounted file systems. Or using df utility $ mount $ df 38
Mounting a partition with mount Automatically mount when the system boot or reboot The /etc/fstab file contains a list of file systems that the operating system mounts whenever it boots. Each line specifies a separate file system to be mounted on boot partition mount_point fs_type options dump check 39
Mounting a partition with mount You can add a new mount partition entry in /etc/fstab that Linux will automatically mount at boot. - Using vi editor to open /etc/fstab, then add the new line 40
unmount Using umount command: two ways umount dir_name umount device_name - Have to unmount before unplug removable devices - Unmount to free the mount point - Automatically unmount when the OS shutdowns (halt) 41
ðĩa USB : /dev/sdb Có 2 partittion /dev/sdb1 /dev/sdb2 /mnt/usb1 /mnt/usb2 Cần rút ñĩa USB => thực hiện unmount umount /dev/sdb Hoặc: umount /mnt/usb1 umount /mnt/usb2 umount /dev/sdb1 umount /dev/sdb2 42
Checking the file system with fsck Using fsck to check your file systems for errors and make repairs fsck device $ fsck /dev/sdc1 43
Use Removable Media Floppies, optical devices, USB, FireWire devices Need to mount with a directory Need to unmount before remove 44
Working with floppy $ mount t vfat /dev/fd0 /media/floppy $ mount $ vi /etc/fstab /dev/fd0 /media/floppy auto noauto,user,sync 0 0 $ mount /dev/fd0 $ umount /dev/fd0 45
Working with optical devices (1) mount t iso9660 /dev/hdc /media/cdrom (2) Adding a line in /etc/fstab file : /dev/hdc /media/cdrom iso9660 noauto,user,sync 0 0 so, mount a cdrom by entering mount /dev/hdc (3) umount /dev/hdc 46
USB, FireWire devices Automatically detect a new device when connect an external storage device SCSI device Check /var/log/messages file after connecting the device Create on the device A partition A file system Mount device mount t auto /dev/sdd /media/thumbdrive umount the device before remove it 47
Using YaST YaST utility Install software, manage hardware settings, partition hard drives, configure bootloaders, configure network board, configure users and groups, settings for the services running on system Ex. : Using YaST to partition on usb disk 48
Back up data Selecting a Backup Medium Floppies, tape drive CDs, DVDs USB External hard drives 49
Back up data Selecting a Backup Strategy Selecting Backup type : Full Incremental Differential Selecting a backup schedule Determining what to back up 50
Back up data Linux Backup Utilites tar, cpio 51