|

Linux CLI 36 ๐Ÿง xargs and touch commands

echo "hello world" | xargs echo @
ls -d */ | xargs ls -lh @
xargs -a aaa.txt
touch my.txt
touch my1.txt my2.txt
touch myfile{1..5}.txt

xargs and touch don’t seem related at first โ€” one builds and runs commands from input, the other creates and updates files. But they’re both classic command-line building blocks: xargs is the glue that turns output from one command into arguments for another, and touch is the simplest way to create files or bump timestamps.

Key point: Many commands (rm, ls, cp) take arguments, not stdin. xargs bridges that gap. And touch is deceptively powerful โ€” it can create many files at once with brace expansion and update access/modification times precisely.


a – xargs command

xargs reads from standard input and executes a command with that input as arguments. You can use it to perform operations on multiple files or directories โ€” especially when a command doesn’t read from stdin directly.

Why it matters: Commands like rm, mkdir, ls, and cp expect their input as arguments, not as piped stdin. xargs converts stdin into arguments. It’s the bridge between pipes and argument lists.

CommandDescription
echo "hello world" | xargs echo @Reads the string and adds it to echo
ls -d */ | xargs ls -lh @Reads all directories and lists them long + human-readable
xargs -a aaa.txtLike the cat command, displays the contents of a file

Common options:

OptionPurpose
-L NLimits the input lines per command
-P NRuns multiple commands in parallel
-I @Replaces @ with each input line as an argument instead of whitespace
-n NPass at most N arguments per command
-0Input items are separated by null (safe for spaces)
-tPrint the command before executing it
-rDon’t run if there’s no input
-a FILERead items from a file instead of stdin

Examples:

# Basic: pass words as arguments
$ echo "hello world" | xargs echo @
@ hello world

$ echo "hello world" | xargs echo
hello world

# List all directories with details
$ ls -d */ | xargs ls -lh
drwxr-xr-x 2 kronos kronos 4.0K Jan 15 10:00 documents/
drwxr-xr-x 5 kronos kronos 4.0K Jan 15 09:00 downloads/
drwxr-xr-x 3 kronos kronos 4.0K Jan 14 18:00 projects/

# Read a file's contents (like cat)
$ xargs -a aaa.txt
line one
line two
line three

# Limit to 2 arguments per command
$ echo "1 2 3 4 5 6" | xargs -n 2 echo
1 2
3 4
5 6

# One line per command
$ printf "a\nb\nc\n" | xargs -L 1 echo
a
b
c

# Replace @ with each input line (one command per line)
$ printf "file1.txt\nfile2.txt\nfile3.txt\n" | xargs -I @ echo "Processing @"
Processing file1.txt
Processing file2.txt
Processing file3.txt

# Show the command before running it
$ echo "1 2 3" | xargs -t echo
echo 1 2 3
1 2 3

# Run commands in parallel (4 at a time)
$ printf "1\n2\n3\n4\n5\n6\n" | xargs -P 4 -I @ echo "Job @"
Job 1
Job 2
Job 4
Job 3
Job 6
Job 5

# Safely handle filenames with spaces
$ find . -name '*.txt' -print0 | xargs -0 ls -lh
-rw-r--r-- 1 kronos kronos 1.2K Jan 15 10:00 ./my file.txt
-rw-r--r-- 1 kronos kronos 2.4K Jan 15 09:00 ./notes.txt

# Remove many files at once
$ find . -name '*.tmp' | xargs rm

# Create many directories from a list
$ cat dirs.txt
projects
archive
backup

$ cat dirs.txt | xargs mkdir

โš ๏ธ Warning: Without -0 or -I, xargs splits input on whitespace. If your filenames contain spaces, you’ll get corrupted arguments. Always use find ... -print0 | xargs -0 ... for filenames.

Warning: xargs rm with no results can try to run rm with no args. Always test with echo first, and consider xargs -r to skip execution when input is empty.


b – touch command

touch is used to create new empty files. It is also used to update the timestamp of a file โ€” both the access time and the modification time. It can do this for one or multiple files.

Two jobs in one command:

  1. If the file doesn’t exist โ†’ create it (empty)
  2. If the file exists โ†’ update its timestamp to now
CommandDescription
touch my.txtCreates an empty file
touch my1.txt my2.txtCreates multiple empty files
touch myfile{1..5}.txtCreates 5 files using brace expansion

Examples:

# Create one file
$ touch my.txt
$ ls -l my.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my.txt

# Create multiple files explicitly
$ touch my1.txt my2.txt
$ ls -l my1.txt my2.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my1.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my2.txt

# Brace expansion โ€” 5 files
$ touch myfile{1..5}.txt
$ ls -l myfile*.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 myfile1.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 myfile2.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 myfile3.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 myfile4.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 myfile5.txt

# Letters too
$ touch file{a..e}.log
$ ls file*.log
filea.log  fileb.log  filec.log  filed.log  filee.log

# Update the timestamp of an existing file
$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 01 08:00 old.txt

$ touch old.txt
$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 15 10:36 old.txt
# โœ… timestamp updated, contents untouched

# Create multiple files in different directories
$ touch dir1/file.txt dir2/file.txt

# Set a specific timestamp
$ touch -t 202401150800 old.txt
$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 15 08:00 old.txt

# Only update access time
$ touch -a file.txt

# Only update modification time
$ touch -m file.txt

# Use another file's timestamps as reference
$ touch -r reference.txt target.txt

# Don't create โ€” only update if exists
$ touch -c nonexistent.txt
$ ls nonexistent.txt
ls: cannot access 'nonexistent.txt': No such file or directory

Common options:

OptionPurpose
-aChange only access time
-mChange only modification time
-t STAMPSet a specific timestamp ([[CC]YY]MMDDhhmm[.ss])
-d STRINGSet time from a human-readable string
-r FILEUse another file’s timestamps
-cDon’t create the file if it doesn’t exist

More timestamp examples:

# Set to a specific date/time
$ touch -d "2024-01-15 08:00:00" old.txt

# Set to yesterday
$ touch -d "yesterday" old.txt

# Set to 1 hour ago
$ touch -d "1 hour ago" old.txt

# Set to next Monday
$ touch -d "next Monday" old.txt

Complete Example Session

# ============================================
# PART 1: XARGS โ€” BASIC USAGE
# ============================================

# Pass words as arguments
$ echo "hello world" | xargs echo @
@ hello world

$ echo "hello world" | xargs echo
hello world

# List directories with details
$ ls -d */
documents/  downloads/  projects/

$ ls -d */ | xargs ls -lh
drwxr-xr-x 2 kronos kronos 4.0K Jan 15 10:00 documents/
drwxr-xr-x 5 kronos kronos 4.0K Jan 15 09:00 downloads/
drwxr-xr-x 3 kronos kronos 4.0K Jan 14 18:00 projects/

# Read from a file
$ cat aaa.txt
line one
line two
line three

$ xargs -a aaa.txt
line one
line two
line three

# ============================================
# PART 2: XARGS โ€” OPTIONS
# ============================================

# -n: limit args per command
$ echo "1 2 3 4 5 6" | xargs -n 2 echo
1 2
3 4
5 6

# -L: one line per command
$ printf "a\nb\nc\n" | xargs -L 1 echo
a
b
c

# -I: replace @ with each line
$ printf "file1.txt\nfile2.txt\n" | xargs -I @ echo "Processing @"
Processing file1.txt
Processing file2.txt

# -t: print command before running
$ echo "1 2 3" | xargs -t echo
echo 1 2 3
1 2 3

# -P: parallel
$ printf "1\n2\n3\n4\n" | xargs -P 2 -I @ echo "Job @"
Job 1
Job 2
Job 4
Job 3

# ============================================
# PART 3: XARGS โ€” SAFE FILENAME HANDLING
# ============================================

# Create a file with spaces
$ touch "my file.txt"

# โŒ Unsafe: xargs splits on whitespace
$ find . -name '*.txt' | xargs ls -lh
ls: cannot access './my': No such file or directory
ls: cannot access 'file.txt': No such file or directory

# โœ… Safe: use -print0 and -0
$ find . -name '*.txt' -print0 | xargs -0 ls -lh
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:00 ./my file.txt

# ============================================
# PART 4: TOUCH โ€” CREATE FILES
# ============================================

$ touch my.txt
$ ls -l my.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my.txt

$ touch my1.txt my2.txt
$ ls -l my1.txt my2.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my1.txt
-rw-r--r-- 1 kronos kronos 0 Jan 15 10:35 my2.txt

$ touch myfile{1..5}.txt
$ ls myfile*.txt
myfile1.txt  myfile2.txt  myfile3.txt  myfile4.txt  myfile5.txt

$ touch file{a..e}.log
$ ls file*.log
filea.log  fileb.log  filec.log  filed.log  filee.log

# ============================================
# PART 5: TOUCH โ€” UPDATE TIMESTAMPS
# ============================================

$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 01 08:00 old.txt

$ touch old.txt
$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 15 10:36 old.txt

# Specific timestamp
$ touch -t 202401150800 old.txt
$ ls -l old.txt
-rw-r--r-- 1 kronos kronos 1024 Jan 15 08:00 old.txt

# Human-readable date
$ touch -d "yesterday" old.txt
$ touch -d "1 hour ago" old.txt

# ============================================
# PART 6: COMBINING XARGS AND TOUCH
# ============================================

# Create files from a list
$ cat names.txt
alice
bob
charlie

$ cat names.txt | xargs -I @ touch @.txt
$ ls *.txt
alice.txt  bob.txt  charlie.txt

# Bulk-update timestamps
$ find . -name '*.txt' -print0 | xargs -0 touch

Quick Reference

xargs

CommandPurpose
CMD | xargs CMD2Pass output as args to CMD2
xargs -n NN args per command
xargs -L NN lines per command
xargs -I @Replace @ with each line
xargs -P NRun N in parallel
xargs -0Null-separated input
xargs -a FILERead from file
xargs -tPrint command first
xargs -rDon’t run on empty input

touch

CommandPurpose
touch FILECreate or update a file
touch F1 F2 F3Create multiple files
touch file{1..5}.txtBrace expansion
touch file{a..e}.logLetter range
touch -a FILEOnly access time
touch -m FILEOnly modification time
touch -t STAMP FILESpecific timestamp
touch -d STRING FILEHuman-readable time
touch -r REF FILEUse another file’s time
touch -c FILEDon’t create if missing

Best Practices

โœ… Do This:

# Use -print0 | xargs -0 for filenames with spaces
find . -name '*.txt' -print0 | xargs -0 rm    # โœ…

# Use -I @ for one command per item
cat list.txt | xargs -I @ cp @ /backup/       # โœ…

# Use -r to avoid running on empty input
find . -name '*.tmp' | xargs -r rm            # โœ…

# Test destructive xargs with echo first
find . -name '*.bak' | xargs echo rm          # โœ…

# Use brace expansion for many files
touch file{1..100}.txt                        # โœ…

# Use -c with touch to avoid accidental file creation
touch -c existing.txt                         # โœ…

# Quote filenames with spaces
touch "my file.txt"                           # โœ…

โŒ Don’t Do This:

# Don't pipe filenames into xargs without -0
find . -name '*.txt' | xargs rm               # โŒ breaks on spaces

# Don't forget -I when you need @ substitution
echo "a b c" | xargs echo prefix-             # โŒ all on one line

# Don't run xargs rm without testing
find / -name '*.tmp' | xargs rm               # โŒ dangerous!

# Don't touch a file without checking existing content
touch config.txt                              # โŒ timestamp reset

# Don't assume xargs runs in order with -P
cat list.txt | xargs -P 4 process             # โŒ order varies

# Don't use touch as a fallback for editing files
touch script.sh                               # โŒ empty script

# Don't create files in system directories
touch /etc/config                            # โŒ permission/ownership

Common Pitfalls

PitfallProblemSolution
Filenames with spacesxargs splits wrongUse -print0 + -0
Empty input to xargsCommand runs with no argsAdd -r
xargs rm with no resultsDeletes unintended filesTest with echo first
Parallel order-P mixes outputDon’t rely on order
Forgot -INo substitutionAdd -I @
touch on existing fileTimestamp resetUse touch -c or check first
Brace expansion not workingQuoted bracesDon’t quote: {1..5}
PermissionsCan’t create in dirCheck write perms

Real-World Examples

1. Delete All .tmp Files Safely

$ find . -name '*.tmp' -print0 | xargs -0 rm

2. Copy a List of Files to a Backup

$ cat filelist.txt | xargs -I @ cp @ /backup/

3. Run a Command on Each Line of a File

$ cat servers.txt | xargs -I @ ssh @ "uptime"

4. Parallel Downloads

$ cat urls.txt | xargs -P 4 -I @ wget @

5. Create 100 Test Files

$ touch test{001..100}.txt
$ ls test*.txt | wc -l
100

6. Create a Directory Structure Quickly

$ mkdir -p project/{src,bin,doc,test}
$ touch project/src/main.c project/doc/README.md

7. Update All .log File Timestamps

$ find /var/log -name '*.log' -print0 | xargs -0 touch

8. Set File Timestamps to Match a Reference

$ touch -r /etc/hosts myhosts.txt

9. Backdate a File for Testing

$ touch -d "2020-01-01 00:00:00" oldfile.txt
$ ls -l oldfile.txt
-rw-r--r-- 1 kronos kronos 0 Jan  1  2020 oldfile.txt

10. Read a File Line by Line with xargs

$ xargs -a /etc/hostname
olympos

$ xargs -a /etc/hosts -L 1
127.0.0.1 localhost
::1 localhost
192.168.1.100 olympos

11. Use xargs for grep

# Search for a pattern across many files
$ find . -name '*.py' -print0 | xargs -0 grep -l 'TODO'
./src/main.py
./src/utils.py

12. Create Files from a List of Names

$ cat names.txt
alice
bob
charlie

$ cat names.txt | xargs -I @ touch @.txt
$ ls *.txt
alice.txt  bob.txt  charlie.txt

Visual: How xargs Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              Without xargs                   โ”‚
โ”‚                                              โ”‚
โ”‚  $ echo "file1 file2 file3" | rm             โ”‚
โ”‚  rm: missing operand                         โ”‚
โ”‚  (rm doesn't read stdin!)                    โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              With xargs                      โ”‚
โ”‚                                              โ”‚
โ”‚  $ echo "file1 file2 file3" | xargs rm       โ”‚
โ”‚                                              โ”‚
โ”‚  stdin: "file1 file2 file3"                  โ”‚
โ”‚     โ”‚                                        โ”‚
โ”‚     โ–ผ                                        โ”‚
โ”‚  xargs splits into args:                     โ”‚
โ”‚     file1 file2 file3                        โ”‚
โ”‚     โ”‚                                        โ”‚
โ”‚     โ–ผ                                        โ”‚
โ”‚  rm file1 file2 file3   โ† runs!              โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           xargs -I @ substitution            โ”‚
โ”‚                                              โ”‚
โ”‚  $ cat list.txt                              โ”‚
โ”‚  file1.txt                                   โ”‚
โ”‚  file2.txt                                   โ”‚
โ”‚  file3.txt                                   โ”‚
โ”‚                                              โ”‚
โ”‚  $ cat list.txt | xargs -I @ echo "Got @!"   โ”‚
โ”‚                                              โ”‚
โ”‚  Runs:                                       โ”‚
โ”‚    echo "Got file1.txt!"                     โ”‚
โ”‚    echo "Got file2.txt!"                     โ”‚
โ”‚    echo "Got file3.txt!"                     โ”‚
โ”‚                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

CommandPurposeExample
CMD | xargs CMD2Pass output as argsecho a b | xargs echo
xargs -n NN args per commandecho 1 2 3 4 | xargs -n 2
xargs -L NN lines per commandprintf "a\nb\n" | xargs -L 1
xargs -I @Replace @ per linexargs -I @ echo "Got @"
xargs -P NN parallelxargs -P 4 -I @ wget @
xargs -0Null-separatedfind . -print0 | xargs -0
xargs -a FILERead from filexargs -a list.txt
xargs -tShow commandecho a | xargs -t echo
xargs -rSkip empty inputfind . | xargs -r rm
touch FILECreate/update filetouch my.txt
touch F1 F2Multiple filestouch a.txt b.txt
touch file{1..5}.txtBrace expansiontouch file{1..5}.txt
touch -a FILEAccess time onlytouch -a my.txt
touch -m FILEMod time onlytouch -m my.txt
touch -t STAMP FILESpecific timetouch -t 202401150800 f.txt
touch -d STRING FILEHuman-readabletouch -d "yesterday" f.txt
touch -r REF FILEReference another filetouch -r a.txt b.txt
touch -c FILEDon’t create if missingtouch -c maybe.txt

Key takeaways:

  • xargs turns stdin into arguments โ€” essential when a command doesn’t read stdin
  • -n limits args, -L limits lines, -I @ substitutes per item, -P runs parallel
  • -0 with find -print0 is the safe way to handle filenames with spaces
  • -r prevents running the command when input is empty
  • Always test destructive xargs with echo before using rm
  • touch does two things: creates empty files and updates timestamps
  • Brace expansion ({1..5}, {a..e}) creates many files in one command
  • -t sets a specific time, -d accepts human-readable strings, -r copies another file’s time
  • -c prevents accidental file creation when you only want to update an existing file
  • Combine the two: cat list.txt | xargs -I @ touch @.txt to bulk-create files

Remember: xargs is the bridge between pipes and arguments โ€” without it, many commands can’t use piped input. Always use -0 with find -print0 when filenames might have spaces, and -r to avoid empty-input surprises. touch is simple but powerful โ€” create one file or a hundred with brace expansion, update timestamps precisely with -t or -d, and use -c when you don’t want to accidentally create. Master these two, and your one-liners get shorter and your bulk operations get safer.


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!