Top Banner
Linux Operations and Administration Chapter Three Managing Files and Directories
51
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Linux Operations and AdministrationChapter ThreeManaging Files and Directories

    Linux Operations and Administration

    Linux Operations and Administration

    ObjectivesDescribe the Linux file system and the Filesystem Hierarchy StandardNavigate the Linux directory structureManage files and directories in Linux*

    Linux Operations and Administration

    Linux Operations and Administration

    An Overview of the Linux Directory StructureFile system The way files are stored and organized to simplify access to dataLinux has only one root directoryAll files and subdirectories are placed under the root directory in a treelike structureFilesystem Hierarchy Standard (FHS)Specifies requirements and guidelines for file and directory placement in UNIX-like operating systems*

    Linux Operations and Administration

    Linux Operations and Administration

    An Overview of the Linux Directory Structure (contd.)*Figure 3-2 The Linux directory structureCengage Learning 2013

    Linux Operations and Administration

    Linux Operations and Administration

    An Overview of the Linux Directory Structure (contd.)Being able to find critical configuration files quickly cuts down on troubleshooting timeFile systems based on FHS have two distinctions: Shareable versus unshareable files Variable versus static filesShareable fileCan be stored on one machine and used by multiple users on other machinesUnshareable fileCant be accessed by multiple users*

    Linux Operations and Administration

    Linux Operations and Administration

    An Overview of the Linux Directory Structure (contd.)Static filesDont change on their ownVariable filesUsually found in the /var directoryCan change on their ownTable 3-1 Linux directories defined by the FHS*

    Linux Operations and Administration

  • *Table 3-1 Directories defined by the FHSLinux Operations and Administration

    Linux Operations and Administration

    Linux Operations and Administration

    Navigating the Linux Directory StructureMost Linux servers are installed without a graphical environmentAdministrators need to learn how to navigate without a GUITo open a terminal window:KDEClick the K menu button (the green circle) at the lower left and click TerminalGNOMEClick Computer, More Applications to open the Application Browser, and then click the GNOME Terminal icon in the System Groups category*

    Linux Operations and Administration

    Linux Operations and Administration

    Changing Directoriespwd (print working directory) command Displays the directory youre currently working in

    ~>Command-line prompt Indicates where to enter commands Varies depending on the shell*~> pwd/home/dustin

    Linux Operations and Administration

    Linux Operations and Administration

    Changing Directories (contd.)cd (change directory) command Switch to other directoriesCan add a command-line argumentInformation entered after a command to include specific instructions

    ~ (tilde) symbol Represents the users home directoryDirectory where the user has full permission to store files*~> cd /bin/bin> pwd/bin

    Linux Operations and Administration

    Linux Operations and Administration

    Changing Directories (contd.)Example: Users current directory is /bin~ symbol is used to change to the users home directory

    Can use the ~ symbol to specify another users home directory~> cd ~jasmine*/bin> cd ~~> pwd/home/isaiah

    Linux Operations and Administration

    Linux Operations and Administration

    Pathnames in LinuxAbsolute path methodStates the full pathname starting from root (/)Relative path methodSpecifies the pathname starting from the current directory*~/Desktop> cd ../../../etc~/etc> cd ..~> cd home/jake

    Linux Operations and Administration

    Linux Operations and Administration

    Pathnames in Linux (contd.).. (two dots)Navigate to a directory above your current directory with the relative method*~/Desktop> cd ../jake> pwd/home/jake

    Linux Operations and Administration

    Linux Operations and Administration

    The BASH Command Completion FeatureFinish partially typed commandsPress the Tab key to enableEnter enough characters for this feature to workParticularly if more than one subdirectory begins with the same letter*

    Linux Operations and Administration

    Linux Operations and Administration

    The BASH Command Completion Feature (contd.)Activity 3-1: Using the pwd and cd CommandsPractice navigating the Linux directory structure*

    Linux Operations and Administration

    Linux Operations and Administration

    Viewing Files and Directoriesls commandLists files and subdirectories in the current directoryUse arguments to specify other directories*~> lsbin DownloadDesktop Documents

    Linux Operations and Administration

    Linux Operations and Administration

    Viewing Files and Directories (contd.)Options Modify the way a command is carried outSyntax: command -options argumentMust include hyphen before the first option you use*~> ls -ltotal 548drwxr-xr-x 2 sarah users 4096 2012-03-06 20:01 bindrwxr-xr-x 2 sarah users 4096 2012-03-09 09:42 Desktop

    Linux Operations and Administration

    Linux Operations and Administration

    Viewing Files and Directories (contd.)-a optionDisplays all files including hidden files

    Table 3-2 Most common options for ls*~> ls -a. .cache .local bin Desktop Documents Download .. file1

    Linux Operations and Administration

    Linux Operations and Administration

    Viewing Files and Directories (contd.)*Table 3-2 Options with the ls command

    Linux Operations and Administration

    Linux Operations and Administration

    Examining the ls -l CommandOutput of the ls -l command contains important information in eight separate columnsExample: drwxr-xr-x 2 natalie users 4096 2012-02-06 20:01 DesktopColumns:File type: d, which stands for a directoryFile permissions: displayed for three categories: user, group, and otherHard links: number of hard links associated with the file*

    Linux Operations and Administration

    Linux Operations and Administration

    Examining the ls -l Command (contd.)Owneruser owner of the fileGroupfiles group ownerFile sizein bytes by defaultModification timetimestamp showing when the file was last modifiedFilenamename of the file*

    Linux Operations and Administration

    Linux Operations and Administration

    Examining the ls -l Command (contd.)*Figure 3-3 A guide to the ls -l commands outputCengage Learning 2013

    Linux Operations and Administration

    Linux Operations and Administration

    Examining the ls -l Command (contd.)Activity 3-2: Using the ls CommandUse commands for viewing Linux files and directories*

    Linux Operations and Administration

    Linux Operations and Administration

    Getting HelpMan (manual) pages Documentation files that describe Linux shell commands, executable programs, system calls, special files

    *man lsMan: find all matching manual pages* ls (1)ls (1p)Man: What manual page do you want?

    Linux Operations and Administration

  • *Figure 3-4 Excerpt from the man page for the ls commandCengage Learning 2013

    Linux Operations and Administration

    Linux Operations and Administration

    Linux Operations and Administration

    Getting Help (contd.)*Table 3-3 Man page section

    Linux Operations and Administration

    Linux Operations and Administration

    Navigating Man PagesTable 3-4 Lists ways to navigate man pagesActivity 3-3: Working with Man PagesFind and navigate man pages for any Linux command*

    Linux Operations and Administration

    Linux Operations and Administration

    Navigating Man Pages (contd.)*Table 3-4 Methods of navigating man pages

    Linux Operations and Administration

    Linux Operations and Administration

    Using WildcardsWildcardRepresents letters and characters used to specify a filename for searchesLinux administrators use wildcards to: Navigate to directories fasterMove or delete a group of filesLocate files based on a portion of their filenamesTable 3-5Describes wildcards used in Linux*

    Linux Operations and Administration

    Linux Operations and Administration

    Using Wildcards (contd.)*Table 3-5 Wildcards

    Linux Operations and Administration

    Linux Operations and Administration

    Using Wildcards (contd.)* wildcard Represents zero or more characters

    ? wildcard Represents only one character*ls fi*file1 file2~/newdirectory> ls file?file1 file2

    Linux Operations and Administration

    Linux Operations and Administration

    Using Wildcards (contd.)Activity 3-4: Using WildcardsUse wildcards to search for files and directories*

    Linux Operations and Administration

    Linux Operations and Administration

    Managing Files and DirectoriesLinux administrator should know how to use the command line to:Create files and directoriesMove files in and out of directoriesDelete and copy files and directoriesView file and directory contents*

    Linux Operations and Administration

    Linux Operations and Administration

    Creating and Deleting Directories and FilesDirectories Essential for keeping files organized in the Linux file systemEach directory has its own permissions assignedCommands to work with directories and files:mkdirtouchrmrmdir*

    Linux Operations and Administration

    Linux Operations and Administration

    Creating Directoriesmkdir (make directory) command Create directories*~> mkdir Studynotes~> lsbin Documents Music Public Studynotes VideosDesktop Download Pictures public_html Templates

    Linux Operations and Administration

    Linux Operations and Administration

    Creating FilesSeveral ways to create a file in Linuxtouch commandCreate a new empty file Update the timestamp of an existing file

    Use wildcards to update multiple files at the same time*~/Studynotes> touch chapter1~/Studynotes> ls -ltotal 4-rw-r--r-- 1 keith users 0 2012-03-19 13:29 chapter1

    Linux Operations and Administration

    Linux Operations and Administration

    Deleting Directories and FilesDeleting directories can be dangerousNo confirmation message in LinuxMost Linux users log in with an account that has limited permissionsSwitch to the root user only to do administrative tasksrm command Remove files and directoriesrmdir command Remove empty directories*

    Linux Operations and Administration

    Linux Operations and Administration

    Deleting Directories and Files (contd.)*1. ~/Studynotes> cd Math2. ~/Studynotes/Math> ls3. ~/Studynotes/Math> cd ..4. ~/Studynotes> pwd5. /home/andrea/Studynotes6. ~/Studynotes> rmdir Math7. ~/Studynotes> ls -l8. total 24drwxr-xr-x 2 andrea users 4096 2012-03-20 09:27 Englishdrwxr-xr-x 2 andrea users 4096 2012-03-20 09:27 Week1drwxr-xr-x 2 andrea users 4096 2012-03-20 09:27 Week2

    Linux Operations and Administration

    Linux Operations and Administration

    Deleting Directories and Files (contd.)Remove a directory that isnt emptyError message: rmdir: failed to remove 'Math': Directory not emptyActivity 3-5: Creating and Deleting Directories and FilesUse Linux commands for creating and deleting directories and files*

    Linux Operations and Administration

    Linux Operations and Administration

    Moving, Renaming, and Copying Filesmv command Rename filesMove files from one directory to anotherSyntax: mv filename new locationExample: move the notes file to another directory and rename it Newnotes at the same time:~/Studynotes/Week1> mv notes ~/Studynotes/Week2/newnotes*

    Linux Operations and Administration

    Linux Operations and Administration

    Moving, Renaming, and Copying Files (contd.)cp (copy) command Copies files and directoriesSyntax: cp filename new locationExample: ~/Studynotes/Week2> cp newnotes ~/Studynotes/Week1Activity 3-6: Renaming, Moving, and Copying FilesUse Linux commands for renaming, moving, and copying files*

    Linux Operations and Administration

    Linux Operations and Administration

    Creating LinksLearn how to create links with the ln commandLearn about inodes*

    Linux Operations and Administration

    Linux Operations and Administration

    InodesInode Data structure that stores all information about a file except the actual data and filenameInode numberLike an addressReferences an entry in the inode tableList of inodes for all files on a Linux partitionTable entry points to the datas location on the disk*

    Linux Operations and Administration

    Linux Operations and Administration

    Inodes (contd.)ls -il commandView a files inode number*~/Math> ls -iltotal 43327 -rw-r--r-- 1 dustin users 0 2012-03-21 15:46 algebra

    Linux Operations and Administration

    Linux Operations and Administration

    Hard LinksHard links Files that point to data on the hard driveCreate a fileAutomatically linked to the actual data stored on a partition Assigned an inode number referencing this dataCreate a hard link:~/Math/Week1/Calculus> ln notes ~/MathDelete filesData isnt deleted until the last link is deleted*

    Linux Operations and Administration

    Linux Operations and Administration

    Symbolic (Soft) LinksSymbolic links Also called soft linksSpecial types of files that point to other files instead of pointing to data on the hard driveDo not share the same inode numberBenefit of creating a symbolic link Link files that are on separate partitions or even different computers*

    Linux Operations and Administration

    Linux Operations and Administration

    Symbolic (Soft) Links (contd.)

    Activity 3-7: Working with LinksIdentify inode numbers and create hard and symbolic links*~/Sports> ln -s football baseball~/Sports> ls -iltotal 43935 lrwxrwxrwx 1 edward users 8 2012-03-22 10:11 baseball -> football3934 -rw-r--r-- 1 edward users 0 2012-03-22 10:10 football

    Linux Operations and Administration

    Linux Operations and Administration

    Switching Users and Becoming RootEvery user must have a username and password and belong to a primary groupCan switch to a different user account while staying in the same terminal windowsu (switch user) command Enables one user to become another user temporarily*

    Linux Operations and Administration

  • Activity 3-8: Switching UsersSwitch users without logging off the computer*1. ~> su jasmine2. Password:3. jasmine@client:/home/dustin> pwd4. /home/dustin5. jasmine@client:/home/dustin> exit6. exit7. ~> su8. Password:9. client:/home/dustin # pwd10. /home/dustin11. client:/home/dustin # exit12. exit13. ~>Linux Operations and Administration

    Linux Operations and Administration

    Linux Operations and Administration

    SummaryNearly all major Linux distributions follow the Filesystem Hierarchy Standard (FHS)Commands:pwd displays current directorycd changes directoryls lists contents of a directoryman shows help filesmkdir creates a directoryrm removes a file or directorymv moves and renames files

    *

    Linux Operations and Administration

    Linux Operations and Administration

    Summary (contd.)cp copies filesln makes hard and symbolic linkssu changes the current user temporarily*

    Linux Operations and Administration

    ***************************************************