This material is based on work supported by the National Science Foundation under Grant No. 0802551 Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author (s) and do not necessarily reflect the views of the National Science Foundation C1L9S1 Computers store thousands of files, directories, and folders scattered across one or many drives. To keep track of all files, computers keep a detailed record of each file, its location, changes made to the file, access dates, and other important information. Linux has a similar system of file storage and location process called the Filesystem Hierarchy Standard which defines where directories and the files contained within them are stored. This filesystem U the standard is explored in this lesson. It is very important to have a clear understanding of filesystems and storage locations because improper use of filesystem tools can corrupt or destroy important data. You will learn to manipulate files and directories using command line and other tools. Additionally, you will develop an understanding of the hierarchy used within Linux for file location and storage. C1L9S2 Student Expectations You should know what will be expected of you when you complete this lesson. These expectations are presented as objectives. Objectives are short statements of expectations that tell you what you must be able to do, perform, learn, or adjust after reviewing the lesson. Select the Objective tab on the left to review this lesson’s objective. C1L9S3 Objective Given a configured Linux systems, users will be able to describe the Linux Filesystem Hierarchy Standard and use Linux commands to manipulate files as per industry standards. C1L9S4 During this lesson, you will explore: • • • • • • • • Filesystem Hierarchy (FHS) File directory in Linux File types Viewing directories and files Creating directories and files Moving directories and files Using wildcards Deleting directories and files C1L9S5 Every operating system follows a standard to determine where files are stored on a hard drive so they can be found and used by the operating system at a later time. This standard is called a filesystem. File systems allow files to be organized in a hierarchical-based directory. A hierarchy of files is necessary to give preferences and permissions to certain files but not to others. File directories are used to store files, subdirectories, programs, and configuration files. Linux uses the Filesystem Hierarchy Standard (FHS) –a process that began in the early 1990’s as an attempt to restructure the file and directory system in Linux and later, in other Unix-like distributions. However, most modern Unix/Linux distributions do not follow these standards as they were originally intended. Further reading: Linux Filesystem Hierarchy from TLDP Graphic of Filesystem Hierarchy C1L9S6 Understanding the Linux filesystem hierarchy will allow you to save, locate, and retrieve files as needed. In the Unix/Linux environment (as with any other operating system), each directory has it’s own purpose and use. Unix/Linux systems have a root login, which requires a password to access. Linux systems also have a root directory represented by a forward slash (/). An absolute path or absolute file location always includes the root directory; consequently, the path begins with the forward slash (/) to indicate the root directory. To view a demonstration of the PWD command, select PLAY below. View Video VideoLesson9PrintWorkingD irectory(C1L9S16).mp4 You should always differentiate between the root login and the root directory. One is often confused with the other. Tip: If you forget the your current file location while working in a Linux command line environment, type the command pwd (print working directory) at the command prompt, and your path or current working directory will be displayed. C1L9S7 The standard directory system in some Linux distributions (i.e. UBUNTU) may contain some of the following main directories: /bin, /dev, /etc, /home, /media, /root, /sbin, /tmp, /usr, and /var. Review the Linux File System Tree Overview link to learn about the specific purpose of each directory. In Unix/Linux systems, everything is considered a file. Therefore, every path would begin with the forward slash (root directory). For example: /home/mary/client • client is located in the mary subdirectory • mary is located in the home subdirectory • home is located in the root (/) directory Locations within the file directory tree can be identified as having a relative or absolute pathname. For example, an absolute name would start with the slash "/", while a relative path name would not. Required Reading Root directory illustration C1L9S8 Linux files are categorized as “shareable versus unshareable,” and “variable” versus “static.” A shareable file is one that may be stored on one computer but accessed and used by other computers. An unshareable file will be the opposite—it is not shared with other computers. Files used for security purposes and device lock files are examples of unshareable files. A static file is one that does not change during regular use, however, a system administrator may make changes to static files. Examples of static files include documentation files and libraries. Static Variable Shareable Unshareable /usr /etc /opt /boot /var/mail /var/run /var/spool/news /var/lock On the other hand, a variable file changes regularly and does not require a system administrator to make changes to it. C1L9S9 There are six specific file types in Unix/Linux. These file types create a file system where all processes, devices, and connections are accessed. File types include: • Regular file – almost anything in an Unix/Linux system • • • • • Static Shareable Unshareable /usr /etc /opt /boot can be considered a file including text, binary, or Variable /var/mail /var/run executable items. /var/spool/news /var/lock Directory – a directory is a location to save other files. In Unix/Linux, a file may be listed in more than one FHS compliant system directory to create a “link.” Device – all devices in Unix/Linux environments can be accessed as files. Socket – also referred to as "Unix domain socket" or "local domain socket." A socket represents the communication between two programs. Pipe – Allows communication between two programs (or commands). Symbolic (soft) link – similar to a “shortcut” in Windows. A link points to an original file, but if the original file is deleted, the link will be broken and invalid. Directory links are considered “soft links.” C1L9S10 To view file types in a Unix/Linux environment type ls –all or ls –l at the command prompt. The results will display information for each file inside the directory. Let’s review Figure 1: The dash (–) in front of the file permissions indicates the file is regular. The letter “d” indicates the filename is a directory (inside the directory) which makes it a sub-directory. The letter “l” indicates a symbolic file. To review the different switches you can use with the ls command (list directory contents) you can review previous lessons. To search man pages in your Unix/Linux system type man ls at the command prompt. clients and sales are directories. Notice the d at the beginning of the file rights (left). gomez and smith are files, notice the dash (-) at the beginning of the file rights Rodriguez smith represents a soft link file where smith is the source file and rodriguez is the symbolic link. (If smith is deleted, destroyed, or corrupted, then the rodriguez symbolic link would not function.) C1L9S11 Access your command line to create files. Refer to Fig 2. To create a directory: mkdir <name of the file> To create the clients directory: mkdir clients To create a file: touch <nameofthefile> To create the gomez file: touch gomez To create multiple directories in one command line: mkdir <directory name 1> <directory name 2> To create smith and lopez: mkdir smith lopez To create a symbolic link; ln –s <nameofsourcefile> <nameofsymboliclink> To remove a symbolic link, view this reference. To create the Rodriguez symbolic link, type: ln –s smith rodriguez To view the contents of the directory, type: ls –l at the command prompt. C1L9S12 The mv (move command allows you to move files and directories. (Refer to figure 3). Terry created a file and named it bonus. At the command prompt, he types ls –l to view the list of files in that directory. He is certain bonus is a file (and not a directory) because the first character that appears for the bonus file is a dash (–) indicating file status. Terry wants to move the bonus file into the lopez subdirectory. At the prompt he types mv bonus lopez where bonus is the file to be moved and lopez is the destination directory to which bonus will be moved. Terry types ls -l again to ensure bonus was indeed moved to the lopez directory. The file bonus does not show in the original directory listing. Therefore, bonus was indeed moved. C1L9S13 Now Terry wants to make sure bonus is inside the lopez subdirectory. Before he can list the contents of the lopez subdirectory, he needs to move into the lopez subdirectory. At the prompt he types cd lopez Then he types ls –l to list the contents of the lopez subdirectory. The results confirm that the bonus file is stored at that location. To make sure, Terry types pwd to print the working directory, and indeed it displays Terry’s current working directory as /home/terry/lopez. To move out of the lopez subdirectory Terry types cd .. (cd command, followed by a space and two periods). This command takes him out of the lopez subdirectory and back to /home/terry. This move is confirmed when he types pwd (to check his current working directory) and it returns his path as being /home/terry. C1L9S14 Sometimes you will need to find a file but do not remember where you saved or stored it. To locate misplaced files, you can use the find command. You can find a file by typing the command find <filename> at the command prompt. For example, Terry forgot where he stored the bonus file. At the prompt, he types: find . –name bonus Bonus file is located in the lopez directory The computer returns the output indicating bonus is located inside the lopez subdirectory. Note: Years ago, you needed to add print at the end of the command line to get results. Most modern Unix distributions do not require this. C1L9S15 Sometimes you will not remember the exact name or spelling of a file or directory you seek. In these instances, wildcard characters work well. There are two types of wildcard characters: the asterisk (*) and the question mark (?). Both types may be used in place of actual letters. However, there is a slight difference between them. The asterisk (represented by the * character on your keyboard) represents any number of characters. The question mark (represented by the ? symbol on your keyboard) represents one character. For example, if you are searching for a file that begins with B and you know it is four letters long, you would type ls B??? at the command prompt. If you knew the filename begins with b but you do not remember the other letters in the filename, you would type ls b* at the command prompt. ls b??? Ls b??? is the command to list the contents of the directory for all filenames that begin with the letter b and are four characters long. ls b* Ls b* is the command to list the contents of the directory for all filenames that begin with the letter b. The asterisk (*) wildcard is used more frequently than the question mark (?) character to do wildcard searches in a Unix/Linux environment. C1L9S16 Sometimes you will need to remove directories and files. The following commands will allow you to achieve this goal (refer to figure 4): To remove a file at the command prompt: rm <name of file> For example, to remove the gomez file: rm gomez To learn more about the rm command you may visit the man rm page. By default, the rm command removes files, not directories. Remember: Unix/Linux systems are case sensitive. Consequently, rm Gomez is not the same as rm gomez! C1L9S17 To remove a directory: rmdir <nameofdirectory> For example, to remove the clients directory: rmdir clients This directory was deleted because it was empty. If there were other items or files contained in this directory, you would have to use the rm –r command to delete it. To remove or delete a symbolic link: rm <symbolic link> The removal of a symbolic link will not affect nor delete the original file. To view the listing of directories and files created type: ls –l (or –all) at the command prompt. C1L9S18 Linux distributions use the Filesystem Hierarchy Standard (FHS) to determine the content and location of files and directories, but not all Linux distros follow this standard precisely. Linux is case sensitive so upper and lower case letters are differentiated. However, Linux treats everything within the OS as files. The main file location is called the root directory and is represented with a forward slash (/). File locations are called paths and can be relative or absolute. Relative paths are referenced based on your current location within a directory, while absolute paths have a fixed location and are not based on your current directory or location. Absolute paths reference the root directory. Linux accepts various commands at a command prompt to create, move, view, locate, rename, and delete files. C1L9S19