parsing file names and then grouping similar files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parsing file names and then grouping similar files
# 1  
Old 01-02-2010
parsing file names and then grouping similar files

Hello Friends,

I have .tar files which exists under different directories after the below code is run:

Code:
find . -name "*" -type f -print | grep .tar > tmp.txt

Code:
cat tmp.txt
./dir1/subdir1/subdir2/database-db1_28112009.tar
./dir2/subdir3/database-db2_28112009.tar
./dir3/application-app1_28112009.tar
./dir3/application-app2_21112009.tar
./dir4/application-app3_14112009.tar
./dir5/subdir4/ivr_backup-ivr1_21112009.tar
./dir6/subdir4/ivr_backup-ivr2_28112009.tar
.............

what i need is to group these .tar files under a spesific directory. I need to create a directory according to their descriptions like "database, application,etc" and place the related .tar files into this directories. If we consider "/" as delimeter i coudlnt parse the last part including file names. First i need to parse last parts with file name, then a second parsing in whole file name according to second delimeter "-" .

desired otput is

Code:
./database
db1_28112009.tar
db2_28112009.tar
./application
app1_28112009.tar
app2_21112009.tar
app3_14112009.tar
.....

Any help appreciated,
# 2  
Old 01-02-2010
Change cp to mv if you want to move your files.

Code:
find . -name '*.tar' | 
  while IFS= read -r; do
    d=${REPLY##*/} d=${d%-*}
    [ -d ./"$d" ] || mkdir -- ./"$d" || exit 1
    cp -- "$REPLY" "./$d/${REPLY#*-}"
  done

If your read builtin doesn't support the -r option, just remove it.

Last edited by radoulov; 01-03-2010 at 06:31 AM..
# 3  
Old 01-03-2010
Code:
cat tmp.txt | while read i;do
dir=${i%%-*}
if [ ! -d $dir ];then
mkdir $dir
fi
if [ ! -f $dir/$i ];then
cp $i $dir
fi
rm $i
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To group the text (rows) by similar columns-names in a file

As part of some report generation, I've written a script to fetch the values from DB. But, unluckily, for certain Time ranges(1-9.99,10-19.99 etc), I don't have data in DB. In such cases, I would like to write zero (0) instead of empty. The desired output will be exported to csv file. ... (1 Reply)
Discussion started by: kumar_karpuram
1 Replies

2. Answers to Frequently Asked Questions

Why Parsing Can't be Done With sed ( or similar tools)

Regularly we have questions like: i have an XML (C, C++, ...) file with this or that property and i want to extract the content of this or that tag (function, ...). How do i do it in sed? Yes, in some (very limited) cases this is possible, but in general this can't be done. That is: you can do... (0 Replies)
Discussion started by: bakunin
0 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. Shell Programming and Scripting

Finding files in directory with similar names

So, I have a directory tree that has many files named thusly: X_REVY.PDF I need to find any files that have the same X portion (which can be nearly anything) as any another file (in any directory) but have different Y portions (which can be any number from 1-99). I then need it to return... (3 Replies)
Discussion started by: Kamezero
3 Replies

5. Windows & DOS: Issues & Discussions

Issue: Unzipping file containing files/folders with a similar name

Hi, I have a zip file created on a Linxux server that I need to extract on a Windows machine... The zip file containing folders with the same name but they each have a different case, one if camel case and the other is just capitalised. When I extract using 7zip, I get prompted if I want to... (3 Replies)
Discussion started by: muay_tb
3 Replies

6. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

7. Shell Programming and Scripting

Merging two columns from two files with similar names into a loop

I have two files like this: fileA.net A B C fileA.dat 1 2 3 and I want the output output_expected A 1 B 2 C 3 I know that the easier way is to do a paste fileA.net fileA.dat, but the problem is that I have 10,000 couple of files (fileB.net with fileB.dat; fileC.net with... (3 Replies)
Discussion started by: valente
3 Replies

8. Shell Programming and Scripting

Script to move files with similar names to folder

I have in directory /media/AUDIO/WAVE many .mp3 files with names like: my filename_01of02.mp3 my filename_02of02.mp3 Your File_01of06.mp3 Your File_02of06.mp3 etc.... In the same directory, /media/AUDIO/WAVE, I have many folders with names like 9780743579490 9780743579491 etc.. Inside... (7 Replies)
Discussion started by: glev2005
7 Replies

9. Shell Programming and Scripting

Help with parsing mailbox folder list (identify similar folders)

List sample: user/xxx/Archives/2010 user/xxx/BLARG user/xxx/BlArG user/xxx/Burton user/xxx/DAY user/yyy/Trainees/Nutrition interns user/yyy/Trainees/Primary Care user/yyy/Trainees/Psychiatric NP interns user/yyy/Trainees/Psychiatric residents user/yyy/Trainees/Psychology... (4 Replies)
Discussion started by: spacegoose
4 Replies

10. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies
Login or Register to Ask a Question