· learnings · 4 min read
Linux学习日志 Ⅶ
这两个链接里面是详细介绍find, grep以及其他seach功能的。
下面都是一些关于search的例子。
Find
Ex.
return a list of all files that are larger than 80000 bytes;
find –size +80000c
return a list of all files of type directory;
find –type d
return a list of all files that were modified in the last 48 hours;
find –mtime -2
return a list of all files that are less than 10 bytes;
find –size -10c
return a list of all files in the /proc/net directory which are empty;
find /proc/net -empty
return a list of all files in the /etc directory which have the extension “conf”;
find /etc –name *.conf
return a list of all files that contain the string “file” anywhere in the file name, and were last accessed at least 3 minutes ago.
Find –name *file* -amin -3
Use the “find” command to list all files in the /sbin directory that have the “SetUID” bit on (ignoring all other permissions).
Find /sbin –perm –u=s
As the root user, use a single “find” command line to locate all the files on the system with a filename ending in the extension “conf”, and simultaneously display the number of lines in each of those files.
find / -name *.conf -exec wc -l {} /;
Use a “sort” command line to display a sorted list of the entries in the /etc/passwd file in ascending numeric order of group ID.
sort /etc/passwd -g -t ”:” -k 4
(where -k indicates field, -t indicates field seperators, -g general numeric sort, -n numeric sort, -r to reverse the order of sort.)
Use a single “find” command line that finds all files in the /etc directory with a filename starting with the string “issue”, and deletes the read permission for the owning group of these files.
find /etc -name issue* -exec chmod g= {} /;
As a normal user, use a “find” command line which displays all files on the system with the string “xinetd” in the filename.
find /etc -name *xinetd*
As a normal user, use a single “find” command line which displays all files on the system with the string “xinetd” in the filename, and which directs any output errors to the pseudo device, null.
find /etc -name *xinetd* 2> /dev/null
(1> will specify stdout to whatever is behind, in this case, null device. 2> will specify stderr (error msg) to null, and &> will specify all output to null)
Write down the single “find” command line to locate all regular files in the /sbin directory that are owned by the user root and have either the SetUID or the SetGID permission set (ignoring all other permissions), and then changes the owning group of each of these files to the group nobody. Assume that the root user would be executing it.
find /sbin -perm 6000 -type f -user root -exec chgrp nobody {} /;
Write down the single “find” command line to locate all regular files in the /home/sharedproject directory that are owned by the user “jbrown” and have been modified in the last 3 days, and then prints each of these files to the queue “lasser4”. Assume that the user “jbrown” would be executing it.
Find /home/sharedproject –user jbrown –mtime -3 –exec lpr –P laser 4 {}/;
grep
Ex.
return all lines in your “prac1file” file containing the string “ls”;
grep –F “ls” prac1file
return all lines in your “prac1file” file containing the string “ls”, together with the line below these lines;
grep –F “ls” prac1file –A1
return all lines in your “prac1file” file containing the word “whatis” and the line number of these lines.
grep – F “whatis” –n prac1file
Use the “grep” command to list the entries for all the users on the system who use “bash” as their default shell. Recall that this information is stored in the “/etc/passwd” file.
grep –w /bin/bash /etc/passwd