I am trying to move the files and getting error.
#!/usr/bin/ksh -x
FILES="/home/region/files"
client="reg"
ERROR="$FILES/error/"
upper=`echo "$2" | tr [a-z] [A-Z] | cut -d "." -f1-3`
lower=`echo "$upper" | tr '[A-Z]' '[a-z]'`
mv $FILES/$client/\*"${lower}"*\ $ERROR
mv $FILES/$client/\*"${upper}"*\ $ERROR
Eventhougj the files present in the directory. Files are not moved. Getting a error mesage .
@Siva2023 , show all commands, all inputs, outputs ... saying there's an error is a waste of time, just show what is going on. show the values of upper and lower , give directory listings of /home/region/files/reg /home/region/files/error/
Input files :
/home/region/files/reg/c03232.s48384.l3434.tar
/home/region/files/reg/C03223.S48384.13434.tar
Error :
mv /home/region/files/reg/*c03232.s48384.l3434* /home/region/files/error/
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
mv [-f] [-i] [-e warn|force|ignore] d1 d2
mv /home/region/files/reg/*C03223.S48384.13434* /home/region/files/error/
Usage: mv [-f] [-i] [-e warn|force|ignore] f1 f2
mv [-f] [-i] [-e warn|force|ignore] f1 ... fn d1
mv [-f] [-i] [-e warn|force|ignore] d1 d2
that implies
ls -al /home/region/files/reg/
ls -al /home/region/files/error/
script.ksh PARAMETER1 PARAMETER2
echo $upper
echo $lower
none of which you have supplied, so, in order to avoid confusion/assumptions please supply as requested.
why are you escaping the * ? in the mv command ?
tks
running my version of your script ... i get
./t.ksh c03232.S48384.l3434.tar c03232.s48384.l3434.tar
+ FILES=./region/files
+ client=reg
+ ERROR=./region/files/error/
+ echo c03232.s48384.l3434.tar
+ cut -d . -f1-3
+ tr '[a-z]' '[A-Z]'
+ upper=C03232.S48384.L3434
+ echo 'upper [C03232.S48384.L3434]'
upper [C03232.S48384.L3434]
+ echo C03232.S48384.L3434
+ tr '[A-Z]' '[a-z]'
+ lower=c03232.s48384.l3434
+ echo 'lower [c03232.s48384.l3434]'
lower [c03232.s48384.l3434]
+ mv './region/files/reg/*c03232.s48384.l3434* ./region/files/error/'
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
+ echo
+ mv './region/files/reg/*C03232.S48384.L3434* ./region/files/error/'
usage: mv [-f | -i | -n] [-v] source target
mv [-f | -i | -n] [-v] source ... directory
I tried this and getting this msg now.
code
mv $FILES/$client/\*"${lower}"* $ERROR
mv $FILES/$client/\*"${upper}"* $ERROR
output
mv: region/files/reg/*c03232.s48384.l3434*: cannot access: No such file or directory
I coulds see the file is presen in the directory.
@Siva2023 , why not show what's actually happening as requested, we won't need any dialog from you, just run the commands as requested - they will show all that is needed in 99.9% of cases.
also , show the output from
file FILES-YOU-ARE-PASSING-IN
also, what OS are you running (SunOS/Windows/linux ..... )
tks
#!/usr/bin/ksh -x
FILES="/home/region/files"
client=$1
ERROR="$FILES/error/"
upper=`echo "$2" | tr [a-z] [A-Z] | cut -d "." -f1-3`
lower=`echo "$upper" | tr '[A-Z]' '[a-z]'`
mv $FILES/$client/\*"${lower}"* $ERROR
mv $FILES/$client/\*"${upper}"* $ERROR
Error :
mv: region/files/reg/*c03232.s48384.l3434* cannot access: No such file or directory
ksh -x movefiles.ksh reg c03232.s48384.13434.tar
$1=> tar.
$2=> c03232.s48384.13434.tar
Files present in input directory.
/home/region/files/reg/c03232.s48384.13434.tar
/home/region/files/reg/C03232.S48384.13434.tar
/home/region/files/reg/C03232.s48384.13434.tar
Output :
All lower case, mixed, upper case file need to be moved to error folder.
And even more importantly, why are you escaping a space directly before $ERROR
? It causes mv
command to receive only a single (additionally invalid) argument (with a space in the name).
Please describe what you're actually trying to achieve.
Moving files with small letters only to a certain directory?
Moving files with capital letters only to other/the same directory?
Moving all files (regardless their names) to another directory? / Leaving only files with mixed capitalization?
For detecting if any filename has only small letters or only capital letters (apart from .tar extension), the *
may not be fully applicable here - you'd most likely need to use some shell patterns or regex patterns in conditional expressions.
So until you answer all of the questions above (maybe even including mine), it's a guesswork, but here's a rudimentary, unoptimized solution proposal:
$ cat movefiles.ksh
#!/usr/bin/ksh -x
FILES="/home/region/files"
client="reg"
ERROR="$FILES/error/"
# I don't know what neither of the $1 nor $2 are referring to
# so I'd suggest:
mkdir -p "$ERROR" || { echo "Couldn't create $ERROR"; exit 1; } # makes sure the "$ERROR" subdirectory exists
cd "$FILES/$client" || exit; # makes sure you start in the correct source subdirectory
for filename in *
do
fn_noext="${filename%.tar}";
if [[ "$fn_noext" =~ [[:lower:]] ]] && [[ ! "$fn_noext" =~ [[:upper:]] ]]; then
# basename (without .tar) contains only small letters
mv -v "$filename" "$ERROR"; # move it wherever you like
elif [[ "$fn_noext" =~ [[:upper:]] ]] && [[ ! "$fn_noext" =~ [[:lower:]] ]]; then
# basename (without .tar) contains only capital letters
mv -v "$filename" "$ERROR"; # move it wherever you like
else
# basename (without .tar) contains mixed small/capital letters
echo "not moving: $filename" # leave the file here
fi;
done
$ echo 'small' > /home/region/files/reg/c03232.s48384.13434.tar
$ echo 'caps' > /home/region/files/reg/C03232.S48384.13434.tar
$ echo 'mixed' > /home/region/files/reg/C03232.s48384.13434.tar
$ ls -lR /home/region/files/
/home/region/files/:
total 8
drwxr-xr-x 2 theia theia 4096 Mar 18 14:26 error
drwxr-xr-x 2 theia theia 4096 Mar 18 14:27 reg
/home/region/files/error:
total 0
/home/region/files/reg:
total 12
-rw-r--r-- 1 theia theia 5 Mar 18 14:27 C03232.S48384.13434.tar
-rw-r--r-- 1 theia theia 6 Mar 18 14:27 C03232.s48384.13434.tar
-rw-r--r-- 1 theia theia 6 Mar 18 14:27 c03232.s48384.13434.tar
$ ./movefiles.ksh
+ FILES=/home/region/files
+ client=reg
+ ERROR=/home/region/files/error/
+ mkdir -p /home/region/files/error/
+ cd /home/region/files/reg
+ fn_noext=C03232.S48384.13434
+ [[ C03232.S48384.13434 = ~(E)[[:lower:]] ]]
+ [[ C03232.S48384.13434 = ~(E)[[:upper:]] ]]
+ [[ ! C03232.S48384.13434 = ~(E)[[:lower:]] ]]
+ mv -v C03232.S48384.13434.tar /home/region/files/error/
renamed 'C03232.S48384.13434.tar' -> '/home/region/files/error/C03232.S48384.13434.tar'
+ fn_noext=C03232.s48384.13434
+ [[ C03232.s48384.13434 = ~(E)[[:lower:]] ]]
+ [[ ! C03232.s48384.13434 = ~(E)[[:upper:]] ]]
+ [[ C03232.s48384.13434 = ~(E)[[:upper:]] ]]
+ [[ ! C03232.s48384.13434 = ~(E)[[:lower:]] ]]
+ echo 'not moving: C03232.s48384.13434.tar'
not moving: C03232.s48384.13434.tar
+ fn_noext=c03232.s48384.13434
+ [[ c03232.s48384.13434 = ~(E)[[:lower:]] ]]
+ [[ ! c03232.s48384.13434 = ~(E)[[:upper:]] ]]
+ mv -v c03232.s48384.13434.tar /home/region/files/error/
renamed 'c03232.s48384.13434.tar' -> '/home/region/files/error/c03232.s48384.13434.tar'
$ ls -lR /home/region/files/
/home/region/files/:
total 8
drwxr-xr-x 2 theia theia 4096 Mar 18 14:28 error
drwxr-xr-x 2 theia theia 4096 Mar 18 14:28 reg
/home/region/files/error:
total 8
-rw-r--r-- 1 theia theia 5 Mar 18 14:27 C03232.S48384.13434.tar
-rw-r--r-- 1 theia theia 6 Mar 18 14:27 c03232.s48384.13434.tar
/home/region/files/reg:
total 4
-rw-r--r-- 1 theia theia 6 Mar 18 14:27 C03232.s48384.13434.tar
$ ksh --version
version sh (AT&T Research) 93u+m/1.0.0-beta.2 2021-12-17