LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Move file contents to another file with bash script (find, mimetype and cat) (https://www.linuxquestions.org/questions/linux-newbie-8/move-file-contents-to-another-file-with-bash-script-find-mimetype-and-cat-4175479793/)

_furrycat_ 10-06-2013 09:26 AM

Move file contents to another file with bash script (find, mimetype and cat)
 
Hi There :)

I get some files from my ftp and now I want to sort them. Some are images, some are text files which contain one link each. I want to sort the files with a while loop. Here is what I have done so far.
Code:

#!/bin/bash

#: Script for getting stuff from the ftp
#: Step 1 will get the files
#: Step 2 will get the links :)
#: Step 3 will follow the links

HOST=192.111.222.333
USER=username
PASS=userpass

# Step 1. Uses the ftp command with the -inv switches.  -i turns off interactive prompting. -n Restrains FTP from attempting the auto-login feature. -v enables verbose and progress.

ftp -inv $HOST << EOF

user $USER $PASS

cd some/dir

mget *
mdelete *
bye
EOF
 
#: Step 2
#: Check img-folder / create it
if [ ! -d "images" ]; then
  mkdir images
fi

#: link-list there?
if [ ! -f "temp.list" ]; then
  touch "temp.list"
fi

while read -a Files; do
  if [[ $(mimetype -b "${Files}") == "text/plain" ]]; then
    cat "${Files}" | sort >> temp.list
#    rm "$Files"
  fi

  if [[ $(mimetype -b "${Files}") == "image/jpg" ]]; then
    mv "${Files}" "images/${Files}"
  fi
done < <(find * -maxdepth 0 -type f ! -name "*.list" ! -name "*.sh")

cat does not work somehow. I tried many things with IFS and other hints I found on the web but it seems not easy to get the two arguments (find, mimetype) working with cat. I also tried to remove the spaces in all filenames and so on.

The optimal solution would be, if the resulting textfile contains the filename and in the next line the link from the file.

Maybe it's just a small thing I forgot...?

grail 10-06-2013 11:12 AM

I am not sure I follow the logic??

1. read -a Files -> this means read into the array Files - as find will return each item found, why would you want to read them into an array?

2. you refer to ${Files} throughout the loop -> again see above but this is not how you would refer to an array, should be something like ${Files[@]}

3. cat "${Files}" | sort >> temp.list - you mentioned that the files contain one link each (I look at this as saying only one line in each file) ... if this is the case there is nothing to sort??
So this should just be:
Code:

cat "${Files}" >> temp.list
4. I am not familiar with the 'mimetype' command, but are you 100% that the output is exactly what you have listed? Remembering that any whitespace or different case would make the tests fail

5. personally I would use a for loop with globbing and pass if the files are not the ones your looking for (again though ... that is just my thought on that one)

Hope some of that helps


All times are GMT -5. The time now is 05:24 PM.