-   Programming (
-   -   Bash 2 several words filename (

trintukaz 11-27-2011 05:16 AM

Bash 2 several words filename
Hello guys. I am making a script which one of the functions hide files.


if [ $1 == "--hide" ]

        if [ $2 != "--all" ]
                files=`echo $@ | awk '{$1=""; print $0}'`
                for i in $files
                        if [ -e $i ]
                                echo "$i -> .$i"
                                mv $i .$i

Here is my code but I face with a problem. When filename has few words hidding does not working.

sinuhe 11-27-2011 06:18 AM

.hidden files
First, you would do better with a case statement instead of if statements. Second, do you mean that when you have spaces in a file name, that your script fails? If so, double quote your expansions, e.g. "$i"

trintukaz 11-27-2011 06:37 AM

i tried double quote before but still dos not work.

trintukaz 11-27-2011 07:33 AM

maybe any suggestions?

grail 11-27-2011 10:58 AM

If file names have spaces in them the for loop will not work as it will break each file into the whitespace separated pieces.

Maybe you could show us how you are calling your script as the format you currently have is a little vague?

NevemTeve 11-27-2011 12:00 PM

Use find(1).
something like this (not tested):


find . -name "*mask" -exec mv "{};" ."{};"

David the H. 11-27-2011 12:07 PM

Yes, more info please. But to start with, check out these links:
See 1, 2, 4, 10 in particular. And perhaps 24 and 37.

$(..) is highly recommended over `..`

Have a look at the shift built-in command.

Arrays might possibly be of use too, depending on your exact requirements. See the BashGuide for more on them and other general scripting advice:

trintukaz 11-27-2011 02:02 PM

i call my script like this

~/ScriptDestination --hide filename

grail 11-27-2011 10:12 PM

Well if you are guaranteeing all file names look just like that, I see no issues as script will work without issue.

All times are GMT -5. The time now is 07:19 AM.