Friday 29 March 2013

copy/move/delete files using xargs


How to move/copy/delete largest 15 files to a particular directory

Go to the user directory
$ cd /home/user

If you want to move them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do mv "$f" /backup;done


Explanation:
ls -s - prints size of each file
sort -n - sort files
tail -15 - 15 largest files
awk '{print $2}' - cut the filename
while loop moves files to /backup directory




If you want to delete them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do rm "$f";done

If you want to copy them, use this below command
$ ls -s|sort -n|tail -15|awk '{print $2}'|while read f;do cp "$f" /backup;done