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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to sort files into folder according to a string in the filename
# 1  
Old 01-31-2012
Debian 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: /test/testfiles
Files:
data20120106_101_015118.unl.bz2
data20120107_101_016118.unl.bz2
data20120108_101_015618.unl.bz2
data20120109_101_015118.unl.bz2
data20120109_101_015118.unl.bz2
data20120109_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120110_101_015118.unl.bz2
data20120111_101_015118.unl.bz2
data20120111_101_015118.unl.bz2
data20120112_101_015118.unl.bz2
data20120112_101_015118.unl.bz2

I need to create a folder e.g: 20120110 and then move all files containing 20120110 e.g data20120110_101_015118.unl.bz2 into the created folder. I have to do this for several
files as far back as 2010. All files have the same filename format as above.

Thanks for your help.
# 2  
Old 01-31-2012
Try this (not tested):
Code:
cd /test/testfiles
for i in data*.unl.bz2
do
   data_ymd=${i%%_*}
   ymd=${data_ymd#data}
   if [ ! -d $ymd ]; then
      mkdir $ymd
   fi
   mv $i $ymd
done

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simply Bash Script to Confirm that there are 2 Files in Same Folder

Im looking for 2 Files -> *.WAV and *.wav with find . -name 'unavail.wav' -o -name 'unavail.WAV' I need to list the Folders that contains these 2 Files? Thx in advance (13 Replies)
Discussion started by: valdez
13 Replies

2. UNIX for Beginners Questions & Answers

Sort all files in one folder

Could it be possible to sort all of files in a folder and add z- to the name of the sorted file? The names of the files to be sorted before the files are sorted AC-FOUR-136-ZEL2-ZECO-111 AC-SEVEN-56-ZEL4-ZECO-68 AC-NINE-994-ZEL3-ZECO-811 AC-ONE-4-ZEL1-ZECO-544 The names of the files to... (18 Replies)
Discussion started by: Eve
18 Replies

3. Shell Programming and Scripting

Bash script read specific value from files of an entire folder

Hello, I heva a problem creating a script that read specifc value from all the files of an entire folder I have a number of email files into a directory and i need to extrect from each file 2 specific values. After that i have to put them into a new file that looks like that: To: value1 ... (1 Reply)
Discussion started by: ahmenty
1 Replies

4. UNIX for Dummies Questions & Answers

Bash script to sort files

I've got a disorganized list of items and quantities for each. I've been using a combination of grep and sort to find out how much to buy of each item. I'm tired of having to constantly using these commands so I've been trying to write a shell script to make it easier, but I can't figure out how... (3 Replies)
Discussion started by: PTcharger
3 Replies

5. Shell Programming and Scripting

Sort log files based on numeric value in the filename

Hi, I have a list of log files as follows: name_date_0001_ID0.log name_date_0001_ID2.log name_date_0001_ID1.log name_date_0002_ID2.log name_date_0004_ID0.log name_date_0005_ID0.log name_date_0021_ID0.log name_date_0025_ID0.log .......................................... (4 Replies)
Discussion started by: alex2005
4 Replies

6. Shell Programming and Scripting

URGENT!!! 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

Sort files by date in filename

Hi, I am a newbie to shell programming and I need some help in sorting a list of files in ascending order of date in the filenames. The file format is always : IGL01_AC_D_<YYYYMMDD>_N01_01 For example, in a directory MyDirectory I have the following files: IGL01_AC_D_20110712_N01_01.dat... (11 Replies)
Discussion started by: Yuggy
11 Replies

8. 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

9. UNIX for Dummies Questions & Answers

sort files by numeric filename

dear all, i have .dat files named as: 34.dat 2.dat 16.dat 107.dat i would like to sort them by their filenames as: 2.dat 16.dat 34.dat 107.dat i have tried numerous combinations of sort and ls command (in vain) to obtain : 107.dat 16.dat 2.dat 34.dat (1 Reply)
Discussion started by: chen.xiao.po
1 Replies

10. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies
Login or Register to Ask a Question