ubuntu, updated

xargs snippets

Ready-to-use xargs one-liners for files, text, users, packages, services, hosts and Docker.

New to xargs? Read xargs: run a command for many items first. Test destructive snippets with echo in front of the command (xargs echo rm ...).

Files

# Delete files older than 30 days (null-separated: safe for spaces)
find /srv/tmp -type f -mtime +30 -print0 | xargs -0 -r rm

# Compress all .log files, 4 at a time
find /var/log/app -name "*.log" -print0 | xargs -0 -r -P 4 -n 1 gzip

# Copy all files listed in a text file to a folder
xargs -d '\n' -I {} cp {} /backup/ < files.txt

# Make all shell scripts executable
find /usr/local/bin -name "*.sh" -print0 | xargs -0 chmod +x

# Fix ownership of files that belong to a deleted user
sudo find /srv -nouser -print0 | sudo xargs -0 -r chown root:root

# Total size of all matching files
find /var/log -name "*.gz" -print0 | xargs -0 du -ch | tail -1

Text inside files

# Replace a string in every file that contains it
grep -rlZ "old.example.com" /etc/nginx/ | sudo xargs -0 -r sed -i 's/old\.example\.com/new.example.com/g'

# Count lines in all PHP files
find . -name "*.php" -print0 | xargs -0 wc -l | tail -1

# Search inside the files a command lists
dpkg -L nginx | grep '\.conf$' | xargs grep -l "ssl"

Users and permissions

# Create users from a list (one name per line)
xargs -n 1 sudo adduser --disabled-password --gecos "" < users.txt

# Add several users to a group
echo anna ben carla | xargs -n 1 sudo usermod -aG developers

# Lock all accounts listed in a file
xargs -n 1 sudo usermod -L -e 1 < leavers.txt

Packages and services

# Install packages listed in a file
xargs -a packages.txt sudo apt install -y

# Restart several services
echo nginx php8.3-fpm | xargs -n 1 sudo systemctl restart

# Show the status of all failed units
systemctl --failed --plain --no-legend | awk '{print $1}' | xargs -r systemctl status

# Kill all processes of a user (pkill -u is simpler; this shows the pattern)
pgrep -u olduser | xargs -r sudo kill

xargs -a FILE reads the list from a file. Unlike < FILE, it keeps the terminal connected to the command, so prompts still work.

Hosts and network

# Ping a list of hosts, 10 in parallel
xargs -P 10 -I {} sh -c 'ping -c1 -W1 {} >/dev/null && echo "{} up" || echo "{} DOWN"' < hosts.txt

# Run a command on many servers via SSH, 5 at a time
xargs -P 5 -I {} ssh {} "hostname; uptime" < servers.txt

# Check which hosts answer on port 443
xargs -P 10 -I {} sh -c 'nc -z -w2 {} 443 && echo "{} open"' < hosts.txt

When the command needs more than one step per item, wrap it in sh -c '...' with -I {} as above.

Docker

# Remove all stopped containers
docker ps -aq -f status=exited | xargs -r docker rm

# Pull a list of images in parallel
xargs -P 4 -n 1 docker pull < images.txt

Cheat sheet

PatternUse for
find ... -print0 | xargs -0 -r CMDFiles, always safe
... | xargs -n 1 CMDCommands that take one argument
... | xargs -I {} CMD {} targetItem in the middle of the command
... | xargs -P N ...Speed up with N parallel runs
xargs -a list.txt CMDList from a file
... | xargs echo CMDDry run
§ home
~/knubertus/home/
1,1 Top