Find the answer to your Linux question:
Results 1 to 5 of 5
Ok so I've got a text file with a list of .gz files, these .gz files are in various sub directories of one parent directory and I've hacked this little ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    1

    Question Scripting help, copying files listed in a text file

    Ok so I've got a text file with a list of .gz files, these .gz files are in various sub directories of one parent directory and I've hacked this little script together to copy them from their current location to a new one and spit out any it can't find to "/home/user/not_found" but for the life of me can't get it to run properly!

    Any help would be greatly appreciated.

    The shell script as it currently looks:

    Code:
    #!/bin/sh
    
    source="/mnt/source_dir"
    dest="/mnt/output_dir"
    list="/home/user/file_list"
    
    while read -r line
    do
      file=`find "$source" -name "$line"`
      if [ -n "$file" ]
      then
        cp $file $dest
      else
        echo "$line" >> "/home/user/not_found"
      fi
    done<$list

  2. #2
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    616
    what if it was:
    Code:
    if [ -z $file ]; then
    echo $line >> /home/user/not_found
    else
    cp $file $dest
    fi
    linux user # 503963

  3. #3
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by Captain_Quor View Post
    Ok so I've got a text file with a list of .gz files, these .gz files are in various sub directories of one parent directory and I've hacked this little script together to copy them from their current location to a new one and spit out any it can't find to "/home/user/not_found" but for the life of me can't get it to run properly!

    Any help would be greatly appreciated.

    The shell script as it currently looks:

    Code:
    #!/bin/sh
    
    source="/mnt/source_dir"
    dest="/mnt/output_dir"
    list="/home/user/file_list"
    
    while read -r line
    do
      file=`find "$source" -name "$line"`
      if [ -n "$file" ]
      then
        cp $file $dest
      else
        echo "$line" >> "/home/user/not_found"
      fi
    done<$list

    The
    Code:
     -n "$file"
    is testing if the string is not equal to ''.


    Code:
    #!/bin/sh
    
    source="/mnt/source_dir"
    dest="/mnt/output_dir"
    list="/home/user/file_list"
    
    while read -r line
    do
      file=`find "$source" -name "$line"`
      if [ -f "$file" ] ;  then
        cp $file $dest
      else
        echo "$line" >> "/home/user/not_found"
      fi
    done<$list
    The
    Code:
     -f "$file"
    is testing if the file exits and is a regular file.

  4. #4
    Just Joined!
    Join Date
    Oct 2006
    Posts
    1
    Let's assume the parent directory is /folder

    now in shell do like..

    #for i in `find /folder -type f | grep .gz`; do cp -prf $i /path/to/destination_folder; done

  5. #5
    Just Joined!
    Join Date
    Sep 2006
    Location
    Rochester
    Posts
    17

    Scripting help

    Having defined list, now you need to read the contents of that file.
    I know of two quick ways to do that:

    1) exec 3<$list
    while read -r line <&3
    do
    <stuff>
    done

    2) for line in `cat $list`
    do
    <stuff>
    done

    Both will work, but #2 has problems if there are spaces in the file names.
    Also, #2 will blow up if the character count in the list file is large.

    Good luck with your scripting.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...