Skip to content

Xargs & Searching File System

xargs acts as a bridge. Many Linux commands (like rm, mkdir, or touch) don’t know how to read a list of names from a “pipe” (|). xargs takes those names and feeds them to the command one by one.

  • The Default: If you don’t give it a command to run, it defaults to echo (it just prints the items).
  • The Transformation: It can turn a long list of items spread over many lines into a single line of text.
  • i (Replace String): This lets you define a “placeholder” (usually {}) for where the input should go in your next command.
  • 0 (Null Terminator): Critical for files with spaces in their names. It tells the system to treat the whole filename as one item instead of breaking it apart at the space.
  • P (Parallel): If you have 1,000 files to process, P 4 tells the computer to use 4 CPU cores at once to finish the job faster.
  • Turning Lists into Files: if you have a file projects.txt with the names Alpha, Beta, and Gamma:

cat projects.txt | xargs mkdir

Result: This instantly creates three folders named Alpha, Beta, and Gamma.

  • Cleaning Up Specific Files: if you want to find all .log files and delete them:

find . -name "*.log" | xargs rm

Why use xargs here? rm doesn’t accept a list of files through a pipe directly; xargs makes it possible.

  • Handling Files with Spaces: If you have a file named My Report.pdf, a normal command might try to delete “My” and “Report.pdf” separately. Use this instead:

find . -name "*.pdf" -print0 | xargs -0 rm

Result: The -print0 and -0 work together to ensure the space in the filename doesn’t break the command.


The find command searches your live filesystem in real-time. It is highly precise because you can filter by name, size, user, or file type.

  • How it works: It crawls through directories starting from a path you specify.
  • Key Syntax: find [where to look] [what to look for]
  • Common Examples:
    • find / -name hosts: Searches the entire system for any file named “hosts”.
    • find /home -user bob: Finds all files in the home directory owned by the user “bob”.
    • find /tmp -type f -name "*.log": Finds only files (not folders) ending in “.log” inside the /tmp folder.
  • Advanced Use: You can tell find to do something to the files it finds, like deleting them automatically using the exec flag.

The locate command is much faster than find because it doesn’t look at the actual files; it looks at a pre-built database index.

  • How it works: It checks a database (mlocate.db) for your keyword.
  • The Catch: If you created a file 5 minutes ago, locate won’t find it yet because the database hasn’t updated.
  • Daily Tip: Always run the command sudo updatedb before using locate to make sure the database knows about your newest files.
  • Example: locate passwd will instantly list every path on your system containing the word “passwd”.

While find and locate look for filenames, grep looks for text inside those files.

  • How it works: It scans lines of text for a pattern or keyword you provide.
  • Common Example: grep -r "fun" ~
    • r means “recursive” (look through all folders).
    • "fun" is the word you are looking for.
    • ~ is your home directory.
  • Why use it: Use this when you remember writing a specific note or setting a specific configuration but can’t remember which file you put it in.