LinuxQuestions.org
Review your favorite Linux distribution.
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 08-11-2010, 02:34 PM   #1
Pocho
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Rep: Reputation: 0
Bash file using ls -l and grep


Hello everyone, I'm trying to do some homework for my Linux class and i ran into a problem, we are supposed to write a bash file that will list files created on a user given date, send the output to a file call delete.list and after that give u the option of erasing said list of files, so far i have

echo "Enter Date"
read answer

ls -l | grep '$answer > delete.list

echo "would you like to erased this files Y/N")
read answer2

if [ $answer2 = Y ]; then
do
ls -l | grep '$answer' | xargs rm -f (this is the one that is giving me an error)
else
echo " have a good day"
fi

i've been searching the internet and there is a whole lot of different info in how to search for multiple files and erased them but i havent been able to find one that lets me search by date and erased it.

the error im getting is something along the lines of "rm --w not a valid option" or something like that.

I would appreciate any help, thx in advance
 
Old 08-11-2010, 02:40 PM   #2
nicedream
Member
 
Registered: Feb 2010
Distribution: Arch Linux
Posts: 68

Rep: Reputation: 19
I don't want to give you too much information, since homework is supposed to be figuring things out for yourself. But I am pretty sure I can see what the problem is, and I suggest you take a look at the actual contents of your delete.list file.
 
Old 08-11-2010, 03:04 PM   #3
smilemukul
Member
 
Registered: Jun 2009
Distribution: Redhat,CentOS,Ubuntu,Puppet
Posts: 292

Rep: Reputation: 34
You have to remove the bracket from the below statement

echo "would you like to erased this files Y/N")

Thanks
 
Old 08-11-2010, 03:06 PM   #4
nicedream
Member
 
Registered: Feb 2010
Distribution: Arch Linux
Posts: 68

Rep: Reputation: 19
Quote:
Originally Posted by smilemukul View Post
You have to remove the bracket from the below statement

echo "would you like to erased this files Y/N")

Thanks
I think that was just a typo in his post. Take a look at the error message he posted. The parentheses would have nothing to do with his error.
 
Old 08-11-2010, 03:13 PM   #5
Pocho
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Original Poster
Rep: Reputation: 0
yeah everything runs find the only line that gives me the error the that one ls -l | grep '$answer' | xargs rm -f , Nicedream i'll have to check that file when i get home, but if i remember correctly it was pulling the files i was looking for, i used a touch -t 200809100100 1 2 3 4 to create 4 files with the date of 2008-09-10 and when i ran the bash and input that date they were print to the file, i'll double check it tho to make sure =)
 
Old 08-11-2010, 06:16 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
The greatest issue will be that
Code:
grep '$answer'
will actually grep for the string $answer, not the content
of the variable $answer (that's because of your single quotes).

How that would result in grep spitting a "--w" at the rm is
beyond me, though.



Cheers,
Tink
 
Old 08-11-2010, 06:44 PM   #7
jrushby
LQ Newbie
 
Registered: Jul 2009
Location: Melbourne, Australia
Distribution: RHEL 4, RHEL 5, Ubuntu, Xubuntu, Puppy
Posts: 3

Rep: Reputation: 0
As nicedream has said, the content of the delete.list file is going to be the cause of the --w error message.

Also, seeing as this is homework, I would think your teacher is expecting you to make use of the delete.list file in some way, and not just create it. Maybe displaying the content to the user, or using its content to pass to xargs and rm?

Cheers,
JimbO
 
Old 08-11-2010, 06:47 PM   #8
nicedream
Member
 
Registered: Feb 2010
Distribution: Arch Linux
Posts: 68

Rep: Reputation: 19
If you do an ls -l command, you get not only filenames, but also timestamps, file sizes, and permissions (among other things).

Using grep to find patterns will return the entire line that contains that pattern. In other words, everything that ls -l spits out.

You can't just pipe that entire ls -l output to rm, because rm only takes the filename as an argument. I'll bet that the "--w" error coming from the rm command is the result of grep passing the file permissions as arguments to rm.



Quote:
Originally Posted by Tinkster View Post
The greatest issue will be that
Code:
grep '$answer'
will actually grep for the string $answer, not the content
of the variable $answer (that's because of your single quotes).

How that would result in grep spitting a "--w" at the rm is
beyond me, though.



Cheers,
Tink
 
Old 08-11-2010, 06:49 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by jrushby View Post
As nicedream has said, the content of the delete.list file is going to be the cause of the --w error message.

Also, seeing as this is homework, I would think your teacher is expecting you to make use of the delete.list file in some way, and not just create it. Maybe displaying the content to the user, or using its content to pass to xargs and rm?

Cheers,
JimbO

Not sure about that ... if what the OP posted above is
the actual script I'd expect it to never run to completion
because it's missing a single quote after the first $answer.

If that was an omission I'd expect the file to be empty.

Neither case should produce an output of "--w".



Cheers,
Tink
 
Old 08-11-2010, 06:58 PM   #10
nicedream
Member
 
Registered: Feb 2010
Distribution: Arch Linux
Posts: 68

Rep: Reputation: 19
Quote:
Originally Posted by Tinkster View Post
Not sure about that ... if what the OP posted above is
the actual script I'd expect it to never run to completion
because it's missing a single quote after the first $answer.

If that was an omission I'd expect the file to be empty.

Neither case should produce an output of "--w".

Cheers,
Tink

Yes, there are some problems with the quotes in there. But I would guess that he was playing around with adding a removing the quotes in different places (we've all done that when we were n00bs), and the version of the script he posted here was not the *exact* one that produced the error message.
 
Old 08-11-2010, 07:14 PM   #11
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
To delete the files you will perhaps want to use wildcards.
Kind of
Code:
if [[ $answer1 = "yes" ]]
then
   for i in *"$answer0"*
   do
        rm "$i" 
   done
fi
btw usage of ls:
http://mywiki.wooledge.org/BashPitfa...0ls_.2A.mp3.60

just two cents Or: Thats what i would do.

Last edited by j1alu; 08-11-2010 at 08:13 PM.
 
Old 08-11-2010, 08:05 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
Originally Posted by jrushby
Also, seeing as this is homework, I would think your teacher is expecting you to make use of the delete.list file in some way, and not just create it.
I agree as what happens if an important file is created in the directory you are listing in between the 2 times you call ls? The user may have approved the removal
of all files that are now contained in delete.list, but not any newly created files.

Also, the following points:

1. Your code makes no attempt to single out files only, so what about directories? If you run 'rm -f' on a directory you will get an error.

2. answer2 is requesting input from a user but you make no attempt to sanitize it. Also, what of the simple option of the difference between Y and y.

3. Also on answer2, what happens if the user accidentally or on purpose just presses enter? (ie variable is now not set)

4. echo "Enter Date" - what is the expected format?? Being from Australia I would enter - 12/08/2010 ... will this work?

Lastly, as you are new to the site, please use [code][/code] tags to enclose your code to make it more readable (see post #11 by j1alu)

Last edited by grail; 08-11-2010 at 08:07 PM.
 
Old 08-12-2010, 01:09 AM   #13
Pocho
LQ Newbie
 
Registered: Aug 2010
Posts: 4

Original Poster
Rep: Reputation: 0
thx for all ur answers guys, i can say that tonight i discover the beauty of keeping a copy of my Linux VM close by, i was following ur code j1alu and i copy as u wrote it, little did i know it was gonna erased everything on the working directory hehehe. Well i think i got it done just need to do some house keeping with the code but here is how it ended up looking

Code:
#!/bin/sh

echo "Please enter a date (yyyy-mm-dd):"
read answer

echo " "

ls -l | grep $answer > delete.list
ls -l | grep $answer

echo " "

echo "Would you like to erased this files (Y/N):"
read answer2

if [ $answer2 = 'Y' ];
then
	ls -l | grep $answer | awk '{print($8)} | xargs rm
	echo "All requested files have been erased"
else 
	echo "Bye"
fi
Please do let me know if anyone sees an issue with it, i tried it and it works, it erased all the files with the date i entered.
 
Old 08-12-2010, 02:19 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well my first 3 points still stand and you are still not using the file you created. Maybe you do not need to create it??
 
Old 08-12-2010, 03:07 AM   #15
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
grail makes some points that you should listen to.

Some basic control on what your script does should be added, at least to avoid accidental deletion of files. There are some potential problems with your script (and dangerous ones if you ask me, unless you are using this on a controlled environment).

A big big big problem is that you are using three lists of files because you calculate the list of files 'on the fly' at three different momments. We all know what does that mean on a multitasking OS, don't we? The output for the ls command at moment1 can be different from the output at moment2, and (what's much worse) at momment3.

As grail very well said, you are not using the contents of delete.list for anything. You just dump the output to that file, then forget about the file and never again use it.

There are some other problems in that script. I personally wouldn't ever parse the output of ls like that, for a good reason:

http://mywiki.wooledge.org/ParsingLs

You should also be checking if the item you are managing is a file (check the '-f' test condition in the bash man page), and, at least, you should be checking also that $answer is not an empty string '' nor a blank space ' '. You should also check explicitly that $answer2 is either 'y', 'Y', 'n' or 'N', and abort with an error in any other case (I hint to the 'case' keyword here).

A convenient way to solve most of the problems here would be to use the 'find' tool.

Code:
find . -type f -newermt 2010-08-12 ! -newermt 2010-08-13 -print0 | xargs -0 echo rm -f
This is the general idea for you to study. This serves a number or purposes. It find the files created on a given date (2010-08-12), it finds *only files*, and not symlinks, device nodes or directories, avoiding in the way a number of problems. It also uses nulls as separators so you won't have problems with files that have spaces or strange characters that could be caught by your shell.

A problem here would be how to calculate the specified date +1 day for the find command, though. The bash arithmetic capabilities are not the best around. Not impossible, but probably more complicated than you need it to be.
 
  


Reply

Tags
delete



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
[SOLVED] Bash: How to grep stdout? ryan858 Programming 4 06-05-2010 10:17 AM
bash script and grep inside file? agrinog Programming 3 04-02-2010 02:26 PM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
Bash: using file as input for grep/tail and then some.. sleipnir Programming 5 06-18-2009 04:51 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

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

All times are GMT -5. The time now is 07:52 PM.

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