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 |
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 Code:
echo $file | grep -Eo '^[0-9]+' |
Quote:
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 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 |
Quote:
Code:
echo ${string:0:4} # gets first 4 letters Code:
dat_file_num=${file:0:4} Sorry I cannot use code tags - I have a browser configured for minimum funcionatlity and I cannot see any of those options up here |
Quote:
|
All times are GMT -5. The time now is 08:29 AM. |