Visit Jeremy's Blog.
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 08-13-2004, 03:24 PM   #1
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, 03:50 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
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 04:02 PM.
Old 08-13-2004, 03:55 PM   #3
Registered: Aug 2004
Distribution: debian, SuSE
Posts: 365

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

for i in *.mp3
chmod 755 $i

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

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

vi ~/my_chmod

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

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

Then create a driver file:
vi ~/driver
To run this script:

~/my_chmod 700 ~/driver


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

All times are GMT -5. The time now is 01:59 AM.

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