LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script to copy files into folder that matches the file name (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-copy-files-into-folder-that-matches-the-file-name-917345/)

daveant 12-06-2011 03:18 AM

shell script to copy files into folder that matches the file name
 
Hi,

I am newbie in Linux. I need to write a shell scripts for copying the files that mathces the directory name. Eg. I am having files in a folder
1234_test.dat
1235_test.dat
1236_test.dat
....n

I want to move or copy these files into particular folder my folder structure would be 1234,1235,1236. Also if there is no folder with the file name(prefix) i neeed to generation one.

Pls help me to write a shell script

Thanks in advance

Dave

colucix 12-06-2011 04:15 AM

Hi Dave and welcome to LinuxQuestions!

It would be better if you'd show us what have you tried so far, so that we can be of help if you have specific questions. The script should do basically three operations for every file you want to move. A meta-code might be:
Code:

for file in *_test.dat
do
  1. Extract the number from the file name (for example using grep)
  2. Create the directory if it doesn't exist (mkdir -p could be useful in this case)
  3. Move the file to the directory
done

The grep command could be something as
Code:

echo $file | grep -Eo '^[0-9]+'
but the regular expression strongly depends on the actual format of the file names (I mean of all the files you want to move). Hope this helps a bit.

TB0ne 12-06-2011 10:45 AM

Quote:

Originally Posted by daveant (Post 4543245)
Hi,
I am newbie in Linux. I need to write a shell scripts for copying the files that mathces the directory name. Eg. I am having files in a folder
1234_test.dat
1235_test.dat
1236_test.dat
....n

I want to move or copy these files into particular folder my folder structure would be 1234,1235,1236. Also if there is no folder with the file name(prefix) i neeed to generation one. Pls help me to write a shell script

Please spell out your words. And we'll be glad to HELP you debug your script...but we're not going to write it for you. Post what you've written so far, and where you're stuck. Checking Google for shell-scripting guides brings up lots, like this: http://tldp.org/LDP/abs/html/

colucix gave you a good start, but in Linux, there are always multiple ways to do something...picking the best one is up to you and your situation.

David the H. 12-06-2011 01:08 PM

For the basic foundations of scripting, start by reading the bash guide:

Bash Guide

Also it's vital that you understand what an argument is, and how the shell processes whitespace:

Arguments
Word Splitting
Quotes

After that you can peruse these links for techniques on how to match filenames and process your text strings:

globbing
exended globbing
brace expansion
parameter substitution
string manipulation

Finally, look here for suggestions on avoiding problems and working around trouble spots:

Bash Pitfalls
BashFAQ

asimba 12-07-2011 06:49 AM

Quote:

for file in *_test.dat
do
1. Extract the number from the file name (for example using grep)
2. Create the directory if it doesn't exist (mkdir -p could be useful in this case)
3. Move the file to the directory
done
Number from file can be extracted using following - considering that first 4 letters are number.
Code:

echo ${string:0:4}  # gets first 4 letters
assign it to variable as need be
Code:

dat_file_num=${file:0:4}
rest else remains same.

Sorry I cannot use code tags - I have a browser configured for minimum funcionatlity and I cannot see any of those options up here

catkin 12-07-2011 07:28 AM

Quote:

Originally Posted by asimba (Post 4544184)
Sorry I cannot use code tags - I have a browser configured for minimum funcionatlity and I cannot see any of those options up here

You could manually enter code tags as (remove the spaces) [ CODE ] ... [ /CODE ]


All times are GMT -5. The time now is 08:29 AM.