Chmod list of files

Hi,
I have a list of files in a text file. I want to change the mode of every one of those files, but am having difficulty in doing so.

#!/bin/bash
files=/home/david/files.txt
for $item in $files {
     chmod 640 $item 
}

.. doesn't cut it.

Can anyone help?

Thanks.

Try

while read item; do
   chmod 640 ${item}
done < ${files}

Hang on - obviously depends on input data too - please post a sample - I was assuming one file per line.

Yes, the file is called /home/david/files.txt and there is one filename per line..

/home/david/fileone
/home/david/directory_example/filetwo

.. etc.

I want to read in the list, and chmod every single one of them to 640.

Thanks.

OK cool - my code will work then:

files=/home/david/files.txt
while read file; do
   chmod 640 ${file}
done < ${files}

Hmm, that's firing up some 'no such file'....

You may experience this if spaces exist in the filenames.
Try this:

files=/home/david/files.txt
while read file; do
   chmod 640 "${file}"
done < ${files}

Do all the files in /home/david/files.txt exist?

You can double check prior to the chmod with (and good form on the quotes, dexdex200):

files=/home/david/files.txt
while read file; do
   if [ -e "${file}" ]; then
      chmod 640 "${file}"
   fi
done < ${files}

OK, that worked - some of them must have had spaces in them.

Thank you for your help, Sir. :slight_smile: