[SOLVED] shell script to copy files into folder that matches the file name
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Click here to see the post LQ members have rated as the most helpful post in this thread.
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.
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.
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
Last edited by asimba; 12-07-2011 at 07:48 AM.
Reason: added code tag
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.