Script to parse filenames in a directory and do some actions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to parse filenames in a directory and do some actions
# 1  
Old 03-01-2010
Question Script to parse filenames in a directory and do some actions

Hi All,

I don't have much experience in scripting, and couldn't find anything that will help me to write a script I need, hopefully you can help me with it.

I have lots of files in one directory with the following file name pattern:
Code:
100001-something.ext1
100101-something2.ext2
100002-something3.ext1

etc.

I need to:
1. Loop through all the files in this directory,
2. Parse every file name and extract a number every file name starts with,
3. Check if there is a dir with this name already exists within this dir
4. If the dir doesn't exist then create it
5. Move parsed file to the create dir.

Thank you very much!

Last edited by radoulov; 03-01-2010 at 07:59 AM.. Reason: Added coda tags.
# 2  
Old 03-01-2010
You use the following script to parse the file names and move them to the directory.

Code:
check=`find -maxdepth 1 -type f `
for file in $check
do
  dir=`echo $file | egrep  "[0-9]+.*$" | cut -d '-' -f 1`
  if [[ $dir != "" ]];then
    if [ -d $dir ]; then
      echo "$dir Directory Already Available"
      mv $file $dir
    else
      mkdir $dir
      mv $file $dir
    fi
  fi
done


Last edited by Franklin52; 03-01-2010 at 08:07 AM.. Reason: Please indent your code, thank you
# 3  
Old 03-01-2010
If you don't have underlying directories:

Code:
for file in *
do
  dirname=${file%-*}
  mkdir -p ${PWD}/dirname
  mv "$file" ${PWD}/dirname
done

# 4  
Old 03-03-2010
Thank you very much for your replies!

Since some subdirectories can already be in the directory, I used script from Nila and it worked like a charm!

Regards
Roman
# 5  
Old 03-03-2010
Quote:
Originally Posted by troman
Since some subdirectories can already be in the directory...
No problem if you use the -p option of mkdir, from the man page:

Quote:
-p, --parents
no error if existing, make parent directories as needed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing filenames in my current directory

Can someone give me a tip on writing a script that, for each file in the working directory, prints the filename, the # of lines, and the # of words to stdout? (2 Replies)
Discussion started by: flash123
2 Replies

2. Shell Programming and Scripting

Script to compare substrings of multiple filenames and move to different directory

Hi there, I am having trouble with a script I have written, which is designed to search through a directory for a header and payload file, retrieve a string from both filenames, compare this string and if it matches make a backup of the two files then move them to a different directory for... (1 Reply)
Discussion started by: alcurry
1 Replies

3. Shell Programming and Scripting

[awk] print all filenames in directory

Hello, I was given a (I suppose) a simple task which I don't know how to do. Before coming here I have read a dozen of awk tutorials (full read, not just line-skipping) but I still don't know how to do this. Task: Write an script file 'check.awk' with a parameter current directory that... (5 Replies)
Discussion started by: c0dehunter
5 Replies

4. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

5. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

6. Shell Programming and Scripting

parse apl-numeric codes from filenames, and match them to entries in database

Hello, I am new to Unix scripting, and would like some help with my issue: I have vairous files having some alphanumeric codes in them e.g. 10000-01 34440TE 34590SR All these codes are stored in the database, and I need to parse these codes out of these filenames, and match them... (2 Replies)
Discussion started by: mvaidya
2 Replies

7. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

8. Shell Programming and Scripting

concatenating the filenames in a directory

hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code to do this var1='' for filename in $_DIR/${FILE_NAME}* do if if then... (7 Replies)
Discussion started by: nvuradi
7 Replies

9. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies

10. AIX

Script to perform some actions on multiple files

I have this Korn script that I wrote (with some help) that is run by cron. I basically watches a file system for a specific filename to be uploaded (via FTP), checks to make sure that the file is no longer being uploaded (by checking the files size), then runs a series of other scripts. The... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question