Need Help - match file name and copy to Directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help - match file name and copy to Directory
# 1  
Old 07-08-2011
Need Help - match file name and copy to Directory

I am trying to sort the following files from folder Bag to Apple, Cat Food, Dog Food. I can get all of the files I want into a new folder, but not sure of the best approch to get them to their final directory

My Files
==========
Code:
apple.1234.ext
apple.1235.ext
cat food 101.ext
Cat Food 102.ext
Dog Food - 101 -Chicken.ext
Dog Food - 102 - Beef.ext
stuff.junk
junk.junk
paper.junk

My Folders
===========
Code:
Apple - Get All Files starting with apple here
Cat Food - get all cat food here
Dog Food - get all dog food here 
Bag - all My Files are here
Table - all the .ext here

I can
Code:
find ~/Bag -name "*.ext"  -exec cp {} ~/Table \;

just fine but I have no clue as to best way to sort from ~/Table to each of the folders

Trying to get this done but have been banging my head about it, Please help or point me in the right direction please

Last edited by radoulov; 07-08-2011 at 12:54 PM.. Reason: Additional code tags.
# 2  
Old 07-08-2011
This sounds like homework to me. We have a homeowrk forum.

Code:
#!/bin/bash
cd ~/Bag
for fname in *
do
  echo "$fname | grep -q '\.ext$' || continue
  val=${fname,,}
  cp $fname ~/Table
  case "$val" in
     "apple*") dest=~/apple ;;
     "dog*")  dest~/'Dog Food' ;;     
  esac
  cp $fname ${dest}/$fname
done

This works only in the bash shell. You can add the extra stements needed to get all of the destinations defined.

Note: spaces in UNIX filenames are a PITA, and are usually inflicted on you only by windows applications. Note the ' ' around Dog Food.
# 3  
Old 07-08-2011
Jim,
Thanks for your assist - it is not homework per se - I have no formal education in Unix or scripting, but am trying to learn. So far these forums have been great, but this one had me stumped...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

how to copy the directory but not copy certain file

Hi experts cp bin root src /mnt but not copy bin/bigfile any help? ( I post this thread in the "redhat" forum wrongly, I don't know how to withdraw that question in that wrong forum) Thanks (6 Replies)
Discussion started by: yanglei_fage
6 Replies

4. Programming

how to copy file to a directory

Hello, I've been spending a lot of hours trying to imitate cp copying a file to a directory. cp I just can't seem to write to a specified directory, it only creates a copy on the current directory. any hints/tips will help! Thanks! here's the code i've been trying to manipulate: ... (1 Reply)
Discussion started by: l flipboi l
1 Replies

5. UNIX for Dummies Questions & Answers

How to copy a file to a directory?

Hello all, I've been researching this problem for days, and have gotten no luck . =/ How do you copy a file to another directory without being in the same directory as the file? So, for example, say I wanted to copy the file 'my.txt' that is in the directory ' /export/hom0/user/asdf ' to the... (9 Replies)
Discussion started by: kvnqiu
9 Replies

6. Shell Programming and Scripting

find and copy file to another directory..

Hi Everybody, i want a samll help to write a script. i had source location with :/user/bin (bin contains subdirectories with like names emails etc and had several files in each subdirectory) and target location with :/usr/scripts (having same subdirectories names and had some files)... (1 Reply)
Discussion started by: Reddy482
1 Replies

7. Shell Programming and Scripting

Match File and Copy File Script (Homework, Closed)

Can you please help on this? I am looking for the shell script which does following:- step 1: It should open the file /u/manish/input/FileIndex.dat and read line by line step 2: Once first line is read (for ex: File1), we have to find a file, that contains this matching... (4 Replies)
Discussion started by: teteguru1
4 Replies

8. Shell Programming and Scripting

Copy the latest file to a directory

Hi Team, I wish to copy the latest file of pattern "MyFile*" to some other location. I need to do all the operation in a single command separated by |. ls -rt <MyFile*> | tail -1 | <copy command>. How can I do? Please help me. Thanks, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

9. Shell Programming and Scripting

Copy the latest file from one directory to another

Hi All, I am in the directory a/b/processed the files in this directories are -rw-r--r-- 1 owb users 330 Aug 8 chandantest.txt_08082008 -rw-r--r-- 1 owb users 220 Aug 7 chandantest.txt_07082008 -rw-r--r-- 1 owb users 330 Aug 6... (3 Replies)
Discussion started by: chandancsc
3 Replies

10. Solaris

Copy files from the file to another directory

I have created a file that has list of all the files I want to copy into another directory.Is there a way to do it? Thanks In advance (4 Replies)
Discussion started by: shreethik
4 Replies
Login or Register to Ask a Question