Bash script needed to move files to another folder.
Greetings One and All,
This is my first post in this forum. Let me tell you what I am needing. I need a bash script that will look at the 6th and 7th characters of a filename and if it is equal to a set number, then it will be moved to a specified folder. Here's an example of what I'm talking about: If I have a listing of files 2009-12-18.fictitious.gz 2009-11-01.notreal.gz 2009-02-22.fake.gz and I want to move 2009-12-18.fictitious.gz to the December folder, since the 6th and 7th digits of the filename = 12, then I would like it to create the December folder if it doesn't exist and move the file to that directory. Likewise, I want to move 2009-11-01.notreal.gz to the November folder and have it create the folder if it doesn't exist, etc. Anyone up to the challenge? Also, can all the .gz files for the month go into one big tarball? |
Well you can get the sixth and seventh character using cut, awk, sed, etc...
Code:
#!/bin/bash |
As usual with linux, there are many possible solutions.
I think it might be easier to create the January ... December folders yourself instead of writing a script to do it. Then you could just do Code:
mv 2009-12* /path/to/December You could then copy your command history to a file with history > foo and then edit foo to turn it into a script you can run next year to save yourself creating all those monthly directories again. Quote:
Welcome to LQ! |
rweaver,
Thank you for such a quick turn! It seems pretty straight forward and simple enough. I will have to make a modification since all the files in that directory don't begin with the date, but a simple find statement should be enough. I will try to finish writing it, and test it for my December files later on today to see if it works. Thanks once again! |
tredegar,
I had contemplated just creating the monthly folders myself, too, but I have like 10 servers that this script will need to run on, so I was hoping to put the script in a cron job and let it work its magic. |
Quote:
Test your script. When you are happy with it, copy the script to your servers. |
rweaver,
I have completed the proposed script you gave me, and I am getting a Code:
./monthly_logs.sh: line 52: syntax error near unexpected token `done' Code:
done Code:
#!/bin/bash |
Quote:
Code:
case $SS in Edit: I think if you are going to use "-p" with mkdir, you should not have the leading slash before /month as otherwise the dir month will be made at /. you'll have to experiment! |
tredegar,
I like your approach to this. It is clean and should eliminate the else if statements that seem to be causing me a little grief in scripting correctly. I will go back to the drawing board and see what happens. Thanks for your help! |
There's no need to use external tools like cut or awk to extract the substring. All you need is bash's built-in parameter substitution.
Also $() is recommended over ``. Code:
for i in $(ls $DIR); do Code:
if [ value to test ]; then Code:
for i in $(ls $DIR); do |
Quote:
bash-scripting is not my strong point, so I like to learn too ;) |
David the H.,
Quote:
So where I would normally do a: Code:
for i in $DIR ; do `find . 2* -name -type f -print` Code:
$(find . 2* -name -type f -print) |
That's true. I did have more to add. I was mostly just complaining about all the time I took writing up the case statement, only to find someone beat me to it.
But this stuff happens all the time, so no worries. :) I see that I also failed to point out that there's no need for the embedded ls command (use of ls in loops is considered by many to be very ungraceful). Bash can read the directory listing directly; just use a * wildcard in the directory path. You can use variable substitution to provide the path itself. Code:
for i in $DIR/*; do # or specify a matching pattern |
Embedded commands work similarly to variable substitution. Any commands placed inside will be executed first, and their output substituted before the line containing it is executed.
http://www.tldp.org/LDP/abs/html/commandsub.html Backticks (``) are a very old way to use embedded commands in bourne-type shells. $() is a newer form of the same thing. I say newer, but it's been around long enough now that, unless you work on very old legacy systems, you shouldn't have any problems using it. This page enumerates the reasons the newer form is preferred. http://mywiki.wooledge.org/BashFAQ/082 |
David the H.,
Thank you for clarifying that for me, and I will bear that in mind in future scripting efforts. As it stands, everything is working as it should, but I neglected to create a find statement to grab just the '2*.gz' files in the directory and then utilize the parameter substitution you pointed out earlier. So I've just created that statement, and here's what I have: Code:
for i in $DIR ; $(find . '2*.gz' -name -type f -print); do case "${i:5:2}" in Code:
syntax error near unexpected token `$(find . '2*.gz' -name -type f -print)' |
All times are GMT -5. The time now is 01:31 PM. |