1 / 41

Linux File Structure

Linux File Structure. Prepared by: Dr. Bahjat Qazzaz. File Structure. All of the files in the Linux file system are organized into a multi-leveled hierarchy called: a directory tree.

Télécharger la présentation

Linux File Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux File Structure Prepared by: Dr. BahjatQazzaz

  2. File Structure • All of the files in theLinux file system are organized into a multi-leveled hierarchy called: a directory tree. • At the very top of the file system is single directory called "root" which is represented by a / (slash). • All other files are "descendents" of root.

  3. File Structure • The number of levels is largely arbitrary, although most UNIX systems share some organizational similarities. • Example:

  4. File Structure / (root) | ------------------------------------------------------------- | | | /bin /usr /home | | ----------------------- ------------------------------ | | | | | | /public /misc /staff /bahjat /talat /rafat | | --------------- ------------------------------- | | | | | | /software /doc /john /mary /bill /carl

  5. File Structure: File Types • The UNIX file system contains several different types of files: • Ordinary Files • Used to store your information, such as some text you have written or an image you have drawn. This is the type of file that you usually work with. • Always located within/under a directory file • Do not contain other files

  6. File Structure: File Types • Directories • Branching points in the hierarchical tree • Used to organize groups of files • May contain ordinary files, special files or other directories • Never contain "real" information which you would work with (such as text). Basically, just used for organizing files. • All files are descendants of the root directory, ( named / ) located at the top of the tree.

  7. File Structure: File Types • Special Files • Used to represent a real physical device such as a printer, tape drive or terminal, used for Input/Ouput (I/O) operations • Unix considers any device attached to the system to be a file - including your terminal: • By default, a command treats your terminal as the standard input file (stdin) from which to read its input • Your terminal is also treated as the standard output file (stdout) to which a command's output is sent • Stdin and stdout will be discussed in more detail later • Two types of I/O: character and block • Usually only found under directories named /dev

  8. File Structure: File Types • Pipes • UNIX allows you to link commands together using a pipe. The pipe acts a temporary file which only exists to hold data from one command until it is read by another • For example, to pipe the output from one command into another command:

  9. File Structure: File Types For example: who | wc -l This command will tell you how many users are currently logged into the system. man cd | wc –l This command will tell you how many lines the man cd will display.

  10. File Structure: File Names • UNIX permits file names to use most characters, but avoid • spaces, • tabs and • characters that have a special meaning to the shell, such as: & ; ( ) | ? \ ' " ` [ ] { } < > $ - ! / Case Sensitivity: uppercase and lowercase are not the same! • These are three different files: NOVEMBER Novembernovember

  11. File Structure: File Name • Length: can be up to 256 characters • Extensions: may be used to identify types of files libc.a - archive, library file program.c - C language source file alpha2.f - Fortran source file xwd2ps.o - Object/executable code mygames.Z - Compressed file

  12. File Structure: File Name • Hidden Files: have names that begin with a dot (.) For example: .bash_history .bash_logout .mailrc .mwmrc • Uniqueness: as children in a family, • no two files with the same parent directory can have the same name. • Files located in separate directories can have identical names. • Reserved Filenames: / - the root directory (slash) . - current directory (period) .. - parent directory (double period) ~ - your home directory (tilde)

  13. File Structure: Pathnames • Specify where a file is located in the hierarchically organized file system • Absolute Pathname: tells how to reach a file begining from the root; always begins with / (slash). For example: /usr/local/doc/training/sample.f

  14. File Structure: Pathnames • Relative Pathname: tells how to reach a file from the directory you are currently in ( current or working directory); never begins with / (slash). For example: • training/sample.f ../bin ~/projects/report.001

  15. File Structure: Pathnames For example, • if your current directory is /usr/home/sami and you wanted to change to the directory /usr/home/ali you could use either of these commands: cd ../ali- relative pathname cd /usr/home/ali- absolute pathname

  16. File and Directory Commands Prepared by: Dr. BahjatQazzaz

  17. File and Directory Commands • Linux provides a number of commands for working with files. • These commands usually have • several options and • accept wildcard characters as arguments. For details, see the respective man pages • The more common ones are described below:

  18. File and Directory Commands • ls lists files ls- show contents of working directory ls file - list file, if it exists in working directory ls dir- show contents of the directory dir ls -a- shows all your files, including hidden ones ls -al- give detailed listing of contents ls -F- mark directories with "/" and executable files with "*" ls *.doc- show all files with suffix ".doc"

  19. File and Directory Commands • more : browses/displays files one screen at a time. Use • h for help, • spacebar to page, • b for back, • q to quit, • /string to search for string more sample.f

  20. File and Directory Commands • lesssimilar to more, but with more features. Not available on every system. less sample.f • head displays the first n lines of a file head sample.f- display first 10 lines (default) head -5 sample.f- display first 5 lines head -25 sample.f- display first 25 lines

  21. File and Directory Commands • taildisplays the last n lines or n characters of a file less sample.f- display last 10 lines (default) less -5 sample.f- display last 5 lines less -5c sample.f- display last 5 characters less -25 sample.f- display last 25 lines

  22. File and Directory Commands • catdumps the entire file to the screen without paging. • This command is more useful for concatenating files together than it is for reading files. cat myprog.c- diplays entire file cat -b myprog.c- shows line numbers cat file1 file2 > file3 - adds file1 and file2 to make file3

  23. File and Directory Commands • cpcopies files. • Will overwrite unless otherwise specified. • Must also have write permission in the destination directory. cp sample.f sample2.f - copies sample.f to sample2.f cp -R dir1 dir2 - copies contents of directory dir1 to dir2 cp -i file.1 file.new- prompts if file.new will be overwritten cp *.txt chapt1 - copies all files with .txt suffix to directory chapt1 cp /usr/doc/README ~ - copies file to your home directory cp ~betty/index . - copies the file "index" from user betty's home directory to current directory

  24. File and Directory Commands Prepared by: Dr. BahjatQazzaz

  25. File and Directory Commands • mv moves files. • Will overwrite unless otherwise specified. • Must also have write permission in the destination directory. mv sample.f sample2.f - moves sample.f to sample2.f mv dir1 newdir/dir2 - moves contents of directory dir1 to newdir/dir2 mv -i file.1 file.new- prompts if file.new will be overwritten mv *.txt chapt1 - moves all files with .txt suffix to directory chapt1

  26. File and Directory Commands • rm deletes/removes files or directories if file permissions permit. rm sample.f- deletes sample.f rm chap?.txt - deletes all files with chap as the first four characters of their name and with .txt as the last four characters of their name rm -i * - deletes all files in current directory but asks first for each file rm -r /olddir- recursively removes all files in the directory olddir, including the directory itself

  27. File and Directory Commands • file - identifies the "type" of file. The command syntax is: file filename For example: file * - reports all files in current directory and their types. The output might appear as shown below: about.html: ascii text bin: directory staff.directory: English text bggen: executable or object module not stripped bmbinc: commands text man2html: executable or object module not stripped man2html.c: ascii text

  28. File and Directory Commands • find - finds files. The syntax of this command is: find pathname -name filename -print • The pathname defines the directory to start from. • Each subdirectory of this directory will be searched. • The -print option must be used to display results. • You can define the filename using wildcards. If these are used, the filename must be placed in 'quotes'.

  29. File and Directory Commands • For example: • find . -name mtg_jan92 -print - looks for the file mtg_jan92 in current directory • find ~/ -name README -print - looks for files called README throughout your home directory • find . -name '*.fm' -print - looks for all files with .fm suffix in current directory • find /usr/local -name gnu -type d -print - looks for a directory called gnu within the /usr/local directory

  30. File and Directory Commands • diff - comparing two files or directories. • Indicates which lines need be added (a), deleted (d) or changed (c). • Lines in file1 are identified with a (<) symbol: lines in file2 with a (>) symbol diff file1 file2 - compares file1 to file2 diff -iw file1 file2 - compares two files ignoring letter case and spaces diff dir1 dir2 - compares two directories showing files which are unique to each and also, line by line differences between any files in common.

  31. File and Directory Commands • For example, if file1 and file2 are: John erpl08@ed John erpl08@ed Joe CZT@cern.ch Joe CZT@cern.ch Kim ks@x.co Jean JRS@pollux.ucs.co Keith keith@festival Jim jim@frolix8 Kim ks@x.co Keith keith@festival

  32. File and Directory Commands • Using the diff command: diff file1 file2 Yields the output: 2a3,4 > Jean JRS@pollux.ucs.co > Jim jim@frolix8 Which means that to make these files match you need to add (a) lines 3 and 4 (3,4) of file2 (>) after line 2 in file1

  33. File and Directory Commands • sdiff- similar to diff, but displays each line of the two files side by side, making it easier for you to see the differences between them • Lines that are different are shown with a | symbol. • Lines unique to file1 are identified by a < symbol; • lines unique to file2 with a > symbol. • Identical lines appear next to each other. • The option -w 80 is used to set the width of the output from the command to 80 characters. The default is 130 characters.

  34. File and Directory Commands • Foe example sdiff -w 80 file1 file2 Mike erpl08@ed | John erpl08@ed Joe CZT@cern.ch Joe CZT@cern.ch > Jean JRS@pollux.ucs.co > Jim jim@frolix8 Kim ks@x.co Kim ks@x.co Sam s.wally@aston < Keith keith@festival Keith keith@festival

  35. File and Directory Commands • lnlink one file name to another. The command syntax is: ln source linkname Making a link to a file or directory does not create another copy of it. It simply makes a connection between the source and the linkname. Allows a single file to be "pointed to" by other filenames without having to duplicate the file. ln results.1 last.run- links filename "last.run" to the real file results.1 in the current directory. ln notes ../Notes.jan - links filename "notes" in current directory to real file Notes.jan in parent directory.

  36. File and Directory Commands • sort • sorts files, • merges files that are already sorted, • and checks files to determine if they have been sorted. • The command syntax is: sort options filename By default, lines in "filename" are sorted and displayed to the screen. If the "filename" parameter specifies more than one file, the sort command concatenates the files and sorts them as one file. An output file can be specified with the -o flag. Files can be sorted by "fields" - single or multiple.

  37. File and Directory Commands • The sort command supports many options. See the man page for details. sort addresses- sorts the file addresses and displays output on screen sort -o sorted addresses- sorts the file addresses and writes output to the file called sorted. sort -u -o mail_labels addresses - removes all duplicate lines from the file addresses and writes the output in the file mail_labels. sort +2 -4 addresses - sorts the file by its third and fourth fields. Note that +2 means to skip first two fields and -4 means to stop after the fourth field.

  38. File and Directory Commands • pwd - print working directory. Tells you which directory you are currently in.

  39. File and Directory Commands • cd- change to specified directory. May specify either the absolute or relative pathname. • cdwith no pathname changes to your home directory. cd /usr/local - change to /usr/local cd doc/training - change to doc/training in current directory cd .. - change to parent directory cd ~/data - change to data directory in home directory cd ~joe- change to user joe's home directory cd- change to home directory

  40. File and Directory Commands • rmdir- remove directory. • Directories must be empty before you remove them. rmdir project1 To recursively remove nested directories, use the rm command with the -r option: rm -r dirctory_name

  41. File and Directory Commands

More Related