Linux Bible. Christopher Negus

Чтение книги онлайн.

Читать онлайн книгу Linux Bible - Christopher Negus страница 60

Linux Bible - Christopher Negus

Скачать книгу

that name in the output of an echo command: $ find /etc -iname passwd -exec echo "I found {}" \; I found /etc/pam.d/passwd I found /etc/passwd

       The following command finds every file under the /usr/share directory that is more than 5MB in size. Then it lists the size of each file with the du command. The output of find is then sorted by size, from largest to smallest. With -exec entered, all entries found are processed, without prompting: $ find /usr/share -size +5M -exec du {} \; | sort -nr 116932 /usr/share/icons/HighContrast/icon-theme.cache 69048 /usr/share/icons/gnome/icon-theme.cache 20564 /usr/share/fonts/cjkuni-uming/uming.ttc

       The -ok option enables you to choose, one at a time, whether each file found is acted upon by the command you enter. For example, you want to find all files that belong to joe in the /var/allusers directory (and its subdirectories) and move them to the /tmp/joe directory: # find /var/allusers/ -user joe -ok mv {} /tmp/joe/ \; < mv … /var/allusers/dict.dat> ? y < mv … /var/allusers/five> ? y

      For more information on the find command, enter man find.

      Searching in files with grep

      If you want to search for files that contain a certain search term, you can use the grep command. With grep, you can search a single file or search a whole directory structure of files recursively.

      When you search, you can have every line containing the term printed on your screen (standard output) or just list the names of the files that contain the search term. By default, grep searches text in a case-sensitive way, although you can do case-insensitive searches as well.

      Instead of just searching files, you can also use grep to search standard output. So, if a command turns out lots of text and you want to find only lines that contain certain text, you can use grep to filter just want you want.

      Here are some examples of grep command lines used to find text strings in one or more files:

       $ grep desktop /etc/services desktop-dna 2763/tcp # Desktop DNA desktop-dna 2763/udp # Desktop DNA $ grep -i desktop /etc/services sco-dtmgr 617/tcp # SCO Desktop Administration Server sco-dtmgr 617/udp # SCO Desktop Administration Server airsync 2175/tcp # Microsoft Desktop AirSync Protocol …

      In the first example, a grep for the word desktop in the /etc/services file turned up two lines. Searching again, using the -i to be case-insensitive (as in the second example), there were 29 lines of text produced.

      To search for lines that don't contain a selected text string, use the -v option. In the following example, all lines from the /etc/services file are displayed except those containing the text tcp (case-insensitive):

       $ grep -vi tcp /etc/services

      To do recursive searches, use the -r option and a directory as an argument. The following example includes the -l option, which just lists files that include the search text, without showing the actual lines of text. That search turns up files that contain the text peerdns (case-insensitive).

       $ grep -rli peerdns /usr/share/doc/ /usr/share/doc/dnsmasq-2.66/setup.html /usr/share/doc/initscripts-9.49.17/sysconfig.txt …

       $ grep -ri --color root /etc/sysconfig/

      To search the output of a command for a term, you can pipe the output to the grep command. In this example, I know that IP addresses are listed on output lines from the ip command that include the string inet, so I use grep to display just those lines:

       $ ip addr show | grep inet inet 127.0.0.1/8 scope host lo inet 192.168.1.231/24 brd 192.168.1.255 scope global wlan0

      Being able to work with plain-text files is a critical skill for using Linux. Because so many configuration files and document files are in plain-text format, you need to become proficient with a text editor to use Linux effectively. Finding filenames and content in files are also critical skills. In this chapter, you learned to use the locate and find commands for finding files and grep for searching files.

      The next chapter covers a variety of ways to work with processes. There, you learn how to see what processes are running, run processes in the foreground and background, and change processes (send signals).

      Use these exercises to test your knowledge of using the vi (or vim) text editor, commands for finding files (locate and find), and commands for searching files (grep). These tasks assume that you are running a Fedora or Red Hat Enterprise Linux system (although some tasks work on other Linux systems as well). If you are stuck, solutions to the tasks are shown in Appendix B (although in Linux, there are often multiple ways to complete a task).

      1 Copy the /etc/services file to the /tmp directory. Open the /tmp/services file in vim, and search for the term WorldWideWeb. Change that to read World Wide Web.

      2 Find the following paragraph in your /tmp/services file (if it is not there, choose a different paragraph) and move it to the end of that file.# Note that it is presently the policy of IANA to assign a single well-known # port number for both TCP and UDP; hence, most entries here have two entries # even if the protocol doesn't support UDP operations. # Updated from RFC 1700, "Assigned Numbers" (October 1994). Not all ports # are included, only the more common ones.

      3 Using ex mode, search for every occurrence of the term tcp (case-sensitive) in your /tmp/services file and change it to WHATEVER.

      4 As a regular user, search the /etc directory for every file named passwd. Redirect error messages from your search to /dev/null.

      5 Create a directory in your home directory called TEST. Create files in that directory named one, two, and three that have full read/write/execute permissions on for everyone (user, group, and other). Construct a find command to find those files and any other files that have write permission open to ″others″ from your home directory and below.

      6 Find files under the /usr/share/doc directory that have not been modified in more than 300 days.

      7 Create a /tmp/FILES directory. Find all files under the /usr/share directory that are more than 5MB and less than 10MB and copy them to the /tmp/FILES directory.

      8 Find every file in the /tmp/FILES directory, and make a backup copy of each file in the same directory. Use each file's existing name, and just append .mybackup

Скачать книгу