How to Use the Command `peco` (with examples)
Peco is an interactive filtering tool that allows the user to search and select specific lines from generated lists or pipelines via a convenient text-based user interface. It is especially useful in scenarios where users deal with large outputs from commands and need to quickly find and extract specific information without manually scrolling through the entire list. By utilizing peco
, users can easily sift through output and choose the desired line items with minimal hassle.
Use case 1: Starting peco
on All Files in a Specified Directory
Code:
find path/to/directory -type f | peco
Motivation:
Imagine you’re working on a project that spans multiple files scattered across various subdirectories, and you’re trying to locate a specific file whose name you vaguely remember. Using the traditional find
command provides you with a cluttered list that can be cumbersome to navigate. By leveraging peco
, you can interactively filter and search through the list of files to quickly pinpoint the one you need, all without leaving the terminal.
Explanation:
find path/to/directory -type f
: This part of the command is used to search for all files (-type f
) within the specified directory path.find
recursively traverses the directory structure and outputs the paths of all matching files.| peco
: The pipe|
takes the output from thefind
command and feeds it intopeco
, which then allows you to interact with the output through its user interface. You can type to filter down the list of files interactively.
Example Output:
Upon execution, you would be greeted with a list resembling the following, instantly filtered as you interact:
.
..
./file1.txt
./subdir/file2.txt
./subdir/file3.doc
Use case 2: Starting peco
for Running Processes
Code:
ps aux | peco
Motivation:
On a multi-tasking system, there are typically numerous processes running at any given time. To manually sift through these running processes using the ps aux
command could be quite cumbersome and inefficient. By incorporating peco
, it becomes significantly easier to isolate and select specific processes interactively, enabling you to, for example, identify a process to terminate based on its name, user, or other attributes.
Explanation:
ps aux
: This part of the command lists all currently running processes. Thea
option allows listing of processes from all users,u
provides more detailed information including user names, andx
includes processes not connected to a terminal.| peco
: This connects the output of theps aux
command topeco
, thus enabling you to filter processes interactively by typing part of the process name or any other identifiable attribute.
Example Output:
Upon interacting with peco
, you might see:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 225024 7020 ? Ss Sep15 0:03 /sbin/init
user 123 0.3 1.0 371240 41056 ? Sl Sep15 2:34 /usr/bin/python3
Use case 3: Starting peco
with a Specified Query
Code:
peco --query "query"
Motivation:
In situations where you already have a query in mind and need to filter input or data quickly without manually typing it into peco
, the ability to start peco
with a pre-existing query enhances efficiency and speed. This is particularly useful for building scripts or automated tasks that may require pre-filtered interactive selection based on known criteria.
Explanation:
peco --query "query"
: This command startspeco
with a predefined query that acts as the initial filter. The--query
option sets the query to filter the given input, making it immediately ready for further refinement or selection.
Example Output:
For instance, if you have a log file and start peco
with the query “error”:
$ cat logfile.txt | peco --query "error"
You might see:
2023-01-12 12:32:01 ERROR Something went wrong in module.
2023-01-12 12:35:54 ERROR Failed to load resource.
Conclusion
Peco is a powerful tool that significantly improves the efficiency and user-friendliness of parsing and selecting data in terminal environments. Whether you’re searching for files, processes, or specific strings in logs and lists, peco
turns potentially overwhelming tasks into manageable, interactive experiences. Its ability to quickly filter through output makes it a valuable addition to any power user’s toolkit.