Script move files by name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script move files by name
# 1  
Old 03-10-2012
Script move files by name

hi,

I have a lot of files named xxxxx__AA.txt, xxxxx__BB.txt, xxxxx__CC.txt and I would like to move xxxxx__AA.txt in AA directory, xxxxx__BB.txt in BB etc. Could you help me do it in bash script?
# 2  
Old 03-10-2012
Since you are dealing with lots of files lets sort them out first, so you can validate what the code will do. This makes a script in /tmp. READ the script first to be sure mv is not overwriting something. Change the code below to match the directory name.

Code:
#!/bin/bash

SCRIPT=/tmp/mv_script.sh
cd /home/jim                      # change this path 
echo "cd `pwd`" > $SCRIPT

ls | awk '{
          if( $0~/_[A-Z]{2}.txt$/)
             {                  
                   f=substr($0,length($0)-5,2)                   
                   printf("[ -d %s ] || mkdir %s\n", f, f)
                   printf("mv %s ./%s/%s \n", $0, f, $0)            
             }               
        } ' >> $SCRIPT
chmod +x $SCRIPT

# 3  
Old 03-11-2012
Code:
#!/bin/bash
while read i
do
     mkdir $(echo ${i:6:2}) && mv $i $(echo ${i:6:2})
done < <(ls |awk --posix '$0~/[A-Z]{2}.txt$/')


Last edited by Franklin52; 03-11-2012 at 07:05 AM.. Reason: Please use code tags for data and code samples, thank you
# 4  
Old 03-11-2012
Try shell-only:
Code:
printf "%s\n" *__*.txt | 
while IFS='_.' read name x kind suffix
do
  mkdir "$kind" 2>/dev/null
  mv "${name}__${kind}.${suffix}" "$kind"
done

# 5  
Old 03-11-2012
Thanks a lot! I tried all of them but i could not do working. Could you help me please.
# 6  
Old 03-11-2012
Hi Corfuiti,

Can you supply the directory structure, from the directory at the top level. If the files are at the level above the directories, this is quite straight forward using something like find.

Regards

Dave
This User Gave Thanks to gull04 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to move certain no. of files every second

Hi All, I am new to Linux/Scripting and need some assistance in coming up with a script that can move certain amount of files from one directory to other every seconds. Usercase: We have around 100k files in tmp directory on my server which needs to be moved to another folder to get... (3 Replies)
Discussion started by: Raj1184
3 Replies

2. Shell Programming and Scripting

help with a script to gzip/move files

Hi Please can you help me in writing a script to find files on a specific directory, and of extension "tap" but only of the month of september, gzip and move them to another directory. Your help will be appreciated. (4 Replies)
Discussion started by: fretagi
4 Replies

3. Shell Programming and Scripting

Need shell script to move files

Hi , I need a simple shell script to move the files from one directory to another directory after every 1 hour..!!! ?? (1 Reply)
Discussion started by: SARAL SAXENA
1 Replies

4. Shell Programming and Scripting

Need script to move files based on name

Hi folks, I'm new here and appreciate greatly any help. I have a bunch of files that need be moved and renamed. Fortunately, they are all in sequence... Present filename and path: /.catalog1/t76038_842-01 Move to: /.catalog1/76038-01 So, we need to drop the... (8 Replies)
Discussion started by: axslinger
8 Replies

5. Shell Programming and Scripting

Perl script to move files not in use

I need to write a script to move files only when they are not in use. I have a rudementry bash script for Linux but i need a perl script so it will work on Linux and hpux. Oracle writes files to a directory called /data and the files there are moved every 5 minutes to a new home. But i need to... (1 Reply)
Discussion started by: insania
1 Replies

6. UNIX for Dummies Questions & Answers

Script to move blank files

Anyone could give me an example of scrip to move blank files found into a dir? Thanks, Leandro Takeda (3 Replies)
Discussion started by: letakeda
3 Replies

7. Shell Programming and Scripting

Script to move trace files

Please debug this shell script for me.. Basically the idea is to run the script, based on the command to move some trace files to a separate directory and I am getting the error. Only the COMMAND that has rm {} works and I basically want to use it for the fourth one. Please try for the 2nd, 3rd and... (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question