Help for shell scripts for moving all files from one directory to another

No still same result..unexceptd do please do the needful.Thanks in adv

Oh dear, I typed that too quickly:

dir_one="/usr/bin/sou"
dir_two="/usr/bin/sou/temp"
cd "$dir_one"
for file in *.jpg *.txt *.csv *.pdf *.gz
do
  mv "$file" "$dir_two"
done

hey thanks.while i am executing in script i am getting unexcepeted do error but while executing manually the above code it it working fine.so what ll be the issue while executing the same code pasting mv.sh can you please advise on it

If your script begins as it does in your original post, the script may not be using the shell you think it's using. The script file should begin with the #!path/to/shell line.

If that isn't the problem, provide the full script that you are using in an unambiguous form (using hexdump or od, for example). Also, tell us something about the environment you're working with. Which shell exactly? Which operating system?

Regards,
Alister

Further to alister, please post the script how it appears when processed through this "sed" command which is designed to make control codes visible.
It also shows a normal unix line terminator as a dollar sign. I notice from your post #1 that the script has a large amount of trailing space characters on some lines and I wonder if either this script is the product of a Microsoft editor or whether you have lines which have wrapped and are therefore missing the line terminator.

sed -n l mv.sh

Don't forget to post your Operating System and version and what Shell you are using.

Hi,

Try this one,

find /usr/bin/sou/ -name "*.txt" -o -name "*.csv" -exec  mv {} /usr/bin/sou/temp/ \;

Cheers,
Ranga:)

That will not work correctly due to operator precedence (the -exec only occurs when the basename matches *.csv).

Regards,
Alister

find /usr/bin/sou/ -name "*.txt" -o -name "*.csv" | xargs -I {} mv '{}' /usr/bin/sou/temp/

@alister: Thanks, Please elabrate the issue.

Cheers,
Ranga :slight_smile:

alister is correct.
The command needed the "or" condition to be in escaped brackets.:

find /usr/bin/sou/ -name \( "*.txt" -o -name "*.csv" \) | xargs -I {} mv '{}' /usr/bin/sou/temp/

Ps. I can't verify that "xargs" command but it looks unlikely.

When find parses it arguments, everthing that follows the last pathname argument (/usr/bin/sou/) is a primary argument (-name, -type, -exec, and so on). Each primary argument is a term in a boolean expression. In that expression's grammar, the logical AND operator (-a) has higher precedence than the logical OR operator (-o). When two primary arguments are not the operands of either -a or -o, -a is implicitly assumed.

Starting with your original command:

find /usr/bin/sou/ -name "*.txt" -o -name "*.csv" -exec  mv {} /usr/bin/sou/temp/ \;

Adding the implicit AND operator:

find /usr/bin/sou/ -name "*.txt" -o -name "*.csv" -a -exec  mv {} /usr/bin/sou/temp/ \;

Since -a is higher precedence than -o, the equivalent with precedence made explicit:

find /usr/bin/sou/ -name "*.txt" -o \( -name "*.csv" -a -exec  mv {} /usr/bin/sou/temp/ \; \)

As you can now see, find will only mv the file if the basename matches *.csv. Note that nothing at all will happen when the basename matches *.txt. The -exec will not occur and since there's an -exec in the expression, find will not perform an implicit -print.

What you meant to do:

find /usr/bin/sou/ \( -name "*.txt" -o -name "*.csv" \) -exec  mv {} /usr/bin/sou/temp/ \;

And even then, that may not be correct. The original post's attempt suggests that descending into subdirectories is not desired. If that's the case, this find command is still not adequate.

Regards,
Alister

2 Likes

all the files are moving including subdirectiores files also.how can i ll move only files present in the directory to one specified subdriectires which not move the files present in the other subdirectories

By not using find... e.g. post #22
post#23:
How did you copy/paste? using a PC with windows? saved e.g. with notepad and transfered to unix?