Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
parts = grep "/root/backups/*"

for part in $parts
do
    echo $part
    rm -rf $part #delete
done

I basically need to iterate through a folder and return every item in the folder using a Bash script.

Unable to get working via grep, although may be a much simpler way. First line is pure pseudo-no-clue-code.

share|improve this question
1  
You can use find if I'm understanding what you need to do it's simply... find /root/backup/* –  egorgry Dec 21 '10 at 20:03

4 Answers 4

up vote 3 down vote accepted

This can easily be done without looping.

If you just want to get rid of the files, just use rm:

$ rm -rf /root/backups/*

If you want to show the filenames as you delete them, use find instead:

$ find /root/backups -maxdepth 1 -mindepth 1 -print -exec rm -rf {} \;

The -maxdepth option tells find to not descend any further than one level from the starting point, /root/backups. The -mindepth option tells find where to start returning results, effectively telling find to ignore the the starting point. The -print option just prints the found files to the screen. With the -exec, the {} are replaced with the found files, so this calls rm to delete them.

EDIT: eliminated an unnecessary -exec per comment

share|improve this answer
    
Would -print and -delete work in place of your -exec's? –  Aaron Copley Dec 21 '10 at 20:48
    
You could use -print to replace the first -exec, but -delete will not work on directories that are not already empty. I think you'll still need the second -exec. –  SethG Dec 21 '10 at 20:58

You want a list of file names in a given directory. There is a program that does this, and you be embarrassed to read it's name: ls. Thats all it does! You want a recursive listing including files in subdirectories, so use ls -R. You can put the output of that into a variable in your script and do as you please with it.

share|improve this answer

Change the grep command to:

for i in `ls -bRC1 /path/* ` ; do echo $i ; rm $i ; done

The -b will escape spaces and other characters that may result in errors and unintended consequences with the rm. -R is recursive. -C1 is single column (which may be redundant). I would reconsider the -rf

You may want to use find instead of a for loop

find /path -H -maxdepth 3 -type f -mtime +7 -exec rm {} \; -ls

If you don't know what the above options mean, type man find.

share|improve this answer
    
for i in /path/* does everything except the recursion and works with filenames that have spaces. Here is some information regarding avoiding parsing ls. –  Dennis Williamson Dec 22 '10 at 0:52

grep -l will give you just the filenames

parts = grep -rl ....

share|improve this answer
    
grep is the wrong tool for this job. –  Dennis Williamson Dec 22 '10 at 0:52
    
i presumed there is some content he wants to look into it - yes grep is not doing any good –  silviud Dec 22 '10 at 1:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.