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.
| Command | Description |
|---|---|
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.txt | Like the cat command, displays the contents of a file |
Common options:
| Option | Purpose |
|---|---|
-L N | Limits the input lines per command |
-P N | Runs multiple commands in parallel |
-I @ | Replaces @ with each input line as an argument instead of whitespace |
-n N | Pass at most N arguments per command |
-0 | Input items are separated by null (safe for spaces) |
-t | Print the command before executing it |
-r | Don’t run if there’s no input |
-a FILE | Read 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
-0or-I,xargssplits input on whitespace. If your filenames contain spaces, you’ll get corrupted arguments. Always usefind ... -print0 | xargs -0 ...for filenames.
Warning:
xargs rmwith no results can try to runrmwith no args. Always test withechofirst, and considerxargs -rto 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:
- If the file doesn’t exist โ create it (empty)
- If the file exists โ update its timestamp to now
| Command | Description |
|---|---|
touch my.txt | Creates an empty file |
touch my1.txt my2.txt | Creates multiple empty files |
touch myfile{1..5}.txt | Creates 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:
| Option | Purpose |
|---|---|
-a | Change only access time |
-m | Change only modification time |
-t STAMP | Set a specific timestamp ([[CC]YY]MMDDhhmm[.ss]) |
-d STRING | Set time from a human-readable string |
-r FILE | Use another file’s timestamps |
-c | Don’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
| Command | Purpose |
|---|---|
CMD | xargs CMD2 | Pass output as args to CMD2 |
xargs -n N | N args per command |
xargs -L N | N lines per command |
xargs -I @ | Replace @ with each line |
xargs -P N | Run N in parallel |
xargs -0 | Null-separated input |
xargs -a FILE | Read from file |
xargs -t | Print command first |
xargs -r | Don’t run on empty input |
touch
| Command | Purpose |
|---|---|
touch FILE | Create or update a file |
touch F1 F2 F3 | Create multiple files |
touch file{1..5}.txt | Brace expansion |
touch file{a..e}.log | Letter range |
touch -a FILE | Only access time |
touch -m FILE | Only modification time |
touch -t STAMP FILE | Specific timestamp |
touch -d STRING FILE | Human-readable time |
touch -r REF FILE | Use another file’s time |
touch -c FILE | Don’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
| Pitfall | Problem | Solution |
|---|---|---|
| Filenames with spaces | xargs splits wrong | Use -print0 + -0 |
| Empty input to xargs | Command runs with no args | Add -r |
xargs rm with no results | Deletes unintended files | Test with echo first |
| Parallel order | -P mixes output | Don’t rely on order |
Forgot -I | No substitution | Add -I @ |
touch on existing file | Timestamp reset | Use touch -c or check first |
| Brace expansion not working | Quoted braces | Don’t quote: {1..5} |
| Permissions | Can’t create in dir | Check 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
| Command | Purpose | Example |
|---|---|---|
CMD | xargs CMD2 | Pass output as args | echo a b | xargs echo |
xargs -n N | N args per command | echo 1 2 3 4 | xargs -n 2 |
xargs -L N | N lines per command | printf "a\nb\n" | xargs -L 1 |
xargs -I @ | Replace @ per line | xargs -I @ echo "Got @" |
xargs -P N | N parallel | xargs -P 4 -I @ wget @ |
xargs -0 | Null-separated | find . -print0 | xargs -0 |
xargs -a FILE | Read from file | xargs -a list.txt |
xargs -t | Show command | echo a | xargs -t echo |
xargs -r | Skip empty input | find . | xargs -r rm |
touch FILE | Create/update file | touch my.txt |
touch F1 F2 | Multiple files | touch a.txt b.txt |
touch file{1..5}.txt | Brace expansion | touch file{1..5}.txt |
touch -a FILE | Access time only | touch -a my.txt |
touch -m FILE | Mod time only | touch -m my.txt |
touch -t STAMP FILE | Specific time | touch -t 202401150800 f.txt |
touch -d STRING FILE | Human-readable | touch -d "yesterday" f.txt |
touch -r REF FILE | Reference another file | touch -r a.txt b.txt |
touch -c FILE | Don’t create if missing | touch -c maybe.txt |
Key takeaways:
xargsturns stdin into arguments โ essential when a command doesn’t read stdin-nlimits args,-Llimits lines,-I @substitutes per item,-Pruns parallel-0withfind -print0is the safe way to handle filenames with spaces-rprevents running the command when input is empty- Always test destructive
xargswithechobefore usingrm touchdoes two things: creates empty files and updates timestamps- Brace expansion (
{1..5},{a..e}) creates many files in one command -tsets a specific time,-daccepts human-readable strings,-rcopies another file’s time-cprevents accidental file creation when you only want to update an existing file- Combine the two:
cat list.txt | xargs -I @ touch @.txtto 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!