LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash file using ls -l and grep (https://www.linuxquestions.org/questions/linux-newbie-8/bash-file-using-ls-l-and-grep-825617/)

Pocho 08-11-2010 02:34 PM

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

nicedream 08-11-2010 02:40 PM

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.

smilemukul 08-11-2010 03:04 PM

You have to remove the bracket from the below statement

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

Thanks

nicedream 08-11-2010 03:06 PM

Quote:

Originally Posted by smilemukul (Post 4063350)
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.

Pocho 08-11-2010 03:13 PM

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 =)

Tinkster 08-11-2010 06:16 PM

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

jrushby 08-11-2010 06:44 PM

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

nicedream 08-11-2010 06:47 PM

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 (Post 4063514)
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


Tinkster 08-11-2010 06:49 PM

Quote:

Originally Posted by jrushby (Post 4063527)
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

nicedream 08-11-2010 06:58 PM

Quote:

Originally Posted by Tinkster (Post 4063532)
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.

j1alu 08-11-2010 07:14 PM

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.

grail 08-11-2010 08:05 PM

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)

Pocho 08-12-2010 01:09 AM

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. :)

grail 08-12-2010 02:19 AM

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??

i92guboj 08-12-2010 03:07 AM

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.


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