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?

1
  • 1
    how about cp -p /home/dir1/file[^~]* $dest ? Commented Apr 6, 2016 at 15:52

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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 

2 Comments

yes. it is. do you have other suggestion how to get list of files?
all_files=(*). Now you have a bash array and don't have to worry about whitespace issues until you expand each member.
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/ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.