Reputation: 1484
The idea behind this code is to find all files within a directory larger than 1KB (or 1000 bytes), compress them, and delete them from the original directory. I was able to figure out both of the separate commands but am unsure how to link the output from the first to the second commands (if that makes sense)? Can anyone point me in the right direction?
# Initialize variables
dir=~/Test
# Change directory to $DIRECTORY
cd "$dir"
# Find all files in the current directory that are larger than 1000 bytes (1KB).
find . -maxdepth 1 -type f -size +1000c | zip -mT backup
Upvotes: 4
Views: 4352
Reputation: 26
Use xargs to pass each line in the output of find as zip argument:
find . -maxdepth 1 -type f -size +1000c | xargs -I f zip -mT backup f
Also you can use a while loop to do the same:
find . -maxdepth 1 -type f -size +1000c | while read f ; do zip -mT backup $f ; done
Upvotes: 0
Reputation: 469
I provided a stub before, but I decided to flesh out the script. This still won't handle pathological cases such as filenames containing wildcards.
#!/usr/bin/bash
# the following line handles filenames with spaces
IFS='
'
backupfilename=backup;
for file in $(find . -maxdepth 1 -type f -size +1000c)
do
if zip ${backupfilename} -u "${file}" # test that zip succeeded
then
echo "added file ${file} to zip archive ${backupfilename}" 1>&2;
# add your remove command here; remember to use quotes "${filename}"
echo "file ${file} has been deleted" 1>&2;
fi
done
The only thing I have left out is the delete command. You should work that out yourself and test it carefully to be sure you don't accidentally delete files you didn't want to delete.
Upvotes: 2
Reputation: 47169
Use the -exec
option instead of trying to pipe the next command:
find . -maxdepth 1 -type f -size +1000c -exec zip -mT backup {} \;
Would create a zip
archive containing the matched files.
Upvotes: 4