Showing posts with label xargs command. Show all posts
Showing posts with label xargs command. Show all posts

Tuesday, October 2, 2018

xargs command examples

xargs Command

1) How can we limit output per line for any command like ls command output ?
ls |xargs -n 3

2) How can we ask for prompt from xargs command before execution ?
ls |xargs -p -n 3

3) How can we remove all html files older than 30 days by help of xargs ?

find / -name "*.html" -type f -mtime +30 |xargs rm -f 

4) How can we remove all files those have white-space " " in it's name and extension is csv ?

find / -name "*.csv" -print0 | xargs -0 rm -f 

Note:- "print0" option search files those have whitespace in it's name with regular file name as well.

5) How can we find all txt files those have "string" word in it's content ?

find / -name "*.txt" -type f |xargs grep string

6)  Find all mp3 files and copy all to directory by xargs 

find / -name "*.mp3" -type f |xargs cp {} /path/

7) Find all mp3 files and archived those 

find / -name "*.mp3" -type f |xargs tar -cvfz songs.tar.gz

Remove CTRL-M characters from a file in UNIX

 We can remove ^M by below methods 1st Method - Using sed command Type below command:    sed -e "s/^M//" filename > newfilena...