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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
#!/bin/bash
filelist="/tmp/fileslist"
month=$(date +%m)
....
....
monthslist="01 02 03 04 05 06... 12"
for mymonth in $monthlist
do
ls -la $HOME/logfile.$mymonth* | awk -F" " '{print $9}' > $filelist
for file in `cat $filelist`
do
mv $file /path/to/directory
echo "Movement done for $file"
done
done
Now suppose a file don't exist for some month pattern, let's say logfile.07 doesn't exist, then script is displaying an error of "File logfile.07: No such file or dir.." on my screen. I just want to redirect such error, so it diplay only movement done echo message, not the errors. How can I redirect such error while i am already redirecting output of ls -la... command.
But actually, you seem to be going to a lot of roundabout trouble just to move a few files. Saving the output of ls -l to a file, then using awk to parse out the filenames again, and finally a for loop to move them one at a time. Seriously?
Is there any reason you can't simply do this?
Code:
echo 'Now moving the following files:'
printf '%s\n' $HOME/logfile.*
mv -t /path/to/directory $HOME/logfile.*
You can usually match the filenames you want with the proper globbing patterns. And the nice thing is you won't get any error messages unless there are no files that match at all (and even that can be controlled with the nullglob and failglob shell options).
If you need to be more specific and only match files with actual month extensions, you can use bash's extended globbing:
David, thanks for sharing such valuable info.
I am beginner in shell scripting, thus using simple commands, but knowledge is always limitless and your Wiki helped me a lot! Thanks agian.
Glad to help out, and I hope you didn't think I'm being too hard on you. It's natural for beginners to use "spaghetti logic" in their scripts, and I've been guilty of it myself. But I tend to be a bit blunt in my criticisms.
Just keep working at it, listen to advice, and follow the links I gave. That whole site is a treasure trove of advice. I particularly recommend working through their BashGuide.
One thing that particularly helps when posting here is to focus on explaining your final goal, as opposed to just the immediate problem at hand. There are often better ways to accomplish what you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.