Remove two types of files from a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove two types of files from a directory
# 1  
Old 08-24-2011
Remove two types of files from a directory

Hi All,
i need to move two types of files from a directory.
I have used the below command to find the files from the directory..
Code:
SOURCE_DIR="some directory path"
TARGET_DIR="Target Dir"
Datestamp=Date_format
find $SOURCE_DIR \( -name "*.log" -o -name "*.out" ) - exec ls -1 {} \;

now i need to move them from SOURCE_DIR to TARGET_DIR with date

If the output of the find command is more the two files i need to move all of them to the TARGET_DIR and all the files shuld be appended with datestamp
ex:
outputfile.datetimestamp.out
Thanks in advance

---------- Post updated at 10:27 AM ---------- Previous update was at 10:26 AM ----------

Sorry correcting the Title Its not remove it Move

Last edited by Scott; 08-24-2011 at 04:27 PM.. Reason: Code tags
# 2  
Old 08-24-2011
I think you got it right in the first place; looks more like moving than removal to me Smilie

The
Code:
-exec ls -1

is redundant, find will print the files one by one by default when not given -exec.

ls will always give single-column when printing to a non-terminal anyway. Try
Code:
ls | cat

-- because it's printing to a pipe and not a terminal, it gives single-column output.

Code:
DATESTAMP=`date +%Y-%m-%d`

find $SOURCE_DIR \( -name "*.log" -o -name "*.out" ) |
while read FILE
do
        BASE=`basename "$FILE"`
        echo "$BASE" >/tmp/$$
        IFS="." read BASE EXT < /tmp/$$
        echo mv "$FILE" "/path/to/$BASE.$DATESTAMP.$EXT"
done

rm -f /tmp/$$

Remove the 'echo' from in front of mv once you've tested that it really does what you want.

There's more efficient ways to get the filename and extension, but they may not be available in your shell; what is it?

Last edited by Corona688; 08-24-2011 at 11:45 AM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-24-2011
Hi,

Thanks for you quick reply...

I am very new to unix...trying to learn things...

i have tried you code....

do you mind explaining me how this is done.

---------- Post updated at 10:57 AM ---------- Previous update was at 10:51 AM ----------

Hi Corona688,

Thanks a lot...it worked perfectly as i expected...
# 4  
Old 08-24-2011
Code:
# Get the date in YYYY-MM-DD.  See 'man date'
DATESTAMP=`date +%Y-%m-%d`

# Find all relevant files, and print them one per line into a pipe.
find $SOURCE_DIR \( -name "*.log" -o -name "*.out" ) |
# Read each filename from the pipe into the FILE variable one by one
while read FILE
do
        # Converts /path/from/file.ext into file.ext
        BASE=`basename "$FILE"`
        # write file.ext into a temp file, overwriting whatever's there already.
        # $$ is a special variable meaning "the shell's PID".
        # Each program running on the system gets its own unique number, or PID.
        echo "$BASE" >/tmp/$$
        # read the string "file.ext" into BASE and EXT, splitting on "."
        # so that BASE="file" and EXT="ext"
        IFS="." read BASE EXT < /tmp/$$
        # Move and rename /path/from/file.ext into /path/to/file.date.ext
        echo mv "$FILE" "/path/to/$BASE.$DATESTAMP.$EXT"
done
# Delete the temporary file.
rm -f /tmp/$$

Depending on your shell, it may be possible get away with not using basename and not writing the string into a temporary file, which would make the program faster, but this more basic code should work in most common shells.

Last edited by Corona688; 08-24-2011 at 12:12 PM..
# 5  
Old 08-24-2011
Hi Corona688,

You said we can get away without using basename. how do i do that.

Regards,
ch33ry
# 6  
Old 08-24-2011
Code:
BASE=${FILE##*/}

If you read the man-page for your shell, and search for ## (under "Parameter Expansion"), you will find an explanation (if your shell supports that).
# 7  
Old 08-24-2011
I am very new to unix...learning things....

i did'nt get this

---------- Post updated at 02:41 PM ---------- Previous update was at 02:32 PM ----------

Hi,

I was testing the script and found that,
Code:
BASE=`basename "$FILE"`

for "basename" its always taking the path of Target folder...

first time as the TARGET_DIR is empty its working fine...

from the second time its searching for ".log" -o ".out" files in the Target dir and the moving them

Please advice.

Last edited by Scott; 08-24-2011 at 04:28 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicate files in same directory

Hi all. Am doing continuous backup of mailboxes using rsync. So whenever a new mail arrives it is automatically copied on backup server. When a new mail arrives it is named as xyz:2, when it is read by the email client an S is appended xyz:2,S Eventually , 2 copies of the same file exist on... (7 Replies)
Discussion started by: coolatt
7 Replies

2. UNIX for Dummies Questions & Answers

Types of File in a directory

Hello, I have several thousand files with different extensions in a directory. Is there a single command to get what the various extensions are with a single command. Thanks for your help! Best, Guss (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

3. UNIX for Dummies Questions & Answers

remove files from a directory

I have some empty files that were created from a script I ran that I cannot delete using the rm command.... any ideas as how I can delete them? -rw-r--r-- 1 schroeks x_exp 0 Jul 21 08:24 rrd15CEC -rw-r--r-- 1 schroeks x_exp 0 Jul 21 11:37 rrd15CEC -rw-r--r-- ... (20 Replies)
Discussion started by: freddie88
20 Replies

4. UNIX for Dummies Questions & Answers

How to remove directory with subdirectories and files?

I'm trying to remove several directories which contains sun-dirs and files inside. I used the command rm -r <dirname> But, it always ask "examine file in directory <dirname> yes/no?" line by line. So, i need to write "y" for every line. How can i skip this step and remove all directories with... (9 Replies)
Discussion started by: ppa108
9 Replies

5. Shell Programming and Scripting

Remove files from a directory using perl

Hi all, I have the following question. Considder that in the directory /code the script remove.pl exists. What i want is to run this script and remove some files that exist in the directory /dir/tmp. I use the below code but it does not work. system("cd /dir/code"); system("rm FileName"); ... (6 Replies)
Discussion started by: chriss_58
6 Replies

6. UNIX for Advanced & Expert Users

Can't list or remove files from a directory

Hi I have a problem. One of the directories in /var, on one of my servers, has filled up with loads of files. I have tried to list the directory to see what is in it, but the 'ls' command just hangs. Same thing happens when I try to use 'rm' to remove files from that directory. I can see what... (2 Replies)
Discussion started by: m_smith
2 Replies

7. UNIX for Dummies Questions & Answers

remove old files from a directory

Hello I'm new to this forum, so I did a quick scan of the FAQs to see if my problem had been addressed already, since I believe it is probably a very common need. I was surprised that I did not see anything obvious. I need to remove all files from a directory that have a last modified date... (2 Replies)
Discussion started by: 2davidc8
2 Replies

8. Shell Programming and Scripting

remove duplicate files in a directory

Hi ppl. I have to check for duplicate files in a directory . the directory has following files /the/folder /containing/the/file a1.yyyymmddhhmmss a1.yyyyMMddhhmmss b1.yyyymmddhhmmss b2.yyyymmddhhmmss c.yyyymmddhhmmss d.yyyymmddhhmmss d.yyyymmddhhmmss where the date time stamp can be... (1 Reply)
Discussion started by: asinha63
1 Replies

9. HP-UX

Want to remove files from unix directory

Dear All I am basic user of Unix. i woul like to delete some files (basically 05 and 0801111105) from unix directories but unable to delete it I tried all option. rm , rm -f etc.. but not succeed. Infact it also not allowing me to use chmod option so that i can grant 777 option to... (1 Reply)
Discussion started by: yogi_chavan
1 Replies

10. UNIX for Dummies Questions & Answers

find directory with 2 types of files

Trying to use the find command to find any directory which contains a file ending in .zip AND a file ending in .o I'm having trouble specifying multiple files as criteria and have can't seem to figure it out from Unix in a Nutshell and Google. (2 Replies)
Discussion started by: dangral
2 Replies
Login or Register to Ask a Question