chmod for files and directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers chmod for files and directories
# 1  
Old 03-24-2009
chmod for files and directories

Hi,
OS - Unix, linux (all unix flavors)
My requirement. To check directory/file exists and then change the permission of the directories/files.

Iam trying to start with directory and here is my code in the file totalchange.sh (insideragain - is a directory, test1.txt - is a file under the directory insideragain)

totalchange.sh file
Code:
$1 = insideragain
if [ ! -d "$1" ]
    then
      chmod 550 test1.txt
fi

when i run it, i get the following error message

Code:
rt-opuX4:$ totalchange.sh
totalchange.sh[2]: =:  not found

where am i wrong?

Next i want to check if file exists, so

totalchange1.sh
$1 = insideragain
$2 = test1.txt
if [ ! -d "$1" ]
if [ ! -f "$2"]
then
chmod 550 $2
fi
elsif
echo "directory not find"
fi

Can anyone point to right direction for both these files totalchange.sh and totalchange1.sh?

thanks

Last edited by Yogesh Sawant; 03-25-2009 at 03:09 AM.. Reason: added code tags
# 2  
Old 03-24-2009
Here i did not get you why are you using

Code:
totalchange.sh file

are you parsing any argument to totalchange.sh script?
if not then..

You can try like this..


cat totalchange.sh
Code:
var="insideragain"
if [ ! -d $var ]
then
chmod 550 test1.txt
fi

cat totalchange1.sh
Code:
var="insideragain"
var1="test1.txt"
if [ ! -d "$1" ]
then
if [ ! -f "$2"]
then
chmod 550 $2
fi
echo "directory not find" 
fi

Hope this helps your need..

THanks
SHa
# 3  
Old 03-25-2009
Thanks for your post.
To answer your question, Yes iam not passing any parameters.
Iam using the var as you mentioned.

I made some modifications and its working fine, perfect.
(The script i changed -
Code:
var1="insideragain"
var2="inside2.html"
if [ ! -d $var1 ];
then
echo $var1
echo "Directory not found"
  exit 1
  else
    if [ ! -f $var2 ]
       then
       chmod 444 $var1/inside2.html
    fi
#else
#echo "Directory not found"
fi)

One more question:
Is there any way to loop it. i mean

if <condition true>
then <do something>
elsif goto nextstep

In windows batch script i can use (In the below case if it does not satisfy i go to "nextstep" and it works from there)

if not <condition true> goto nextstep
then <do something>
endif
:nextstep
<whatever needs to be done here>

Thanks for your valuable input. It worked. I appreciate too.

Last edited by Yogesh Sawant; 03-25-2009 at 03:10 AM.. Reason: added code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

2. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

3. UNIX for Dummies Questions & Answers

Change chmod on files in diff directories

I am looking for a small script to crawl through several directories and change a couple of files in each directory to read write status. Anyone have any ideas ? (5 Replies)
Discussion started by: zapper222
5 Replies

4. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

5. UNIX and Linux Applications

What is the difference between chmod in solaris and chmod in Linux?

i think it is the same in both... Iam i right? (1 Reply)
Discussion started by: sumaiya
1 Replies

6. UNIX for Advanced & Expert Users

chmod to parent and sub directories ?

Hi folk, Could you please give me command to give the rwx permissions to the parent and its sub directories ? Is it possible to fire one command or Script for giving permission to both the parent and its sub directories. I meant to say something like in recurcive. I am using AIX 5.2 ... (2 Replies)
Discussion started by: varungupta
2 Replies

7. Shell Programming and Scripting

help with chmod (files only)

hello, i want to chmod 444 all files in a directory, files in subdirs cannot be chmoded same goes for the subdirs themself. So using: chmod -R 444 /dir/ won't work because it will chmod the directorys and files (together with files in subdirectorys) I figured out how to chmod files... (1 Reply)
Discussion started by: TehOne
1 Replies

8. UNIX for Advanced & Expert Users

recursive chmod that only affects directories?

The man page for chmod doesn't list a way to recursively change permissions on directories only, without affecting the files themselves. Let's say that I wanted to change the permissions on the current directory and all subdirectories. I know I can write a bash script that would do this using... (1 Reply)
Discussion started by: retrovertigo
1 Replies

9. UNIX for Dummies Questions & Answers

chmod 777 on all directories below...how do I do that using the "find" command?

I've got 100 directories that each have 2 directories with in them. Structered like this: /home/domains/domain1/ through to /home/domains/domain100/ and those 2 directories mentioned above are here: /home/domains/domain1/directory1/ /home/domains/domain1/directory2/ through to... (7 Replies)
Discussion started by: Neko
7 Replies
Login or Register to Ask a Question