xargs is a very powerful command that takes output of a command and pass it as argument of another command. Following are some practical examples on how to use xargs effectively.
xargs 는 매우 효과적인 명령어로써 출력된 결과를 인자값으로 이용하여 다른 커맨드에서 활용할 수 있게 만들어 줍니다. 아래 예제와 같이 강력하게 사용할 수 있습니다.
Xargs Example 1:
When you are trying to delete too many files using rm, you may get error message: /bin/rm Argument list too long – Linux. Use xargs to avoid this problem.
너님이 존나 많은 파일을 rm으로 지워야 할때 분명 error 메시지가 발생할 겁니다. 이는 rm에서 종니 많은 인자값이 있다고 나올껍니다. (/bin/rm Argument list too long) 이걸 xargs 를 이용하여 해결 할 수 있습니다.
# find ~ -name ‘*.log’ -print0 | xargs -0 rm -f
Xargs Example 2:
Get a list of all the *.conf file under /etc/. There are different ways to get the same result. Following example is only to demonstrate the use of xargs. The output of the find command in this example is passed to the ls –l one by one using xargs.
/etc 폴더에 *.conf 파일 리스트를 얻은 후, 이 리스트를 다른 명령어에서 인자값으로 쓸 수 있도록 할 수 있습니다. 아래 명령은 find 명령으로 찾은 리스트를 ls -l 로 모두 출력하는 결과 입니다.
# find /etc -name "*.conf" | xargs ls –l
Xargs Example 3:
If you have a file with list of URLs that you would like to download, you can use xargs as shown below.
파일안에 URLs 리스트가 있다고 가정할 떄, 아래와 같이 URL리스트를 인자로 wget으로 넘겨 일괄적으로 다운로드 받을 수 있습니다.
# cat url-list.txt | xargs wget –c
Xargs Example 4:
Find out all the jpg images and archive it.
모든 jpg 파일을 찾아 images.tar.gz 로 압축 할 수 있습니다.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
Xargs Example 5:
Copy all the images to an external hard-drive.
ls 로 출력된 모든 이미지를 인자로 받아 외장하드로 복사 할 수 있습니다.
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory