LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2013, 09:26 AM   #1
_furrycat_
LQ Newbie
 
Registered: Jul 2013
Distribution: Debian
Posts: 8

Rep: Reputation: Disabled
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...?

Last edited by _furrycat_; 10-06-2013 at 09:28 AM.
 
Old 10-06-2013, 11:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script, display contents of a file using cat Lindy70 Linux - Newbie 2 05-08-2012 10:13 AM
[SOLVED] Using Expect/TCL to move contents of a text file to an executable file jefsa Linux - Newbie 4 04-20-2012 10:43 AM
In a bash script, how do I move a line to a new file. wonderfullyrich Programming 18 04-29-2011 11:52 PM
Script to find tar archives, read tar file contents, output content to an index file. bluesword1969 Linux - General 4 02-07-2011 12:15 PM
Bash script for editing contents of one file according to other kkpal Linux - Newbie 8 02-26-2008 03:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration