Shell script to move files, add extension and change permissions

Posted on 2009-02-22
Last Modified: 2013-10-08
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)
Question by:Awk2009
    LVL 40

    Accepted Solution


    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
    LVL 23

    Assisted Solution

    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


    Author Comment

    Awesome - Thanks!
    LVL 10

    Expert Comment

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now