• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

shell script : moving files to other folder after grep fiter

i have some hundreds of file  in directory of our application

each file has  2/3 file extention in directory,
example :

file01.ext1,
file01.ext2
file01.ext3
----

to simply which file, have how many counts , I am using below script

ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3'

The above shell script  which display output in below format, for the current directory files..... working well.
---- O/P from the above  script -------------

3    file01
3    file02
2    file03
----------------

where

Column-1,    file count of its different file extension
column-2,    is filename,

now ...I need

i want to move files into some directory, which  has count=3


please advice the needed enhancement
0
mac_g
Asked:
mac_g
  • 5
  • 2
  • 2
  • +1
6 Solutions
 
omarfaridCommented:
try this

for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3' | grep -x 3 | awk '{ print $2 }'`
do
    mv $file.* targetdir
done
0
 
simon3270Commented:
The lines with three extensions are already selected by the awk, so you don't need the "grep -x" command.

for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3{print $2}'`
do
    mv $file.* targetdir
done
0
 
omarfaridCommented:
The grep is based on posted output:


3    file01
3    file02
2    file03
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
mac_gAuthor Commented:
i have created directory name -temp, in same location

altered your script  to
-----
for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3' | grep -x 3 | awk '{ print $2 }'`
do
    mv $file.*  ./temp
done
----

no files has moved ..
any idea how to fix this issue
0
 
simon3270Commented:
Yes, remove the "grep -x 3" - the "-x" matches an entire line, and since none of the lines contain just a "3", none match.
0
 
simon3270Commented:
@omarfarid - it was confusing, but the posted output was of the command up to the "uniq -c" - it didn't include the "awk '+$1 == 3'" bit.
0
 
TintinCommented:
for f in $(ls |awk -F"." '{print $1}'|uniq -c|awk '$1==3 {print $2}')
do
   echo "Moving files starting with $f"
   mv $f*  /some/dir
done

Open in new window

0
 
simon3270Commented:
One small comment on the accepted answer - you need the dot after the filename in the "mv" command (as I had in my answer #a40003432)
for file in `ls |awk -F"." '{print $1}'|uniq -c | awk '+$1 == 3{print $2}'`
do
    mv $file.* targetdir
done

Open in new window

If you have, for example:
    file1.ext1
    file1.ext2
    file1.ext3
    file10.ext
in your input directory, the accepted answer would copy the file10.ext1 file, because it matched "file1*", even though there was only one file with the file10 prefix.
0
 
TintinCommented:
Well picked up Simon.
0
 
mac_gAuthor Commented:
tintin...

I did not see while choose the solution ..

apologies ... you deserve most
0
 
simon3270Commented:
No problem.  Only spotted it because I'd been hit by the same bug recently!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now