· 6 years ago · Sep 27, 2019, 05:10 AM
1http://www.pathname.com/fhs/ (check out this website it will tell you about different directory in ubuntu).
2
3Filesystem Hierarchy Standard
4 Some examples of filesystem types that Linux supports are:ext3, ext4, btrfs, xfs (native Linux filesystems),vfat, ntfs, hfs (filesystems from other operating systems).
5Partitions in Linux:
6 Each filesystem resides on a hard disk partition. Partitions help to organize the contents of disks according to the kind of data contained and how it is used.
7Mount Points:
8 Before you can start using a filesystem, you need to mount it to the filesystem tree at a mount point. This is simply a directory (which may or may not be empty) where the filesystem is to be attached (mounted). Sometimes you may need to create the directory if it doesn't already exist.
9 Warning: If you mount a filesystem on a non-empty directory, the former contents of that directory are covered-up and not accessible until the filesystem is unmounted. Thus mount points are usually empty directories.
10
11More About Mount Points:
12 The mount command is used to attach a filesystem (which can be local to the computer or, as we shall discuss, on a network) somewhere within the filesystem tree. Arguments include the device node and mount point. For example,
13
14 mount /dev/sda5 /home
15
16 will attach the filesystem contained in the disk partition associated with the /dev/sda5 device node, into the filesystem tree at the /home mount point. (Note that unless the system is otherwise configured only the root user has permission to run mount.) If you want it to be automatically available every time the system starts up, you need to edit the file /etc/fstab accordingly (the name is short for Filesystem Table). Looking at this file will show you the configuration of all pre-configured filesystems. man fstab will display how this file is used and how to configure it.Typing mount without any arguments will show all presently mounted filesystems.
17 The command df -Th (disk-free) will display information about mounted filesystems including usage statistics about currently used and available space.
18
19The Network Filesystem:
20 Using NFS (the Network Filesystem) is one of the methods used for sharing data across physical systems. Many system administrators mount remote users' home directories on a server in order to give them access to the same files and configuration files across multiple client systems. This allows the users to log in to different computers yet still have access to the same files and resources.
21
22How to use NFS on the Server:
23 We will now look in detail at how to use NFS on the server machine.On the server machine, NFS daemons (built-in networking and service processes in Linux) and other system servers are typically started with the following command: sudo service nfs start
24 The text file /etc/exports contains the directories and permissions that a host is willing to share with other systems over NFS. An entry in this file may look like the following:
25 /projects *.example.com(rw)
26
27 This entry allows the directory /projects to be mounted using NFS with read and write (rw) permissions and shared with other hosts in the example.com domain. As we will detail in the next chapter, every file in Linux has 3 possible permissions: read (r), write (w) and execute (x).
28 After modifying the /etc/exports file, you can use the exportfs -av command to notify Linux about the directories you are allowing to be remotely mounted using NFS (restarting NFS with sudo service nfs restart will also work, but is heavier as it halts NFS for a short while before starting it up again).
29
30NFS on the Client:
31 On the client machine, if it is desired to have the remote filesystem mounted automatically upon system boot, the /etc/fstab file is modified to accomplish this. For example, an entry in the client's /etc/fstab file might look like the following:
32 servername:/projects /mnt/nfs/projects nfs defaults 0 0
33 You can also mount the remote filesystem without a reboot or as a one-time mount by directly using the mount command:
34 $ mount servername:/projects /mnt/nfs/projects
35 Remember, if /etc/fstab is not modified, this remote mount will not be present the next time the system is restarted.
36
37proc Filesystem:
38 Certain filesystems like the one mounted at /proc are called pseudo filesystems because they have no permanent presence anywhere on disk.
39 The /proc filesystem contains virtual files (files that exist only in memory) that permit viewing constantly varying kernel data. This filesystem contains files and directories that mimic kernel structures and configuration information. It doesn't contain real files but runtime system information (e.g. system memory, devices mounted, hardware configuration, etc). Some important files in /proc are:
40
41 /proc/cpuinfo
42 /proc/interrupts
43 /proc/meminfo
44 /proc/mounts
45 /proc/partitions
46 /proc/version
47
48 /proc has subdirectories as well, including:
49
50 /proc/<Process-ID-#>
51 /proc/sys
52
53 The first example shows there is a directory for every process running on the system which contains vital information about it. The second example shows a virtual directory that contains a lot of information about the entire system, in particular its hardware and configuration. The /proc filesystem is very useful because the information it reports is gathered only as needed and never needs storage on disk.
54
55 The /home directory is often mounted as a separate filesystem on its own partition, or even exported (shared) remotely on a network through NFS.
56
57The /bin and /sbin Directories
58
59 The /bin directory contains executable binaries, essential commands used in single-user mode, and essential commands required by all system users, such as:
60 Command Usage
61 ps Produces a list of processes along with status information for the system.
62 ls Produces a listing of the contents of a directory.
63 cp Used to copy files.
64
65 To view a list of programs in the /bin directory, type: ls /bin
66 Commands that are not essential for the system in single-user mode are placed in the /usr/bin directory, while the /sbin directory is used for essential binaries related to system administration, such as ifconfig and shutdown. There is also a /usr/sbin directory for less essential system administration programs.
67
68 Sometimes /usr is a separate filesystem that may not be available/mounted in single-user mode. This was why essential commands were separated from non-essential commands. However, in some of the most modern Linux systems this distinction is considered obsolete, and /usr/bin and /bin are actually just linked together as are /usr/sbin and /sbin.
69
70
71The /dev Directory
72
73 The /dev directory contains device nodes, a type of pseudo-file used by most hardware and software devices, except for network devices. This directory is:
74 1-empty on the disk partition when it is not mounted
75 2-Contains entries which are created by the udev system, which creates and manages device nodes on Linux, creating them dynamically when devices are found. The /dev directory contains items such as:
76 /dev/sda1 (first partition on the first hard disk)
77 /dev/lp1 (second printer)
78 /dev/dvd1 (first DVD drive)
79
80The /var and /etc Directories
81 The /var directory contains files that are expected to change in size and content as the system is running (var stands for variable) such as the entries in the following directories:
82
83 System log files: /var/log
84 Packages and database files: /var/lib
85 Print queues: /var/spool
86 Temp files: /var/tmp
87
88 The /var directory may be put in its own filesystem so that growth of the files can be accommodated and the file sizes do not fatally affect the system. Network services directories such as /var/ftp (the FTP service) and /var/www (the HTTP web service) are also found under /var.
89
90 The /etc directory is the home for system configuration files. It contains no binary programs, although there are some executable scripts. For example, the file resolv.conf tells the system where to go on the network to obtain host name to IP address mappings (DNS). Files like passwd,shadow and group for managing user accounts are found in the /etc directory. System run level scripts are found in subdirectories of /etc. For example, /etc/rc2.d contains links to scripts for entering and leaving run level 2. The rc directory historically stood for Run Commands. Some distros extend the contents of /etc. For example, Red Hat adds the sysconfig subdirectory that contains more configuration files.
91
92The /boot Directory
93 The /boot directory contains the few essential files needed to boot the system. For every alternative kernel installed on the system there are four files:
94 vmlinuz: the compressed Linux kernel, required for booting
95 initramfs: the initial ram filesystem, required for booting, sometimes called initrd, not initramfs
96 config: the kernel configuration file, only used for debugging and bookkeeping
97 System.map: kernel symbol table, only used for debugging
98
99 Each of these files has a kernel version appended to its name.
100 The Grand Unified Bootloader (GRUB) files (such as /boot/grub/grub.conf or /boot/grub2/grub2.cfg) are also found under the /boot directory.
101
102The /lib and /media Directories
103 /lib contains libraries (common code shared by applications and needed for them to run) for the essential programs in /bin and /sbin. These library filenames either start with ld or lib, for example, /lib/libncurses.so.5.7.
104 Most of these are what are known as dynamically loaded libraries (also known as shared libraries or Shared Objects (SO)). On some Linux distributions there exists a /lib64 directory containing 64-bit libraries, while /lib contains 32-bit versions.
105 Kernel modules (kernel code, often device drivers, that can be loaded and unloaded without re-starting the system) are located in /lib/modules/<kernel-version-number>.
106
107 The /media directory is typically located where removable media, such as CDs, DVDs and USB drives are mounted. Unless configuration prohibits it, Linux automatically mounts the removable media in the /media directory when they are detected.
108
109 Directory name Usage
110 /opt Optional application software packages.
111 /sys Virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes.
112 /srv Site-specific data served up by the system. Seldom used.
113 /tmp Temporary files; on some distributions erased across a reboot and/or may actually be a ramdisk in memory.
114 /usr Multi-user applications, utilities and data.
115
116Subdirectories under /usr:
117 he /usr directory contains non-essential programs and scripts (in the sense that they should not be needed to initially boot the system) and has at least the following sub-directories:
118
119 Directory name Usage
120 /usr/include Header files used to compile applications.
121 /usr/lib Libraries for programs in /usr/bin and /usr/sbin.
122 /usr/lib64 64-bit libraries for 64-bit programs in /usr/bin and /usr/sbin.
123 /usr/sbin Non-essential system binaries, such as system daemons.
124 /usr/share Shared data used by applications, generally architecture-independent.
125 /usr/src Source code, usually for the Linux kernel.
126 /usr/X11R6 X Window configuration files; generally obsolete.
127 /usr/local Data and programs specific to the local machine. Subdirectories include bin, sbin, lib, share, include, etc.
128 /usr/bin This is the primary directory of executable commands on the system.
129
130
131Comparing Files:
132 diff is used to compare files and directories. This often-used utiility program has many useful options (see man diff) including:
133 diff Option Usage
134 -c Provides a listing of differences that include 3 lines of context before and after the lines differing in content
135 -r Used to recursively compare subdirectories as well as the current directory
136 -i Ignore the case of letters
137 -w Ignore differences in spaces and tabs (white space)
138 To compare two files, at the command prompt, type diff <filename1> <filename2>
139
140Using diff3 and patch:
141 You can compare three files at once using diff3, which uses one file as the reference basis for the other two. For example, suppose you and a co-worker both have made modifications to the same file working at the same time independently. diff3 can show the differences based on the common file you both started with. The syntax for diff3 is as follows:
142
143 diff3 MY-FILE COMMON-FILE YOUR-FILE
144
145 The graphic shows the use of diff3.
146
147 Many modifications to source code and configuration files are distributed utilizing patches, which are applied, not suprisingly, with the patch program. A patch file contains the deltas (changes) required to update an older version of a file to the new one. The patch files are actually produced by running diff with the correct options, as in:
148
149 diff -Nur originalfile newfile > patchfile
150
151 Distributing just the patch is more concise and efficient than distributing the entire file. For example, if only one line needs to change in a file that contains 1,000 lines, the patch file will be just a few lines long.
152 To apply a patch you can just do either of the two methods below:
153
154 $ patch -p1 < patchfile
155 $ patch originalfile patchfile
156
157 The first usage is more common as it is often used to apply changes to an entire directory tree, rather than just one file as in the second example. To understand the use of the -p1 option and many others, see the man page for patch.
158
159Using the 'file' utility
160 In Linux, a file's extension often does not categorize it the way it might in other operating systems. One can not assume that a file named file.txt is a text file and not an executable program. In Linux a file name is generally more meaningful to the user of the system than the system itself; in fact most applications directly examine a file's contents to see what kind of object it is rather than relying on an extension. This is very different from the way Windows handles filenames, where a filename ending with .exe, for example, represents an executable binary file.
161 The real nature of a file can be ascertained by using the file utility. For the file names given as arguments, it examines the contents and certain characteristics to determine whether the files are plain text, shared libraries, executable programs, scripts, or something else.
162
163Backing Up Data:
164 It can be done by using 2 commands like rsync and cp.
165
166 rsync is more efficient because it checks if the file being copied already exists. If the file exists and there is no change in size or modification time, rsync will avoid an unnecessary copy and save time. Furthermore, because rsync copies only the parts of files that have actually changed, it can be very fast.
167
168 cp can only copy files to and from destinations on the local machine (unless you are copying to or from a filesystem mounted using NFS), but rsync can also be used to copy files from one machine to another. Locations are designated in the target:path form where target can be in the form of [user@]host. The user@ part is optional and used if the remote user is different from the local user.
169
170 rsync is very efficient when recursively copying one directory tree to another, because only the differences are transmitted over the network. One often synchronizes the destination directory tree with the origin, using the -r option to recursively walk down the directory tree copying all files and directories below the one listed as the source.
171
172Using rsync:
173
174 rsync is a very powerful utility. For example, a very useful way to back up a project directory might be to use the following command:
175 $ rsync -r project-X archive-machine:archives/project-X
176 Note that rsync can be very destructive! Accidental misuse can do a lot of harm to data and programs by inadvertently copying changes to where they are not wanted. Take care to specify the correct options and paths. It is highly recommended that you first test your rsync command using the -dry-run option to ensure that it provides the results that you want.
177 To use rsync at the command prompt, type
178
179 rsync sourcefile destinationfile
180
181 where either file can be on the local machine or on a networked machine.The contents of sourcefile are copied to destinationfile.
182
183
184Compressing Data:
185 File data is often compressed to save disk space and reduce the time it takes to transmit files over networks.
186 Command Usage
187 gzip The most frequently used Linux compression utility
188 bzip2 Produces files significantly smaller than those produced by gzip
189 xz The most space efficient compression utility used in Linux
190 zip Is often required to examine and decompress archives from other operating systems
191
192 These techniques vary in the efficiency of the compression (how much space is saved) and in how long they take to compress; generally the more efficient techniques take longer. Decompression time doesn't vary as much across different methods.
193
194 In addition the tar utility is often used to group files in an archive and then compress the whole archive at once.
195
196Compressing Data Using gzip:
197 gzip is the most oftenly used Linux compression utility. It compresses very well and is very fast. The following table provides some usage examples:
198 Command Usage
199 gzip * Compresses all files in the current directory; each file is compressed and renamed with a .gz extension.
200 gzip -r projectX Compresses all files in the projectX directory along with all files in all of the directories under projectX.
201 gunzip foo De-compresses foo found in the file foo.gz. Under the hood, gunzip command is actually the same as gzip –d.
202
203Compressing Data Using bzip2:
204 bzip2 has syntax that is similar to gzip but it uses a different compression algorithm and produces significantly smaller files, at the price of taking a longer time to do its work. Thus, It is more likely to be used to compress larger files.
205
206 Examples of common usage are also similar to gzip:
207 Command Usage
208 bzip2 * Compress all of the files in the current directory and replaces each file with a file renamed with a .bz2 extension.
209 bunzip2 *.bz2 Decompress all of the files with an extension of .bz2 in the current directory. Under the hood, bunzip2 is the same as calling bzip2 -d.
210
211Compress Data Using xz:
212 xz is the most space efficient compression utility used in Linux and is now used by www.kernel.org to store archives of the Linux kernel. Once again it trades a slower compression speed for an even higher compression ratio.
213 Some usage examples:
214
215 Command Usage
216 xz * Compress all of the files in the current directory and replace each file with one with a .xz extension.
217 xz foo Compress the file foo into foo.xz using the default compression level (-6), and remove foo if compression succeeds.
218 xz -dk bar.xz Decompress bar.xz into bar and don't remove bar.xz even if decompression is successful.
219 xz -dcf a.txt b.txt.xz > abcd.txt Decompress a mix of compressed and uncompressed files to standard output, using a single command.
220 xz -d *.xz Decompress the files compressed using xz.
221
222 Compressed files are stored with a .xz extension.
223
224Handling Files Using zip:
225 The zip program is not often used to compress files in Linux, but is often required to examine and decompress archives from other operating systems. It is only used in Linux when you get a zipped file from a Windows user. It is a legacy program.
226 Command Usage
227 zip backup * Compresses all files in the current directory and places them in the file backup.zip.
228 zip -r backup.zip ~ Archives your login directory (~) and all files and directories under it in the file backup.zip.
229 unzip backup.zip Extracts all files in the file backup.zip and places them in the current directory.
230
231Archiving and Compressing Data Using tar
232 Historically, tar stood for "tape archive" and was used to archive files to a magnetic tape. It allows you to create or extract files from an archive file, often called a tarball. At the same time you can optionally compress while creating the archive, and decompress while extracting its contents.
233
234 Here are some examples of the use of tar:
235 Command Usage
236 tar xvf mydir.tar Extract all the files in mydir.tar into the mydir directory
237 tar zcvf mydir.tar.gz mydir Create the archive and compress with gzip
238 tar jcvf mydir.tar.bz2 mydir Create the archive and compress with bz2
239 tar Jcvf mydir.tar.xz mydir Create the archive and compress with xz
240 tar xvf mydir.tar.gz Extract all the files in mydir.tar.gz into the mydir directory. Note you do not have to tell tar it is in gzip format.
241
242 You can separate out the archiving and compression stages, as in:
243
244 tar mydir.tar mydir ; gzip mydir.tar
245 gunzip mydir.tar.gz ; tar xvf mydir.tar
246
247 but this is slower and wastes space by creating an unneeded intermediary .tar file.
248
249Disk-to-Disk Copying:
250 The dd program is very useful for making copies of raw disk space. For example, to back up your Master Boot Record (MBR) (the first 512 byte sector on the disk that contains a table describing the partitions on that disk), you might type:
251
252 dd if=/dev/sda of=sda.mbr bs=512 count=1
253
254 To use dd to make a copy of one disk onto another, (WARNING!) deleting everything that previously existed on the second disk, type:
255
256 dd if=/dev/sda of=/dev/sdb
257
258 An exact copy of the first disk device is created on the second disk device.
259 Do not experiment with this command as written above as it can erase a hard disk!
260 Exactly what the name dd stands for is an often-argued item. The words data definition is the most popular theory and has roots in early IBM history. Often people joke that it means disk destroyer and other variants such as delete data!