Reputation: 11
I'm new to bash scripting. The requirement is similar to BASH copy all files except one. I'm trying to copy all files that starts with file and exclude one file that starts with file~ (backup file). This is what I hvae tried so far with bash.
path1="/home/dir1/file*" ( I know this * doesn't work - wildcards)
file_to_exclude="/home/dir1/file~*"
dest=/var/dest
count=`ls -l "$path1" 2>/dev/null | wc -l`
if [ $count !=0 ]
then
cp -p !$file_to_exclude $path1 $dest (Not sure if this is the way to exclude backup file)
fi
Could anyone please help me how to resolve this?
Upvotes: 0
Views: 705
Reputation: 16540
I don't see a reason for complication, it could be as simple as:
cp -p /home/dir1/file[^~]* /var/dest/
Or, if you only want files with extensions
cp -p /home/dir1/file[^~]*.* /var/dest/
Upvotes: 0
Reputation: 67467
use find
find . -maxdepth 1 -type f -size +0 ! -name *~* -exec cp {} dest \;
instead of line count this checks size being nonzero.
dest is the destination directory.
Upvotes: 3
Reputation: 1233
Try something like this:
file_to_exclude="some_pattern"
all_files=`ls /home/dir1/file*`
for file in $all_files; do
if [ "$file" != "$file_to_exclude" ]; then
cp $file /some/path
fi
done
Upvotes: 0