'xargs' takes the output of one command (or contents of a file), converts it into a list of arguments, and passes that list to another command to execute.
- Run a command using the input data as arguments:
command1 | xargs command2
- Count lines in multiple files:
ls *.txt | xargs wc -l
- Cat a file and use the input data as an argument:
cat file | xargs
NOTE If no xargs command is given, xargs uses 'echo' as the command.
- The same as above except it executes the command once per argument (-n 1):
cat file | xargs -n 1
- Create 10 sequential .txt files. The '-I {}' symbolizes all the input:
seq 10 | xargs -I {} touch {}.txt
- Delete files with '.log' extension found by 'find'. The -print0 in 'find' and -0 in 'xargs' use a null character as a delimiter, ensuring filenames with spaces or special characters are handled correctly.
find . -name "*.log" -print0 | xargs -0 rm -f
- Find and delete all backup files (.bak). The '-p' option is useful for destructive operations, as it displays the command to be executed and asks for user confirmation (y/n).
find . -type f -name "*.bak" | xargs -p rm
- The '-d' option sets the delimiter (spaces by default):
ls | xargs -n 1 (Files/directories with spaces are a problem.)
ls | xargs -n 1 -d \n (Uses new line as delimiter and problem solved!)
- xargs can print (-a) the contents of a file to stdout. We can use '-p' to prompt for 'y/n' before executing. We can use '-r' to only execute if stdin is not empty.
xargs -a 1.txt
xargs -p -a 1.txt
xargs -r -p -a 1.txt
REFERENCED:
โบ https://gitlab.com/dwt1/vidman
WANT TO SUPPORT THE CHANNEL?
๐ฐ Patreon: https://www.patreon.com/distrotube
๐ณ Paypal: https://www.paypal.com/donate/?hosted_button_id=MW3ZFGS8Q9JGW
๐๏ธ Amazon: https://amzn.to/2RotFFi
๐ Teespring: https://teespring.com/stores/distrotube
DT ON THE WEB:
๐ธ๏ธ Website: http://distro.tube
๐ GitLab: https://gitlab.com/dwt1
๐จ๏ธ Mastodon: https://fosstodon.org/@distrotube
๐ซ Reddit: https://www.reddit.com/r/DistroTube/
๐ฝ๏ธ Odysee: https://odysee.com/@DistroTube:2
FREE AND OPEN SOURCE SOFTWARE THAT I LIKE:
๐ Brave Browser - https://brave.com/
๐ฝ๏ธ Open Broadcaster Software: https://obsproject.com/
๐ฌ Kdenlive: https://kdenlive.org
๐จ GIMP: https://www.gimp.org/
๐ป VirtualBox: https://www.virtualbox.org/
๐๏ธ Doom Emacs: https://github.com/hlissner/doom-emacs
Your support is very much appreciated. Thanks, guys!
...
https://www.youtube.com/watch?v=tUkQcvytVtw