Introduction to Linux. Lesson 1. Unix Command Line. Part 1.
10K+
Managing files under Linux is different from managing files under Windows NT/200x/ XP/Vista, and radically different from managing files under Windows 95/98. In this section, we discuss basic file management tools and concepts under Linux. We’ll start with specifics on some useful general-purpose commands, and then we’ll step back and look at some background information. Under Linux (and UNIX in general), almost everything is abstracted to a file. Originally, this was done to simplify the programmer’s job. Instead of having to communicate directly with device drivers, special files (which look like ordinary files to the application) are used as a bridge. Several types of files accommodate all these file uses. Normal Files Normal files are just that—normal. They contain data or executables, and the operating system makes no assumptions about their contents.
Directory files are a special instance of normal files. Directory files list the locations of other files, some of which may be other directories. (This is similar to folders in Windows.) In general, the contents of directory files won’t be of importance to your daily operations, unless you need to open and read the file yourself rather than using existing applications to navigate directories. (This would be similar to trying to read the DOS file allocation table directly rather than using command.com to navigate directories or using the findfirst/ findnext system calls.)
Each file in the Linux file system gets its own i-node. An i-node keeps track of a file’s attributes and its location on the disk. If you need to be able to refer to a single file using two separate filenames, you can create a hard link. The hard link will have the same i-node as the original file and will, therefore, look and behave just like the original. With every hard link that is created, a reference count is incremented. When a hard link is removed, the reference count is decremented. Until the reference count reaches zero, the file will remain on disk. NOTE A hard link cannot exist between two files on separate partitions. This is because the hard link refers to the original file by i-node, and a file’s i-node may differ among file systems.
Unlike hard links, which point to a file by its i-node, a symbolic link points to another file by its name. This allows symbolic links (often abbreviated symlinks) to point to files located on other partitions, even other network drives.
Since all device drivers are accessed through the file system, files of type block device are used to interface with devices such as disks. A block device file has three identifying traits: ▼ It has a major number. ■ It has a minor number. ▲ When viewed using the ls -l command, it shows b as the first character of the permissions field. For example, [yyang@fedora-serverA ~]$ ls -l /dev/sda brw-r----- 1 root disk 8, 0 2090-09-30 08:18 /dev/sda Note the b at the beginning of the file’s permissions; the 8 is the major number, and the 0 is the minor number. A block device file’s major number identifies the represented device driver. When this file is accessed, the minor number is passed to the device driver as a parameter, telling it which device it is accessing. For example, if there are two serial ports, they will share the same device driver and thus the same major number, but each serial port will have a unique minor number.
Similar to block devices, character devices are special files that allow you to access devices through the file system. The obvious difference between block and character devices is that block devices communicate with the actual devices in large blocks, whereas character devices work one character at a time. (A hard disk is a block device; a modem is a character device.) Character device permissions start with a c, and the file has a major number and a minor number. For example, [yyang@fedora-serverA ~]$ ls -l /dev/ttyS0 crw-rw---- 1 root uucp 4, 64 2007-09-30 08:18 /dev/ttyS0
Named pipes are a special type of file that allows for interprocess communication. Using the mknod command, you can create a named pipe file that one process can open for reading and another process can open for writing, thus allowing the two to communicate with one another. This works especially well when a program refuses to take input from a command-line pipe, but another program needs to feed the other one data and you don’t have the disk space for a temporary file. For a named pipe file, the first character of its file permissions is a p. For example, if a named pipe called mypipe exists in your present working directory (PWD), a long listing of the named pipe file would show this: [yyang@fedora-serverA ~]$ ls -l mypipe prw-r--r-- 1 root root 0 Mar 16 10:47 mypipe
Out of necessity, we have been using the ls command in previous sections and chapters of this book. We will look at the ls command and its options in more details here. The ls command is used to list all the files in a directory. Of more than 50 available options. The options can be used in any combination. To list all files in a directory with a long listing, type this command: [yyang@fedora-serverA ~]$ ls -la To list a directory’s nonhidden files that start with the letter A, type this: [yyang@fedora-serverA ~]$ ls A*
Option for ls Description -l Long listing. In addition to the filename, shows the file size, date/time,permissions, ownership, and group information. -a All files. Shows all files in the directory,including hidden files. Names of hidden files begin with a period. -t Lists in order of last modified time. -r Reverses the listing. -1 Single-column listing. -R Recursively lists all files and subdirectories
It is inevitable that you will sit down in front of an already logged-in workstation and not know where you are in the directory tree. To get this information, you need the pwd command. Its only task is to print the current working directory. To display your current working directory, use this command: [yyang@fedora-serverA ~]$ pwd /home/yyang
The cat program fills an extremely simple role: to display files. More creative things can be done with it, but nearly all of its usage will be in the form of simply displaying the contents of text files—much like the type command under DOS. Because multiple filenames can be specified on the command line, it’s possible to concatenate files into a single, large, continuous file. This is different from tar in that the resulting file has no control information to show the boundaries of different files. To display the /etc/passwd file, use this command: [yyang@fedora-serverA ~]$ cat /etc/passwd To display the /etc/passwd file and the /etc/group file, issue this command: [yyang@fedora-serverA ~]$ cat /etc/passwd /etc/group Type this command to concatenate /etc/passwd with /etc/group and send the output into the file users-and-groups.txt: [yyang@fedora-serverA ~]$ cat /etc/passwd /etc/group > users-and-groups.txt To append the contents of the file /etc/hosts to the users-and-groups.txt file you just created, type [yyang@fedora-serverA ~]$ cat /etc/hosts >> users-and-groups.txt
Content type
Image
Digest
Size
91 MB
Last updated
almost 9 years ago
docker pull marlabsdocker/lesson1