Xargs: The ability to convert data passed from a pipe or standard input into a parameter followed by a Xargs command
[Email protected]:~/linux/CP$lsghostwu_hardlink ghostwu_home ghostwu_softlink ghostwu.txt[email protected]:~/linux/CP$CatGhostwu.txt1 2 34 5 6 78 9 Ten[email protected]:~/linux/CP$Xargs<Ghostwu.txt1 2 3 4 5 6 7 8 9 Ten
The contents of the Ghostwu.txt file are passed through input redirection to Xargs, which is equivalent to echo 1 2 3 4 5 6 7 8 9 10
-N: Specifies a row to display several
[Email protected]:~/linux/cpxargs2 <1234 56789
-D: Specify delimiter
[Email protected]:~/linux/cpecho a-b-c-da-b-c-d[email protected]:~/linux/ CPEchoxargs -D-a b c D
-I: Replace previous results with {}, often in conjunction with the Find command
Copy the files from the home directory to the TMP directory at the end of the. py
[Email protected]:~$lsDesktop examples.desktop linux Pictures python tmpdocuments git_test Music project software V IdeosdownloadsInfoPHP public templates[email protected]:~$lsTmp[email protected]:~$Find. -name"*.py"|Xargs-I.CP-a {} tmp/[email protected]:~$lstmp for. py func3.py func5.py global2.py while1.pyfunc2.py func4.py func.py global.py while. py
Delete files ending with. PY under TMP
[Email protected]:~$lstmp for. py func3.py func5.py global2.py while1.pyfunc2.py func4.py func.py global.py while. Py[email protected]:~$Touchtmp/{A.. F}.txt[email protected]:~$lstmpa.txt c.txt e.txt f.txt func3.py func5.py global2.py while1.pyb.txt d.txt for. py func2.py func4.py func.py global.py while. Py[email protected]:~$Find./tmp-name"*.py"|Xargs-I.RM-RF {}[email protected]:~$lstmpa.txt b.txt c.txt d.txt e.txt f.txt
Create a file with spaces
[Email protected]:~/tmp$Touch "Hello Ghostwu.txt"[email protected]:~/tmp$lsa.txt b.txt c.txt d.txt e.txt f.txt Hello ghostwu.txt[email protected]:~/tmp$Touchhi\ ghostwu.txt[email protected]:~/tmp$lsa.txt b.txt c.txt d.txt e.txt f.txt Hello ghostwu.txt hi ghostwu.txt[email protected]:~/tmp$RMhello\ ghostwu.txt [email protected]:~/tmp$lsa.txt b.txt c.txt d.txt e.txt f.txt hi ghostwu.txt[email protected]:~/tmp$RMhi\ ghostwu.txt [email protected]:~/tmp$lsa.txt b.txt c.txt d.txt e.txt f.txt
You cannot delete a file with spaces
[Email protected]:~/tmp$lsAlltxt.Tar. GZ b.txt d.txt f.txt hi ghostwu.txta.txt c.txt e.txt Hello ghostwu.txt[email protected]:
~/tmp$Find. -name"*.txt"|Xargs RMRM: Cannot remove'./hello': No Suchfileor directoryRM: Cannot remove'Ghostwu.txt': No Suchfileor directoryRM: Cannot remove'./hi': No Suchfileor directoryRM: Cannot remove'Ghostwu.txt': No Suchfileor Directory[email protected]:~/tmp$lsAlltxt.Tar. gz Hello ghostwu.txt hi ghostwu.txt
Use the man manual to find the-PRINT0 option in Find
-print0
True; Print the full file name in the standard output, followed
by a null character (instead of the newline character that
-print uses). This allows file names that contain newlines or
Other types of white space to being correctly interpreted by pro‐
Grams that process the find output. This option corresponds to
the-0 option of Xargs.
This means that the-print0 option of the Find command, when enabled, interprets the file name with a newline or other whitespace character correctly. This option is typically used in conjunction with the Xargs-0 option
So, you can do this in the following way, Xargs
ls Alltxt. Tar . GZ b.txt d.txt f.txt hi ghostwu.txta.txt c.txt e.txt Hello Ghostwu.txt[email Protected]:find"*.txt"xargs -0RM [email protected]:lsalltxt. tar. gz
Find all the. py files under the home directory, and then package
[Email protected]:~/tmp$ls[email protected]:~/tmp$Find~-name"*.py"|Xargs TarCVF allpy.Tar. GZTar: Removing leading '/'From member names/home/ghostwu/python/func2.py/home/ghostwu/python/func3.py/home/ghostwu/python/func4.py/home/ghostwu/python/func.py/home/ghostwu/python/global.py/home/ghostwu/python/global2.py/home/ghostwu/python/ while. PY/home/ghostwu/python/ for. PY/home/ghostwu/python/func5.py/home/ghostwu/python/While1.py[email protected]:~/tmp$lsallpy.Tar. Gz[email protected]:~/tmp$Tar-TVF allpy.Tar. GZ-rw-rw-r--GHOSTWU/GHOSTWU179 2018-Geneva- - +: inhome/ghostwu/python/func2.py-rw-rw-r--GHOSTWU/GHOSTWU - 2018-Geneva- - +: tohome/ghostwu/python/func3.py-rw-rw-r--GHOSTWU/GHOSTWUBayi 2018-Geneva- - +: -home/ghostwu/python/func4.py-rw-rw-r--GHOSTWU/GHOSTWU - 2018-Geneva- - +: -home/ghostwu/python/func.py-rw-rw-r--GHOSTWU/GHOSTWU Max 2018-Geneva- - +: -home/ghostwu/python/global.py-rw-rw-r--GHOSTWU/GHOSTWU124 2018-Geneva- - +: -home/ghostwu/python/global2.py-rw-rw-r--GHOSTWU/GHOSTWU - 2018-Geneva- - +: +home/ghostwu/python/ while. PY-rw-rw-r--GHOSTWU/GHOSTWU the 2018-Geneva- - +: ,home/ghostwu/python/ for. PY-rw-rw-r--GHOSTWU/GHOSTWU About 2018-Geneva- - +: -home/ghostwu/python/func5.py-rw-rw-r--GHOSTWU/GHOSTWU the 2018-Geneva- - +: athome/ghostwu/python/while1.py
Linux Common basic Commands (Xargs)