LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-06-2011, 03:18 AM   #1
daveant
LQ Newbie
 
Registered: Dec 2011
Posts: 9

Rep: Reputation: Disabled
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.
Old 12-06-2011, 04:15 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
2 members found this post helpful.
Old 12-06-2011, 10:45 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by daveant View Post
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.
 
Old 12-06-2011, 01:08 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
3 members found this post helpful.
Old 12-07-2011, 06:49 AM   #5
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
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

Last edited by asimba; 12-07-2011 at 07:48 AM. Reason: added code tag
 
Old 12-07-2011, 07:28 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by asimba View Post
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 ]
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to search and copy jpg to Another Folder. ajayan Linux - Newbie 9 09-23-2012 06:46 AM
shell script to copy files eduard Linux - Newbie 9 07-13-2011 03:26 PM
Noob questions: making script to copy files to another folder supernova88 Linux - Newbie 8 06-06-2011 01:52 AM
i need to copy files from one folder to another folder using shell anurupr Linux - Newbie 17 03-04-2010 09:07 AM
How to copy in a script files to a samba folder? Julianus Linux - Networking 1 10-09-2004 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration