0

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?

Community
  • 1
  • 1
juggernaut
  • 11
  • 4

3 Answers3

3

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.

karakfa
  • 66,216
  • 7
  • 41
  • 56
0

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
alnet
  • 1,093
  • 1
  • 12
  • 24
0

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/
Abdulrahman Alsoghayer
  • 16,462
  • 7
  • 51
  • 56