0

I'm trying to copy files from directory A to directory B. But I want to copy only files that have lower size then X and don't have file extensions specified in file extension.txt.

My main problem here is, I don't want to delete unwanted files after coping - I want to copy just files I need, using bash script.

Any ideas?

  • 1
    find might be a good start. What have you tried so far? – Jakuje Apr 17 '16 at 11:26
  • mainly "for" loop, which was a disaster... also i tried to "ls" and "cp" with flags, but it didnt worked :/ – user166182 Apr 17 '16 at 11:28
  • Can you post a sample of the extention.txt file? – choroba Apr 17 '16 at 11:53
  • i. e.: txt xy tar – user166182 Apr 17 '16 at 13:46
  • 2
    Please post the scripts that you tried to write so far. And post an example for your extensions file properly formatted in the question. – Lucas Apr 17 '16 at 13:51
2

A extended version of @mihver1's answer. Have a look at man find if you want to understand this thoroughly.

#!/bin/bash
X=1000c # or what ever your size limit is
find_args=()
for ext in $(cat extensions.txt); do
  find_args=( "${find_args[@]}" -o -name "*.$ext" )
done
find_args=( -type f -size -$X -not \( -false "${find_args[@]}" \) )
# first try it
find directory_A "${find_args[@]}" -print
# if that looks fine, copy the files
find directory_A "${find_args[@]}" -exec cp {} direcotry_B +
0
# assuming that extensions.txt looks like:
# *.txt
# *.csv
# *.img

X=your_size
pushd directoryA
rsync --exclude-from=extensions.txt $(find . -type f -size -${X}c) directoryB/
popd
  • This is buggy if files in directoryA have spaces is the file names. – Lucas Apr 17 '16 at 13:51
  • @Lucas True, but it's a good start. Maybe add --max-size option, or use --from0 --files-from. – Mikel Apr 17 '16 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.