Command problem sets (iii)

Source: Internet
Author: User

1. Find files for a time period


In a directory, there are many files, just want to find the December 1 to December 31 documents, how to find?

First manually found in this time period of the earliest files, such as 1.txt is December 1 0:1 created, and then find this time period of the latest files, such as 2.txt was created December 31 23:55;

Find. -type f-newer 1.txt! -newer 2.txt


Do not go to manually find, refer to the following:

In fact, the creation date (mtime) of the modified file. Touch has this feature.

Example create a new file for October 8, 2012

Touch-m-T 1210080000 1.txt


Then stat 1.txt

found that except for modify's act. October 8, 2012 0:0, the other two items are the current date and time


It's OK

Touch-c-T 1210080000 2.txt

Stat 2.txt

Access and Modify are October 8, 2012 0 O'Clock 0, change is the current date and time



2. Create a file of the specified date


In fact, the creation date (mtime) of the modified file. Touch has this feature.
Example create a new file for October 8, 2012
Touch-m-T 1210080000 1.txt

Then stat 1.txt
found that except for modify's act. October 8, 2012 0:0, the other two items are the current date and time

It's OK
Touch-c-T 1210080000 2.txt
Stat 2.txt
Access and Modify are October 8, 2012 0 O'Clock 0, change is the current date and time


3. Use Find to exclude certain directories or files



1. Exclude only one directory or file, such as find all the files in the/tmp/directory (not including the directory), and do not include the directory 123

find/tmp/-path "/tmp/123"-prune-o-type f-print


2. Exclude two or more directories or files, such as find all files in the/tmp/directory (not including directories), and do not contain directory 123 and directory 234 and directory 345

find/tmp/\ (-path "/tmp/123"-o-path "/tmp/234"-o-path "/tmp/345" \)-prune-o-type f-print


Note that if you are looking for a directory, the directory name after-path must not take/as written-path "/tmp/123/" is wrong, and when looking for files, with/without problems.




4. Add a custom path to your Linux


Install MySQL under the/usr/local/mysql. If we were to use the MySQL command, we would have to hit the absolute path/usr/local/mysql/bin/mysql.

Then add the/usr/local/mysql/bin to the path variable.

Vi/etc/profile

On the last side, add

Export path= $PATH:/usr/local/mysql/bin

After the report is saved, run the command

Source/etc/profile

Check the PATH variable

Echo $PATH

This allows you to enter MySQL directly.



5. Special permissions for files under Linux s and t



Let's take a look at the permissions of these two files:

[Email protected] ~]# ls-ld/usr/bin/passwd/tmp

DRWXRWXRWT 4 root root 4096 June 2 17:33/tmp

-rwsr-xr-x 1 root root 22984 Jan 7 2007/usr/bin/passwd


The S and T here are for execution permission.

This s permission is intended to allow the general user to temporarily have execute permissions for the primary/group to which the file belongs. For example,/usr/bin/passwd in the implementation of it needs to modify/etc/passwd and/etc/shadow files, these files in addition to root, other users do not have write permission, but in order to allow ordinary users to modify their own password, They are only allowed to have root permissions temporarily. So this s permission is used to accomplish this particular task. The S permission can only be applied to the binary executable file.

If you don't want ordinary users to change their passwords, just

[Email protected] ~]# chmod u-s/usr/bin/passwd or

[Email protected] ~]# chmod 0755/usr/bin/passwd

0755 The first 0 indicates that no special permissions are used, the digits on the bit can be 0,1 (--t), 2 (-s-), 3 (-st), 4 (s--), 5 (S-T), 6 (ss-), 7 (SST)

The T permission is only valid for the directory, which means that only the owning master and root can delete (rename/move) files in that directory. For example, the/tmp directory is any user can read and write, if others can arbitrarily delete (rename/move) their own files, it is not very dangerous. So this T-permission is to solve this problem. Here is an example of how to use this permission:

[Email protected] ~]# cd/tmp/

[Email protected] tmp]# mkdir test

[Email protected] tmp]# chmod 1777 test

[Email protected] tmp]# LS-LD test

DRWXRWXRWT 2 root root 4096 June 2 18:10 test

[email protected] tmp]# su test1

[email protected] tmp]$ Touch test/1.txt

[Email protected] tmp]$ ls-l test

Total 4

-rw-r--r--1 test1 Test 0 June 2 18:12 1.txt

[[Email protected] tmp]$ exit

[email protected] tmp]# su www

[Email protected] tmp]$ ls-l test/1.txt

-RWXRWXRWX 1 test1 Test 6 June 2 18:12 Test/1.txt

[Email protected] tmp]$ RM test/1.txt

Rm:cannot remove ' test/1.txt ': Operation not permitted

Prompt cannot delete 1.txt

[[Email protected] tmp]$ exit

[Email protected] tmp]# chmod-t test

Remove the T permission.

[Email protected] tmp]# LS-LD test

DRWXRWXRWX 2 root root 4096 June 2 18:13 test

[email protected] tmp]# su www

[Email protected] tmp]$ rm-f test/1.txt

Deleted again, the deletion succeeds.

[[email protected] tmp]$ ls test/1.txt

Ls:test/1.txt:no such file or directory




6. Locate Command Error


Docate/bin/ls

Warning:locate:could not open database:/var/lib/slocate/slocate.db:no such file or directory

Warning:you need to run, the ' updatedb ' command (as root) to create the database.

Please be at/etc/updatedb.conf to enable the daily cron job.



This is a hint to us that the system is not currently creating a slocate database.

Workaround: Enter the command updatedb create the database

Then you need to vim/etc/updatedb.conf

Let Daily_update=yes


7. Find-name Find multiple Files


Find/-name 1.txt-o-name 2.txt-o-name 3.txt ...


8. Find files with the specified permissions



This uses the-perm option, as follows:


1.-perm Mode (example:-perm 775)

Explanation: 775 There is no line in front of the representative as long as you find exactly the same permissions.

Like saying a file is-rwxrwxr-x (775) is the match of 100%

But the other file is-rwxrwxrwx (777) is not 100% match,others more Write permission is not 100%, so not match!


2.-perm-mode (ex:-perm-775)

Explanation: 775 The front line indicates that as long as the indicated permission match, the other does not matter, so the top of the second file (777) in this case will match.


About +-in-perm in the Find command

For example, for example, the current directory has a B c D4 files

A file has a permission of 6000 that is a file with only suid Sgid special permissions

The permissions for file B are 2000, that is, the B file has only Sgid special permissions.

C file has permissions of 4000 that is, C files have only suid special permissions

d file has permissions of 6600 that is, d files have special permissions for Sgid suid and the file owner has read and write access to the file.

So now find. -type F-perm 6000 can find a file because the a file has permission of 6000

So now find. -type f-perm-6000 can find two files of a D, this is because:

We can first convert the permissions of the 4 files of a B c D to 2 binary then,

A permission is converted to 2 and 110 000 000 000

b 010 000 000 000 after conversion to 2 binary

C 100 000 000 000 after conversion to 2 binary

D 110 110 000 000 after switching to 2 binary

In Find. -type f-perm-6000 in the 6000 permissions to 2 to 110 000 000 000, then 6000 before the-number represents the indispensable, that is, if 1 of the place must have 1, then here to find-6000 permissions of the file, which 6000 permissions in front of 2 positions are 1, So here find Find-6000 permissions of the file is to find the first 2 locations are 1 of the files. And only a d of these two files are 2 in the top 1 positions, so find. -type f-perm-6000 will only find a D two files.


Find. -type F-perm +6000 will find a B c D of these 4 files because:

+6000 in the + sign for 1 can be, that is, 1 of the position, any position as long as 1 can be. So here to find +6000 permissions of the file, which 6000 permissions in front of 2 locations have 1, so here find Find + 6000 permissions of the file is to find the first 2 locations as long as there is a location 1 of the files can be, the 4 files are in line with the requirements so the final can be found. -type f-perm +6000 found


When we use man to query the help document for find, we find that-perm also has a/modle format, which is actually equivalent to +modle, which suggests that we use/not use + because the + is prone to confusion. So later you see find-perm/modle such a format, it is considered as a +modle can.




Command problem sets (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.