Linux study session 3rd: File deletion prohibition setting and search function

1st Linux study session 1st: Virtual console and file operation 2nd Linux study session 2nd: File operation This is the third article following the first and second articles. On February 14, we held an in-house study session at Ancar Co., Ltd. It is a review of the study session topic

Please note that this article is the "Basics of Linux". If you have any additional information, please leave a comment: bow:

Each command is executed using the standard macOS terminal

Prologue

  1. [File deletion prohibition setting](# File deletion prohibition setting)
  2. [Find File](#Find File)
  3. [Other search functions](#Other search functions)

File deletion prohibition setting

To make a file undeleteable, the directory to which it belongs must be write-protected.

Write-protected file (wrt_no) in writable directory (mydir) Writable files (wrt_ok) in write-protected directory (bandir)

Now, let's actually write-protect it and check the permissions with the ls command.

# wrt_Write-protect no
$ chmod -w mydir/wrt_no

#bandir Dirctry write-protected
$ chmod -w bandir

#bandir directory is write-protected
$ ls -ld *dir
drwxrwxr-x 2 keita ancar 20 February 14 17:39 mydir
dr-xr-xr-x 2 keita ancar 20 February 14 17:39 bandir

You can now confirm that it is write-protected.

# wrt_no File is write-protected
$ ls -l mydir/w* bandir/w*
-r--r--r--1 keita ancar 7 February 14 17:39 mydir/wrt_no
-rw-rw-r--1 keita ancar 7 February 14 17:39 bandir/wrt_ok

Let's check if it can actually be deleted. bandir / wrt_ok can no longer be deleted, and mydir / wrt_no can be deleted with confirmation. I see. .. .. If you can't delete a file, you also need to check the permissions of the directory to which the file belongs.

$ ls -ld *dir
dr-xr-xr-x 2 keita ancar 20 February 14 17:42 bandir
drwxr-xr-x 2 keita ancar 20 February 14 17:43 mydir

$ rm bandir/wrt_ok
rm: `bandir/wrt_ok'Cannot be deleted: Permission denied

$ rm mydir/wrt_no
rm:Write-protected file Normal empty file`mydir/wrt_no'Do you want to delete?y

Find the file

I learned about the find command. I found that the result is not displayed unless the directory is specified correctly as in (2). I want to be careful about this. In the example below, chmod name search -name is performed based on/ bin /, and it is displayed on the screen -print.

$ find /bin -name chmod  -print  ----①
$ find /bin/ -name chmod -print  ----②
/bin/chmod

$ find dr1 -name myfile
dr1/dr1b/myfile
dr1/dr1a/myfile
dr1/myfile

Next is a search by specifying a wildcard. Don't forget to enclose it in "" when specifying wildcards! If you do not enclose it in double quotes, it will be shell-expanded and you will not get the correct output.

$ find /bin/ -name "ch*"
/bin/chacl
/bin/chgrp
/bin/chcon
/bin/chmod
/bin/chown
/bin/chrt
/bin/chardetect
/bin/chmem
/bin/chattr
/bin/chage
/bin/chvt
/bin/chronyc

Next is the case of searching by the date when the file data was updated. Specify the date as a number. Add + to "day before that" If you add -, it will be" a day closer to the present ". It's convenient ~ In the example below, the search will be for files created / updated within 4 days (3 days ago, 2 days ago, 1 day ago, today, 4 days in total).

$ find ~/ -mtime -4
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/.ssh
/root/.ssh/authorized_keys
/root/aa1.txt
/root/aa2.txt
/root/aa4.txt
/root/hoge.txt
/root/hogehoge.php
/root/hoge.rb
/root/rfile2

Below is a search for files created / updated 4 days ago.

$ find ~/ -mtime 4
/root/
/root/rfile3

Other search functions

locate string A search command that displays files that contain the specified string. The locate command only displays the file name.

$ locate mkdir
/usr/bin/mkdir
/usr/lib/python2.7/site-packages/lockfile/mkdirlockfile.py
/usr/lib/python2.7/site-packages/lockfile/mkdirlockfile.pyc
/usr/lib/python2.7/site-packages/lockfile/mkdirlockfile.pyo
/usr/share/man/man1/mkdir.1.gz
/usr/share/man/man1p/mkdir.1p.gz
/usr/share/man/man2/mkdir.2.gz
/usr/share/man/man2/mkdirat.2.gz
/usr/share/man/man3p/mkdir.3p.gz
/usr/share/man/overrides/cs/man1/mkdir.1
/usr/share/man/overrides/es/man1/mkdir.1
/usr/share/man/overrides/fr/man1/mkdir.1
/usr/share/man/overrides/pl/man1/mkdir.1
/usr/share/man/overrides/zh_CN/man1/mkdir.1

That's all for this time!

Summary

Although it is a basic content, I was able to learn how to use commands that I do not normally use. If I have a chance, I would like to use it more and more.

Any corrections or corrections are welcome, so please leave a comment!

Recommended Posts

Linux study session 3rd: File deletion prohibition setting and search function
Linux Study Session 1st: Virtual Console and File Manipulation
Linux study session 2nd: File operation
[Linux] File search
Device and Linux file system
Linux file and directory permissions