Use Linux find command with caution
当使用 Linux 下的 find 命令时,请使用 -ok 选项来避免文件被意外删除,这个选项会在移除任何文件之前都会请求你的许可。
最近有朋友提醒我有一个有用的选项来更加谨慎地运行 find 命令,它就是 -ok。除了一个重要的区别之外,它的工作方式与 -exec 相似,它使 find 命令在执行指定的操作之前请求权限。
这有一个例子。如果你使用 find 命令查找文件并删除它们,你可能使用的是下面的命令:
$ find . -name runme -exec rm {} \;
在当前目录及其子目录中中任何名为 “runme” 的文件都将被立即删除 —— 当然,你要有权限删除它们。改用 -ok 选项,你会看到类似这样的东西,但 find 命令将在删除文件之前会请求权限。回答 y 代表 “yes” 将允许 find 命令继续并逐个删除文件。
$ find . -name runme -ok rm {} \; ?
另一个可以用来修改 find 命令行为,并可能使其更可控的选项是 -execdir 。-exec 会运行指定的任何命令,而 -execdir 则从文件所在的目录运行指定的命令,而不是在运行find` 命令的目录运行指定的命令。这是两个它的例子:
$ pwd /home/shs $ find . -name runme -execdir pwd \; /home/shs/bin
$ find . -name runme -execdir ls \; ls rm runme
到现在为止还挺好。但要记住的是,-execdir 也会在匹配文件的目录中执行该命令。如果运行下面的命令,并且目录包含一个名为 “ls” 的文件,那么即使该文件没有执行权限,它也将运行该文件。使用 -exec 或 -execdir 类似于通过 source 来运行命令。
$ find . -name runme -execdir ls \; Running the /home/shs/bin/ls file
$ find . -name runme -execdir rm {} \; This is an imposter rm command
$ ls -l bin total 12 -r-x------ 1 shs shs 25 Oct 13 18:12 ls -rwxr-x--- 1 shs shs 36 Oct 13 18:29 rm -rw-rw-r-- 1 shs shs 28 Oct 13 18:55 runme
cat bin/ls echo Running the $0 file $ cat bin/rm echo This is an imposter rm command
要更谨慎,可以使用 -okdir 选项。类似 -ok,该选项将请求权限来运行该命令。
$ find . -name runme -okdir rm {} \; ?
你也可以小心地指定你想用的命令的完整路径,以避免像上面那样的冒牌命令出现的任何问题。
$ find . -name runme -execdir /bin/rm {} \;
find 命令除了默认打印之外还有很多选项,有些可以使你的文件搜索更精确,但谨慎一点总是好的。
The above is the detailed content of Use Linux find command with caution. For more information, please follow other related articles on the PHP Chinese website!

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res

Wine 10.0 stable version release: Running Windows applications on Linux to a higher level Wine, this open source and free application, allows Linux users to run Windows software and games on Unix/Linux operating systems, ushering in the release of the 10.0 stable version! This version has been provided with source code and binary package downloads, and supports various distributions such as Linux, Windows and Mac. This edition embodies a year of hard work and over 8,600 improvements, bringing many exciting improvements. Key highlights include: Enhanced support for Bluetooth devices. Improve support for HID input devices. Optimized performance of 32-bit and 64-bit applications.

This tutorial guides you through installing SQL Server 2022 on RHEL 8.x or 9.x, connecting via the sqlcmd command-line tool, database creation, and basic querying. Prerequisites Before beginning, ensure: A supported RHEL version (RHEL 8 or 9). Sudo

Mozilla Thunderbird 135: Powerful cross-platform mail client Mozilla Thunderbird is a free, open source, cross-platform email, calendar, news, chat and contact management client designed to efficiently handle multiple email accounts and news sources. On February 5, 2025, Mozilla released the Thunderbird 135 version, introducing a number of new features, performance improvements and security fixes. Thunderbird 135 main features: XZ Packaging for Linux Binaries: Smaller files, faster unpacking, and better integration with modern distributions. Cookie storage support: when creating space

This guide demonstrates how to protect files on Linux from accidental renaming or deletion using simple commands. We'll use the file important.txt in /home/user/ as an example. Method 1: Using chattr for Immutability The chattr command modifies fil


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
