Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-16-2008, 06:25 PM   #16
LQ Newbie
Registered: May 2008
Posts: 7

Original Poster
Rep: Reputation: 0

GOTIT. I replaced both block1 and block2 with a single block1
#start of block 1
	for file in $arg/* $arg/.*;do
		if [[ ${file:(${#file}-1):1} == "." || ${file:(${#file}-2):2} == ".." ]];then
		elif [ -d "$file" ];then
			delete "$file"
		elif [ -r "$file" ];then
			echo "/bin/rm \"$file\"" >> delete_tmp
#end of block1
Old 05-18-2008, 08:09 AM   #17
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
hmm.. another mistake of mine.. since you already got it.. please ignore this message


i love squeezing logics in a single for loop but i can't explain how to do it anyway perhaps make it simpler like a for ((;;)) statement..
make a few lines that processes the first arg then do the other processes that appends arg with the old arg in the for loop like:
for arg in "$@"; do

some suggestions:

you can make arg not parse spaces by doing:
while [ $# -eq 1 ]; do
for arg in "$@"; do
it's also simpler to do IFS=$'\n' in newer shells like bash

Last edited by konsolebox; 05-18-2008 at 08:12 AM.
Old 05-19-2008, 06:31 AM   #18
Registered: Apr 2008
Location: Gray, TN, USA
Distribution: UbuntuStudio, Linux Mint
Posts: 36

Rep: Reputation: 15
Originally Posted by yao_man View Post
I aliased rm and rmdir so that they move the files to the trash instead of removing them completely by using a "trash" function I wrote. I am trying to create a delete function that checks for approval before completely removing the given file and anything within that file (even if it is a directory) so I can easily remove files and directories when I need to.
Both of those have troubles if the file has a space in it.
BTW, this is more of an attempt to teach myself programming problem solving and logic as well as overall programming ability then an attempt at efficiency.
If you're using find, you don't need a loop. Just use xargs, and it should handle spaces just fine while still being efficient.

find -maxdepth 1 -type f -print0 | xargs -0 -i -t mv {} /tmp/Trash
from the "find" man page:
True; print the full file name on the standard output, followed by a null character (instead of the newline character that `-print' uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the `-0' option of xargs.
Old 05-19-2008, 07:17 AM   #19
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Originally Posted by jschiwal View Post
You could either use
for file in $(ls -A)
for file in $(find . -maxdepth 1 -type f)

The latter will return only files.

Rather than aliasing rm and rmdir you might want to name the command something like "trash" so that the name describes what you want to do. You might also might want to only "trash" the files that you give in arguments instead of all of the contents in the current directory.
This is your original problem: when you have a file name (or any other argument for that matter) which has an space or any other character that has any special meaning for your shell, then you need to quote or scape it.

So, if

file="my file"

rm $file
Will be substituted as

rm my file
And it will search for two different files, called "my" and "file".

But if you quote the thing adequately, then the thing is much different.

rm "$file"
Remember to quote the things adequately if you don't know what a given variable is going to hold.

I'm in a hurry now, if I can I will review the rest of the thread later just in case.



bash, directory, file, folder, iterate, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash shell script question noisewater Programming 3 11-14-2005 08:39 PM
Bash shell script moodupani Linux - General 2 09-01-2005 06:14 AM
bash shell script globeTrotter Linux - Newbie 5 06-03-2004 05:07 AM
bash/shell script mikis Programming 2 10-14-2003 11:39 AM
bash shell script MaryM Linux - Newbie 0 02-15-2002 11:45 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:17 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration