LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 08-13-2004, 02:24 PM   #1
teodavinci
Member
 
Registered: Apr 2004
Posts: 32

Rep: Reputation: 15
batch file for chmod's?


I am doing a bunch of chmod's to various files on my red hat 7.3 system. is there a way i can create a batch file to run them all at once? If so, how do i accomplish this? thanks in advance for any input.
 
Old 08-13-2004, 02:50 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
You might not need a batch file; you might be able to do it all with one command. Say you have a directory that contains mp3 files organized into subdirectories (artist, album, whatever), and the top directory is /usr/local/share/music. Now, let's say you wanted to make all of the mp3's readable by everyone. You could do this:
find /usr/local/share/music -type f -name "*.mp3" -exec chmod 644 {} \;

'-type f' => only select regular files
'-name "*.mp3"' => only select files with .mp3 extension
'-exec' => execute the following text as a command until you reach '\;'
'chmod 644 {}' => change the permission of the file found. {} is replaced with the matching file.

So now everybody can read those files, but you also need to make sure they can browse to the directories:
find /usr/local/share/music -type d -exec chmod 755 {} \;

'-type d' => only select directories

That will give everone read/execute permissions for the directories so they can list the contents and read files within, and give the owner read/write/execute.

Find is a great command, but if that isn't what you're looking for, I can help with creating a shell script. I'd need need more info on what exactly you're trying to accomplish (relationships between files/directories and things like that).

You can always use a brute force method by opening up a file, and typing all the commands one by one, and then telling bash to execute them (similar to a traditional windows batch file), but that's not saving you any effort as opposed to typing them all in at the command line.

Last edited by Dark_Helmet; 08-13-2004 at 03:02 PM.
 
Old 08-13-2004, 02:55 PM   #3
amfoster
Member
 
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365

Rep: Reputation: 36
If everything is in one directory, then maybe something very simple such as


for i in *.mp3
do
chmod 755 $i
done

or even chmod 755 *.mp3
 
Old 08-13-2004, 03:04 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
You could write a shell script to perform the chmod's

vi ~/my_chmod

Code:
#!/bin/sh
# 1st parameter is chmod code to be used
# 2nd parameter is a file containing the files to be altered
#
if [ $# -ne 2 ]
then
   echo "Syntax: $0 {permissions} {file listing}"
   exit 1
fi

l_chmod=$1
l_list=$2
cat $l_list|while read l_file
do
   if [ -f $l_file ]
   then
      if chmod $l_chmod $l_file
      then
          echo "permissions for $l_file have been set"
      else
         echo "problem setting permissions: $l_chmod to file: $l_file"
      fi
   else
      echo "$l_file not found!"
   fi
done
make it executable:
chmod 700 ~/my_chmod

Then create a driver file:
vi ~/driver
Code:
/tmp/file1
/tmp/file2
/tmp/file3
To run this script:

~/my_chmod 700 ~/driver
 
  


Reply



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
How to create a batch file? Franziss Programming 8 05-16-2005 12:36 AM
batch file for chmod's? teodavinci Linux - Networking 1 08-13-2004 02:39 PM
batch file for chmod's? teodavinci Red Hat 1 08-13-2004 02:37 PM
Batch File Equivalent johnsmith1169 Programming 4 05-16-2004 07:10 PM
ftp batch file cli_man Linux - General 2 05-24-2002 10:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:33 PM.

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