Move file based on filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move file based on filename
# 1  
Old 05-07-2010
Move file based on filename

Hi All

I need a script to manipulate files based on a filename:

example filename: 66600_042706.pdf

the script will create a directory 66000 only if this directory is not existing. If that directory is existing it will just move the file to 66000/666000_042706.pdf

in addition, i want to make sure that when moving the filename to the directory and there's an existing file with the same filename, it will add a random digit e.g. 66000/66000_042706_123.pdf

thanks
# 2  
Old 05-07-2010
Code:
#!/bin/bash
FILE=66600_042706.pdf
DIR=${FILE%%_*}
if [ -d $DIR ]
then
    if [ -f $DIR/$FILE ]
    then
        NEWFILE=${FILE%.*}_$RANDOM.${FILE##*.}
    else
        NEWFILE=$FILE
    fi
else
    mkdir $DIR
fi
mv $FILE $DIR/$NEWFILE

# 3  
Old 05-07-2010
Code:
filename="kamaraj_123.txt"
dir_name=`echo $filename | cut -d_ -f1`
echo $dir_name
if [ -d $dir_name ]; then
echo "there"
       if [ -f $dir_name\/$filename ]; then
        mv $filename $dir_name\/$file_name$RANDOM
    else
        mv $filename $dir_name\/$file_name
    fi
else
    mkdir $dir_name
    cp $filename $dir_name/
fi

# 4  
Old 05-08-2010
Quote:
Originally Posted by frans
Code:
#!/bin/bash
FILE=66600_042706.pdf
DIR=${FILE%%_*}
if [ -d $DIR ]
then
    if [ -f $DIR/$FILE ]
    then
        NEWFILE=${FILE%.*}_$RANDOM.${FILE##*.}
    else
        NEWFILE=$FILE
    fi
else
    mkdir $DIR
fi
mv $FILE $DIR/$NEWFILE

awesome. thanks! Now how do i replace FILE to make it a variable knowing that the the filename convention is NNNNN_XXXX.pdf

also how do it do it when say the filename is 66000_INS.pdf
and i want this to be filed into 66000/INS/66000_INS.pdf

thanks again
# 5  
Old 05-08-2010
Quote:
Originally Posted by aemestech
Now how do i replace FILE to make it a variable knowing that the the filename convention is NNNNN_XXXX.pdf
FILE is a variable, i just initalized it at the beginning to have an example value.
Now you can :
Replace 1st line with FILE=$1, to have the ability to call the script like
Code:
./script 66600_042706.pdf

or put the code in a loop like :
Code:
for FILE in *.pdf
do
  .....
done

---------- Post updated at 10:21 ---------- Previous update was at 10:15 ----------

Second requirement :
Just replace
Code:
DIR=${FILE%%_*}

by
Code:
DIR=$(echo ${FILE%.*} | sed 's|_|/|g')

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find file that matches today's date in filename and move to /tmp in bash

I'm having problems with my bash script. I would like to find a file matching today's date in the filename, i.e. my_file_20120902.txt and then move it to a different directory, i.e. /tmp. Thanks. (1 Reply)
Discussion started by: jamesi
1 Replies

2. UNIX for Dummies Questions & Answers

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (7 Replies)
Discussion started by: johannd
7 Replies

3. Shell Programming and Scripting

Move txt file to with current date appended to filename

I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>" So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (1 Reply)
Discussion started by: johannd
1 Replies

4. Shell Programming and Scripting

Move all files from source to destination directory based on the filename

Move all files starting with a specific name to different directory. This shell script program should have three parameters File Name Source Directory Destination Directory User should be able to enter ‘AB_CD*' in file name parameter. In this case all the files starting with AB_CD will... (1 Reply)
Discussion started by: chetancrsp18
1 Replies

5. Shell Programming and Scripting

Move files based on date in filename

I know this gets covered quite a bit in the forum and I think there is enough there for me to figure out how to do what I am trying to do, I just don't think I would do it very efficiently so I am going to ask the question... I have database log files with date and time stamps in the file like ... (7 Replies)
Discussion started by: slatoms
7 Replies

6. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

7. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

8. UNIX for Dummies Questions & Answers

How to copy/move to a file with a special character as the 1st char in the filename?

I am trying to create files with special characters in its filenames for testing purposes. This is on a Linux RHEL4 but this should also be applicable on a Unix shell. I am able to create files with special characters in the filenames...e.g. cp -pv foo.gif \*special.gif cp -pv foo.gif \... (6 Replies)
Discussion started by: sqa777
6 Replies

9. UNIX for Dummies Questions & Answers

how to move files into different folders based on filename

I need to move a bunch of files into folders that have the same name. I wanted to either do this with some filter command or some type of batch file that I could save that would already include all of the mv commands since I will have to do this process often. Whatever method you think is easier. ... (7 Replies)
Discussion started by: italia5
7 Replies
Login or Register to Ask a Question