Compare filename to file folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare filename to file folder
# 1  
Old 03-28-2012
Compare filename to file folder

Hi. I have a problem, could you help me ?
Situation:
there are alot of files in directory_1, with the names like "notneeded1blabla.bla", "notveryneededblabla.gla".
there is directory_2 that has directories with names like that: "bla_needed1", "blab_veryneeded".
What i need so much is to place all files that contains
"needed1" to put in "bla_needed1" directory and
"veryneeded" -> "blab_veryneeded" directory

i've found alot of scripts that compare characters in one string to another but nothing that suites my case.
# 2  
Old 03-28-2012
Try this

Please test this before using in the real world.

Code:
 
#!/bin/ksh
DIR1=dir1
DIR2=dir2
MATCHSTR1=str1
MATCHSTR2=str2
#MATCHST1 and MATCHSTR2 should be exclusive of each other.
#meaning when searching for MATCHST1, MATCHSTR2 should not come.
`mv *${MATCHSTR1}* DIR1`
`mv *${MATCHSTR2}* DIR2`
exit 0

# 3  
Old 03-29-2012
Thanks but I already have a script like this. Its not exactly what i am looking for.

There are alot of those different directories, more than 150 and it keeps growing. Manually adding DIR and MATCHSTR is not what i need. Automating such work should be the best solution, and thats what i am looking for. Unfortunately i am no programmer so i can't make a script like this.

Script should look at the file name then look at directory and if it found matches for string that contains 8-10 characters in a raw put that file in the respective directry and repeat for another file, if directory not found search for another one until found.
# 4  
Old 03-30-2012
Try the given code

Please make sure to test this before using it in live environments.

Code:
 
#!/bin/ksh
SRCDIR=./src
DSTDIR=./dest
for matchstr in `ls -lrt ${DSTDIR} | grep ^d | awk '{ print length($9) "," $9; }' | sort -r -k 1,1 | cut -d"," -f2`
do
  for fil in `ls ${SRCDIR}`
  do
    ismatch=`echo ${fil} | grep -c ${matchstr}`
    if [[ ${ismatch} -gt 0 ]]
    then
      mvcmd="mv ${SRCDIR}/${fil} ${DSTDIR}/${matchstr}/"
      `${mvcmd}`
    fi
  done
done
exit 0

This User Gave Thanks to arun.ijhs.klm For This Post:
# 5  
Old 03-30-2012
Alternatively test and try this:
Code:
for dir in directory_2/*/
do 
  dir=${dir%/}
  for file in directory_1/*"${dir##*_}"*
  do 
    echo mv "$file" "$dir"
  done
done

If satisfied with the results then you can test and remove the echo statement
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-30-2012
@Scrutinizer: Good compact code.

Please find two observations below.

Issue 1:
Let us say there are two directories in the destination with the below names.
TEST
RETEST

And two files in source directory with the file names 'DoTEST.001' and 'DoRETEST.001'

If the first pattern check is TEST, both the files would be moved to TEST directory. Ideally 'DoRETEST.001' should be moved to RETEST directory.

Issue 2:
There is a limit to the number of characters that an unix command can contain.

If there are multiple files matching the pattern and the command (mv command) length exceeds the limit, it would go into error.
# 7  
Old 03-30-2012
Thank you..
Issue 1: Then one could try Do"${dir##*_}".* for example, the OP would need to improvise a little, or if possible, use distinct directory names (like in the OP's post)..
Issue2: Yes, that is why the script is moving 1 file at a time. If you are referring to "for", this is a shell keyword, there is no limit...

Last edited by Scrutinizer; 04-02-2012 at 06:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find all pdf an get make a folder from filename substring

Hi , i need your advice. i will find all *.pdf files and make a folder for every different prefix of file names. for example: test_21424234.pdf new_242342.pdf at the and i will that i create ( if not exits ) a new folder "test" and "new" , afterwards i will move the file in this new... (3 Replies)
Discussion started by: Maxwill
3 Replies

2. UNIX for Dummies Questions & Answers

How to find a filename of a folder?

Hi, Please help me i have a file directory in .txt. inside this directory is a list of names of folders to find inside in a folder. to make more easy to understand below is my list inside .txt: foldernameA foldernameB foldernameC I need to check if those folder names are inside... (1 Reply)
Discussion started by: cmarzan
1 Replies

3. Shell Programming and Scripting

Compare files in a folder based on another file

I have a file named file.txt that looks as follows //class1.txt 45 234 67 89 90 //class2.txt 456 34 78 89 120 class1 and class2.txt are the names of files in a folder named folder1. The content of class1.txt file in folder1 67 9 89 5 234 9The content of class2.txt file in... (1 Reply)
Discussion started by: jaff rufus
1 Replies

4. Shell Programming and Scripting

Compare text file and a folder

Hi, I have a folder which contains some files a1.txt a2.txt a3.txt a4.txt a5.txt a6.txt a7.txt a8.txt a_index.txt Also i have a text file which contains entries like this a1.txt a3.txt a7.txt I want to comapare this text file and folder and remove the similar values in... (12 Replies)
Discussion started by: arijitsaha
12 Replies

5. Red Hat

Need to compare two folder / FS permissions

Hi, Need to compare two folders / File systems permissions & time stamps including sub folders and files. Kindly let me know if any commands available. Regards :: VM (1 Reply)
Discussion started by: novaothers
1 Replies

6. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

7. Shell Programming and Scripting

Checking files in folder using starting string for filename

Hi, How do i check if there are any files present in the folder with some specific starting string. For eg :- I have used this where Source_File is filename parameter. if then return 2 fi But in my case the source file name is not constant. The only constant thing is... (10 Replies)
Discussion started by: chetancrsp18
10 Replies

8. UNIX for Dummies Questions & Answers

Jar/Tar to a diffent folder/same folder w/ filename

Hi, I want to extract myfile.war to a folder which is in the same folder with war file.I did this as normal: jar -xvf myfile.war But it exploded all the content of file to the same level folder instead of that I was expecting to create a folder called myfile. This works with tar: ... (0 Replies)
Discussion started by: reis3k
0 Replies

9. Shell Programming and Scripting

compare two files using filename

I want to compare two file using filename. suppose two files are same directory. what is the command for this. thanks in advance... (3 Replies)
Discussion started by: rinku
3 Replies

10. UNIX for Dummies Questions & Answers

How to compare the difference between a file and a folder??

Hi, I have a .txt file which has to be compared with a folder and print the difference to some other .txt file. I did try with the diff command..i mean diff /tmp/aaa/bbb.txt /space/aaa/bbb/ /***bbb.txt contains all the files names which may or may not exist in the folder bbb..so i need... (2 Replies)
Discussion started by: kumarsaravana_s
2 Replies
Login or Register to Ask a Question