Shell script to move files, add extension and change permissions

Need help writing a shell script to do the following:

1) Move all files listed in a text file located in /tmp/filelist from their current dir to /home/user/testfiles/ and add the extension .COPY to the end of each file.

2) Modify the files so that they are only readable by myself (chmod 400)
Who is Participating?

cat /tmp/filelist | while read file
   mv $file /home/user/testfiles/$file.COPY
  chmod 400 /home/user/testfiles/$file.COPY # or chmod 400 $file - if you mean orig. files

here assumed that filelist contains files full path names , other wise you need to cd to dir before the while loop
Maciej SsysadminCommented:
If filelist contains full path, then - destination path with your script can be for example: /home/user/testfiles/usr/local/etc/file :)

Another version:
for file in `cat /tmp/filelist`; do
   NAME=`basename ${file}`
   mv ${file} /home/user/testfiles/${NAME}.COPY
   chmod 400 /home/user/testfiles/${NAME}.COPY

Open in new window

Awk2009Author Commented:
Awesome - Thanks!
I was browsing through some old, but still open questions and this one popped up. Out of curiosity why has this one been open for so long without points being awarded?  Do the moderators not monitor open questions and close them when they have obviously been answered?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.