この記事では、さまざまなツールを使用してLinuxでファイルを見つける方法を学びます。
Linuxで最も人気のあるファイル検索ツール
The “find” command cheatsheet
ファイル名で検索
“Find” is one of the most popular tools to search files in Linux. When you know the file name you can search for it using command below. Replace “/home” with a folder to search through and “filename.txt” with the file name.
$ find /home -name filename.txt
To search the current folder for a file replace “/home” with a dot (.) symbol:
$ find . -name Documents.zip ./Documents.zip
When you do not know the exact file name use the regular expression to look for a match. The asterisk (*) symbol matches any character set after the “Documents” keyword.
$ find /home -name "Documents*" /home/eugene/Documents.zip /home/eugene/Documents /home/eugene/Phone/Documents
変更日で検索
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
例えば、10日前に変更された画像を見つけてみましょう:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
ファイルタイプで検索
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
ディレクトリを検索: 指定されたパス内のすべてのディレクトリを見つけるには、-type dオプションを使用します。
$ find /home -type d
通常のファイルを検索: 逆に、通常のファイルを見つけるには、-type fオプションを使用します。
$ find /home -type f -name "*.txt"
シンボリックリンクを検索: シンボリックリンクを検索するには、-type lオプションを使用します。
$ find /home -type l -name "linkname"
特定のファイル拡張子を検索: ファイル拡張子を指定することで検索を絞り込みます。
$ find /home -type f -name "*.jpg"
条件の組み合わせ
You can combine multiple criteria to make your searches more precise. Here’s an example:
特定の拡張子を持つ変更されたファイルを検索: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
日付範囲内で変更されたファイルを検索:2023年11月1日から2023年11月5日までの間に変更されたファイルを特定します。
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
大文字と小文字を無視する: 検索を大文字と小文字を区別しないようにしたい場合は、-inameオプションを使用します。
$ find /home -iname "document*"
検索の深さを制限する: -maxdepthオプションを使用して、検索の深さを特定のレベルに制限します。
$ find /home -maxdepth 2 -name "*.txt"
特定のディレクトリを除外する: -notまたは!オプションを使用して、検索から特定のディレクトリを除外します。
$ find /home -type f -name "*.txt" -not -path "/home/user/exclude/*"
Remember, mastering the “find” command can significantly enhance your ability to navigate and manage files in a Linux environment. Experiment with different options to tailor your searches according to specific criteria.
Search by file content using “grep” command
When it comes to finding specific content within files, the “grep” command becomes your go-to tool. It allows you to search for patterns or text strings within one or multiple files. Here’s a quick cheatsheet to get you started:
基本的な内容検索: ファイル内の特定の文字列を検索するには、次の構文を使用します
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
複数のファイルを検索する: 複数のファイルを検索したい場合は、ワイルドカードまたは特定のファイルパターンを指定します
$ grep "pattern" /path/to/files/*.txt
大文字と小文字を区別しない検索: -iオプションを使用して検索を大文字と小文字を区別しないようにします
$ grep -i "pattern" filename
行番号を表示: パターンが見つかった行番号を知りたい場合は、-nオプションを使用します
$ grep -n "pattern" filename
ファイル名のみ表示: パターンを含むファイルの名前のみを表示するには、-lオプションを使用します
$ grep -l "pattern" /path/to/files/*.txt
再帰的に検索: ディレクトリ内およびそのサブディレクトリ内のすべてのファイルでパターンを検索したい場合は、-rオプションを使用します
$ grep -r "pattern" /path/to/directory
ファイルまたはディレクトリを除外: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
単語全体を検索: 部分一致を防ぐために、単語全体を検索するには-wオプションを使用します
$ grep -w "word" filename
Advanced “grep” Usage
反転マッチの検索: -vオプションを使用して、指定されたパターンを含まない行を表示するためにマッチを反転させます
$ grep -v "pattern" filename
マッチのカウント: パターンを含む行がいくつあるかを知りたい場合は、-cオプションを使用します:
$ grep -c "pattern" filename
一致するテキストのみを表示-oオプションを使用して、パターンに一致するテキストのみを表示します:
$ grep -o "pattern" filename
行番号付きの再帰検索: 包括的な検索のためのオプションを組み合わせます。例えば:
$ grep -r -n "pattern" /path/to/directory
Mastering the “grep” command allows you to swiftly locate specific content within files, making it an indispensable tool for efficient file exploration in a Linux environment. Experiment with different options to tailor your searches and uncover the information you need.
代替ファイル検索方法
In addition to the powerful “find” and “grep” commands, there are alternative methods for searching files in a Linux environment. Let’s explore a few:
Pythonスクリプトを使用してファイルを検索する
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os そして fnmatch modules:
import os
from fnmatch import fnmatch
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch(file, pattern):
print(os.path.join(root, file))
# Usage
find_files('/path/to/search', '*.txt')
This script walks through the directory and its subdirectories, matching files based on the specified pattern. Customize the find_files function to suit your specific search criteria.
The “locate” Command
The “locate” command is another efficient way to find files on a Linux system. It uses a pre-built index, making searches faster compared to the “find” command. However, keep in mind that the index needs regular updates to include recent changes.
ロケートデータベースを更新する:
$ sudo updatedb
ファイルを検索する:
$ locate filename.txt
Using the “which” Command
If you’re looking for the location of an executable in your system’s PATH, the “which” command can help.
実行可能ファイルの場所を見つける:
$ which executable_name
Using “fd” – A Fast and User-friendly Alternative to “find”
The “fd” command is a fast and user-friendly alternative to “find.” It comes with a simplified syntax and colorized output.
fdをインストールする:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
ファイルを検索する:
$ fdfind Documents Phone/Documents Documents Documents.zip
カスタムスクリプトとエイリアス
最後に、ファイル検索プロセスを効率化するためにカスタムシェルスクリプトやエイリアスを作成できます。例えば:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
カスタムスクリプトとエイリアスを使用すると、特定のファイル検索ニーズに対するショートカットを作成でき、作業効率が向上します。
Bashスクリプトを使用してLinuxでファイルを検索する
Bashスクリプトを作成して使用することで、Linuxでの繰り返しのファイル検索タスクを自動化できます。以下は、提供されたパターンに基づいてファイルを検索するシンプルなBashスクリプトの例です:
#!/bin/bash # Bash script to find files based on a pattern if [ $# -ne 2 ]; then echo "Usage: $0 <directory> <pattern>" exit 1 fi directory=$1 pattern=$2 # Check if the directory exists if [ ! -d "$directory" ]; then echo "Error: Directory $directory not found." exit 1 fi # Use find command to search for files find "$directory" -name "$pattern" -print
このスクリプトをファイルに保存します。例えば、find_files.shという名前で保存し、次のコマンドで実行可能にします:
$ chmod +x find_files.sh
これで、ディレクトリとパターンを引数として指定することで、スクリプトを使用してファイルを検索できます:
$ ./find_files.sh /path/to/search "*.txt"
このスクリプトは、正しい数の引数が提供されているか、指定されたディレクトリが存在するかを確認します。その後、findコマンドを使用して、指定されたパターンに基づいてファイルを検索します。
ボーナス:Linuxで画像を検索する方法
Linuxには、画像検索のためのいくつかのツールと方法があり、それぞれ独自の機能と能力を提供しています。以下はいくつかのオプションです:
“find” Command with File Type Filtering:
The standard “find” command can be used to locate image files based on their file types. For example, to find all JPEG files in a directory and its subdirectories:
$ find /path/to/images -type f -name "*.jpg"
The “fdfind” Command:
The “fdfind” command, a fast and user-friendly alternative to “find,” can also be used to search for image files. Install it if you haven’t already:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
特定の拡張子を持つ画像を検索する:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
最近の変更のためにlocateデータベースを定期的に更新することを忘れないでください:
$ sudo updatedb
“grep” Command for Specific Image Names:
If you have a specific naming convention for your image files, you can use the “grep” command to search for them:
$ grep -r 'pattern' /path/to/images
Replace ‘pattern’ with a part of the image file name.
“file” Command
The “file” command can identify file types, helping you filter out image files:
$ file /path/to/images/*
Look for lines that include “image” to identify image files.
“feh” Image Viewer with Filelist
If you have a list of image files and want to view them, the “feh” image viewer allows you to create a filelist:
$ feh -f $(find /path/to/images -type f -name "*.jpg")
これにより、指定されたディレクトリ内のすべてのJPEGファイルのファイルリストを持つ画像ビューアが開きます。
画像メタデータツールの使用
If your images have metadata, you can use tools like “exiftool” to search based on image properties:
$ exiftool -filename -r /path/to/images | grep 'search_term'
グラフィカルファイルマネージャ
Nautilus、Dolphin、Thunarのようなグラフィカルファイルマネージャは、検索機能を提供することがよくあります。ディレクトリに移動して、検索バーを使用できます。
タグベースの画像整理ツール
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
概要
In conclusion, mastering file search on Linux involves understanding and combining various commands, tools, and scripting techniques. Whether you’re a command-line enthusiast or prefer graphical interfaces, this guide equips you with the knowledge to navigate and search for files seamlessly in a Linux environment. Happy file hunting!





