830 Part VII: Appendixes tail Syntax: tail [-count | -fr] filename The tail command displays the end of a file. By default, tail displays the last 10 lines of a file. To display the last 50 lines of the file doc.txt, you issue the following command: tail -50 doc.txt The -r option displays the output in reverse order. By default, -r displays all lines in the file, not just 10 lines. For instance, use the following to display the entire contents of the file doc.txt in reverse order: tail -r doc.txt To display the last 10 lines of the file doc.txt in reverse order, use this: tail -10 -r doc.txt Finally, the -f option is useful when you are monitoring a file. With this option, tail waits for new data to be written to the file by some other program. As new data is added to the file by some other program, tail displays the data on the screen. To stop tail from monitoring a file, press Ctrl+C (the intr key) because the tail command does not stop on its own. touch Syntax: touch file or directory This command updates the timestamp of a file or directory. If the named file does not exist, this command creates it as an empty file. uniq Syntax: uniq [-c] filename The uniq command compares adjacent lines and displays only one unique line. When used with the -c option, uniq counts the number of occurrences. For example, a file with the following contents:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
Appendix D: Linux Primer 829 To save the sorted results, use the -o option: sort -o sorted.txt a.txt saves the sorted a.txt file in sorted.txt. To use sort to merge existing sorted files and to save the output in sorted.txt, use sort -o sorted.txt a.txt b.txt c.txt The -r option for this command reverses the sort order. Therefore, a file that contains the letters of the alphabet on a line is sorted from z to a if you use the -r option. The -d option sorts files based on dictionary order. The sort command considers only letters, numerals, and spaces, ignoring other characters. The -u option looks for identical lines and suppresses all but one. Therefore, sort produces only unique lines. stat Syntax: stat file This program displays various statistics on a file or directory, as shown in the following example: stat foo.txt This command displays the following output: File: foo.txt Size: 4447232 Filetype: Regular File Mode: (0644/-rw-r–r–) Uid: ( 0/root) Gid: (0/root) Device: 3,0 Inode: 16332 Links: 1 Access: Fri Aug 2 21:39:43 2002(00000.02:32:30) Modify: Fri Aug 2 22:14:26 2002(00000.01:57:47) Change: Fri Aug 2 22:14:26 2002(00000.01:57:47 You can see the following displayed: file access; modification; change date; size; owner and group information; permission mode; and so on. strings Syntax: strings filename The strings command prints character sequences at least four characters long. You use this utility mainly to describe the contents of nontext files.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Linux Web Hosting services
828 Part VII: Appendixes The -i option gives you a parachute. It s up to you to either pull the cord (answer no) or suffer the consequences (answer yes). The -f option is completely the opposite. The -f (force) option tells rm to remove all the files you specify, regardless of the file permissions. Use the -f option only when you are 100 percent sure that you are removing the correct file(s). To remove a directory and all files and subdirectories within it, use the -r option. rm -r will remove an entire subtree, as shown here: rm -r documents If you are not sure what you are doing, combine the -r option with the -i option: rm -ri documents The preceding example asks for your permission before it removes every file and directory. sort Syntax: sort [-rndu] [-o outfile] [infile/sortedfile] The obvious task this command performs is to sort. However, sort also merges files. The sort command reads files that contain previously sorted data and merges them into one large, sorted file. The simplest way to use sort is to sort a single file and display the results on your screen. As an example, suppose that a.txt contains the following: b c a d To sort a.txt and display the results to the screen, use the following: sort a.txt a b c d
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
Appendix D: Linux Primer 827 pwd Syntax: pwd This command prints the current working directory. The directories displayed are the absolute path. None of the directories displayed are hard or soft symbolic links. pwd /home/usr/charmaine rm Syntax: rm [-rif] directory/file To remove a file or directory, use the rm command, as shown in the following examples: rm doc.txt rm ~/doc.txt rm /tmp/foobar.txt To remove multiple files with rm, you can use wildcards or type each file individually. For example, rm doc1.txt doc2.txt doc3.txt is equivalent to rm doc[1-3].txt rm is a powerful command that can cause chaos if you use it incorrectly. For instance, suppose that you have been working on your thesis for the last six months. You decide to rm all of your docs, thinking you are in another directory. After finding out that a backup file does not exist (and you are no longer in denial), you wonder whether there was any way you could have prevented this. The rm command has the -i option, which allows rm to be interactive. This tells rm to ask your permission before removing each file: rm -i *.doc rm: remove thesis.doc (yes/no)? n
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Unix Web Hosting services
826 Part VII: Appendixes option, a long listing containing file/directory permission information, size, modification date, and so on, are displayed. The -a option allows you to view all files and directories (including the ones that have a leading period in their names) within the current directory. The -R option allows the command to recursively display contents of the subdirectories (if any). mkdir Syntax: mkdir directory . . . To make a directory, use the mkdir command. You have only two restrictions when choosing a directory name: (1) File names can be up to 255 characters long, and (2) directory names can contain any character except the slash (/). For example, mkdir dir1 dir2 dir3 The preceding example creates three subdirectories in the current directory. mv Syntax: mv [-if] sourcefile targetfile Use the mv command to move or rename directories and files. The command performs a move or rename depending on whether the targetfile is an existing directory. To illustrate, suppose you would like to give a directory called foo the new name of foobar: mv foo foobar Because foobar does not already exist as a directory, foo becomes foobar. If you issue the following command: mv doc.txt foobar and foobar is an existing directory, you perform a move. The file doc.txt now resides in the directory foobar. The -f option removes existing destination files and never prompts the user. The #NAME? the response does not begin with y or Y, the file is skipped.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
Appendix D: Linux Primer 825 You cannot remove a file that has hard links from the hard disk until you remove all links. You create hard links without the -s option: ln ./www ./public_html A hard link does have limitations, however. A hard link cannot link to another directory, and a hard link cannot link to a file on another file system. Using the -s option, you can create a soft link, which eliminates these restrictions: ln -s /dev/fs02/jack/www /dev/fs01/foo/public_html Here you create a soft link between the directory www on file system 2 and a newly created file public_html on file system 1. locate Syntax: locate keyword The locate command finds the path of a particular file or command if updated script was run at an earlier time using cron job or manually. locate finds an exact or substring match. For example: locate foo /usr/lib/texmf/tex/latex/misc/footnpag.sty /usr/share/automake/footer.am /usr/share/games/fortunes/food /usr/share/games/fortunes/food.dat /usr/share/gimp/patterns/moonfoot.pat The output that locate produces contains the keyword foo in the absolute path or does not have any output. ls Syntax: ls [-1aRl] file or directory The ls command allows you to list files (and subdirectories) in a directory. It is one of the most popular programs. When you use it with the -1 option, it displays only the file and directory names in the current directory. When you use the -l
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Unix Web Hosting services
824 Part VII: Appendixes The preceding command finds all text files (ending with a .txt extension) in the current directory, including all its subdirectories. find . -type f -name *.txt -exec grep -l magic {} ; The preceding command searches all text files (ending with the .txt extension) in the current directory, including all its subdirectories for the keyword magic, and returns their names (because -l is used with grep): find . -name ?*.gif? -atime -1 -exec ls -l {} ; The preceding command finds all GIF files that have been accessed in the past 24 hours (one day) and displays their details using the ls -l command. find . -type f -empty The preceding command displays all empty files in the current directory hierarchy. head Syntax: head [-count | -n number] filename This command displays the first few lines of a file. By default, it displays the first 10 lines of a file. However, you can use the preceding options to specify a different number of lines, as follows: head -2 doc.txt # Outline of future projects # Last modified: 02/02/99 The preceding example illustrates how to view the first two lines of the text file doc.txt. ln Syntax: ln [-s] sourcefile target ln creates two types of links: hard and soft. Think of a link as two names for the same file. Once you create a link, you cannot distinguish it from the original file.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Unix Web Hosting services
Appendix D: Linux Primer 823 an existing directory, cp copies the source file into the directory. The -r parameter recursively copies a directory. cp main.c main.c.bak The preceding example copies the existing file main.c and creates a new file called main.c.bak in the same directory. These two files are identical, bit for bit. grep Syntax: grep [-viw] pattern file(s) The grep command enables you to search for one or more files for particular character patterns. Every line of each file that contains the pattern is displayed at the terminal. The grep command is useful when you have numerous files and you want to find out which ones contain certain words or phrases. Using the -v option, you can display the inverse of a pattern. Perhaps you want to select the lines in data.txt that do not contain the word the: grep -vw the data.txt If you do not specify the -w option, any word containing the matches, such as toge[the]r. The -w option specifies that the pattern must be a whole word. Finally, the -i option ignores the difference between uppercase and lowercase letters when searching for the pattern. Much of the flexibility of grep comes from the fact that you can specify not only exact characters but also a more general search pattern. To do this, you use what are described as regular expressions. find Syntax: find [path] [-type fdl] [-name pattern] [-atime [+-]number of days] [-exec command {} ;] [-empty] The find command finds files and directories, as shown in the following example: find . -type d The find command returns all subdirectory names under the current directory. The -type option is typically set to d (for directory), f (for file), or l (for links): find . -type f -name *.txt
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Unix Web Hosting services
822 Part VII: Appendixes chown Syntax: chown [ -fhR ] Owner [ :Group ] { File . . . | Directory. . . } The chown command changes the owner of a file or directory. The value of the Owner parameter can be a user ID or a login name in the /etc/passwd file. Optionally, you also can specify a group. The value of the Group parameter can be a group ID or a group name in the /etc/group file. Only the root user can change the owner of a file. You can change the group of a file only if you are a root user or you own the file. If you own the file but are not a root user, you can change the group only to a group of which you are a member. Table D-3 describes the chown options. TABLE D-3 CHOWN OPTIONS Option Description #NAME? #NAME? the file or directory to which the symbolic link points. -R Descends directories recursively, changing the ownership for each file. When a symbolic link is encountered and the link points to a directory, the ownership of that directory is changed, but the directory is not further traversed. The following example changes the owner of the file to another user: chown bert hisfile.txt cp Syntax: cp [-r] source destination Use the cp command to make an exact copy of a file. The cp command requires at least two arguments. The first argument is the file you want to copy, and the second argument is the location or file name of the new file. If the second argument is
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Unix Web Hosting services
Appendix D: Linux Primer 821 For example, the following are equivalent: upload_max_filesize = 2M upload_max_filesize = 2097152 upload_tmp_dir The load_tmp_dir directive defines the temporary directory location for files uploaded via PHP. It is customary to set this to /tmp on UNIX systems; on Windows systems, this is typically set to /temp or left alone, in which case, PHP uses the system default. Syntax: load_tmp_dir directory Common File/Directory Commands This section describes a few commonly used Linux file and directory commands. chmod Syntax: chmod [-R] permission-mode file or directory Use this command to change the permission mode of a file or directory. The permission mode is specified as a three- or four-digit octal number. For example: chmod 755 myscript.pl The preceding command changes the permission of myscript.pl script to 755 (rwxr-xr-x), which allows the file owner to read, write, and execute, and allows only read and execute privileges for everyone else. Here is another example: chmod -R 744 public_html The preceding command changes the permissions of the public_html directory and all its contents (files and subdirectories) to 744 (rwxr-r-), which is a typical permission setting for the personal Web directories you access using http://server/~username URLs under Apache Server. The -R option tells chmod to recursively change permissions for all files and directories under the named directory.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services