Distinct filenames pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Distinct filenames pattern
# 1  
Old 11-21-2014
Distinct filenames pattern

Hi All,

I am working on designing the archival process for my system, where I will have to find distinct file names ( when excluded time_stamp extention ) from given directory and for each file type keep the latest and move all other older to different location ( lets say dir Back ). Below are example of files I will have :

-rw-r--r-- 1 user_id grp_name 10445 Oct 17 10:04 abc.dat.20141017_100517
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 10:57 abc.dat.20141017_105829
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 11:13 mno.dat.20141017_111422
-rw-r--r-- 1 user_id grp_name 10445 Oct 17 16:01 mno.dat.20141017_160144

==============:
I would expect to get distinct list and for each, keep latest :
So for above example I have 2 distinct file_type ( excluding time_stamped extention ) ==> abc.dat / mno.dat

then once i have list, I need to move older and keep latest.
for ex. for abc.dat :
I will keep abc.dat.20141017_105829 and move abc.dat.20141017_100517
to backup dir.

Is there any easy implementation for this one??


Thanks in advance.
# 2  
Old 11-22-2014
Please use code tags as required by forum rules!

Why don't you adapt / extend the solution to your former problem? That'd be an easy implementation!
# 3  
Old 11-22-2014
If this code works for you and if you keep it (and execute it) in the directory where all the distinct files are initially stored, you can run it again and again without modification. There should not be two completely identical files in the source and destination dir (backup dir), though, else the file in the destination dir will be overwritten.
Code:
#!/bin/bash

# create file list with distinct files
for file in *.dat*; do
 echo "${file%%.*}"
done | sort | uniq -d > distinct.txt

# exit if there are no distinct files
if ! [ -s "distinct.txt" ]; then
 echo "No distinct files."
 rm -f distinct.txt
 exit
fi

# check if backup dir exists, else create it in the current dir
[ -d "backup" ] || mkdir backup

# read from previously created file list
while read dfile; do
x=1
# list available files for the particular distinct file, newest first
# skip very newest file, move the rest into backup dir
 ls -t "$dfile"* |\
  while read line; do
   [ $x -eq 1 ] && x=0 && printf "%s\n" "skipping \"$line\" ..." && continue
   echo mv "$line" "backup"
  done
  x=1
done < distinct.txt

# delete file list
rm -f distinct.txt

If the output of this script is correct, remove the echo command to make the mv commands work.

Last edited by junior-helper; 11-22-2014 at 04:56 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pattern Match FileNames

I am on AIX. I need to list the contents of the directory based on a pattern and write an XML output file with file names. If a filename does NOT match the below pattern then write an OUTPUT xml file in the below xml format Pattern Starts with (.abc) and contains (def) Starts with... (4 Replies)
Discussion started by: techedipro
4 Replies

2. Shell Programming and Scripting

Need the distinct of these lines

Red Hat Enterprise Linux 5.4 I have a text file (error log file) , which has occurences of an error message like ORA-01652: unable to extend temp segment by 8 in tablespace xxxxxThere are around 3000 error messages like this in the error log file. But there are only 7 or 8 distinct... (3 Replies)
Discussion started by: John K
3 Replies

3. Shell Programming and Scripting

Get the distinct of a particular field

From the below ps output , I want the distinct values of the third field (ie. I need the distinct PPIDs) $ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Nov 10 - 48:49 /etc/init root 1769576 1 0 Nov 10 - 0:07... (1 Reply)
Discussion started by: polavan
1 Replies

4. UNIX for Dummies Questions & Answers

Searching for a pattern from filenames stored in a file

Hi, I have got some 10 filenames stored in a file or displayed in the console as a result of some query i made.. Now I need to open each of these files and search for a pattern in these 10 files.. Can someone help me with this? Thanks, Jean (9 Replies)
Discussion started by: jeanjkj
9 Replies

5. UNIX for Dummies Questions & Answers

Grep distinct

Hello, I have a file which looks like: %_SPE_RDP_NUM_ECH(7)*************% %_SPE_RDP_NUM_ECH(70)************% %_SPE_RDP_NUM_ECH(71)************% %_SPE_RDP_NUM_ECH(72)************% %_SPE_RDP_NUM_ECH(8)*************% %_SPE_RDP_NUM_ECH(9)*************% %_SPE_FLUXPREV_PRES1_MT_HT(2)****%... (5 Replies)
Discussion started by: mvalonso
5 Replies

6. UNIX for Dummies Questions & Answers

Capture filenames with a Pattern into another file

Hi i have a command in windows which captures the filenames with the same pattern and route it to another file. for example filetest_20110921, filetest_20110922,filetest_20110923 etc I would like to know the command in UNIX/LINUX for the same. The command which i am using in Windows is ... (12 Replies)
Discussion started by: mansurkhalil
12 Replies

7. Web Development

SQL - Distinct plus more

Hi all, I have an interesting and I am sure simple question for yau'll. Basically this is what I am after: The table: CREATE TABLE places (id INT, city VARCHAR(24), name VARCHAR(24)); The data: id = 1, city = canberra, name = aaron id = 2, city = canberra, name = andrew id = 3, city... (4 Replies)
Discussion started by: STOIE
4 Replies

8. Solaris

Look for distinct files under a directory matching a pattern

Hi, I'm searching for a pattern 'java' under a directory but it is returning all the files containing 'java', but I want to have only distinct files not all. please help (2 Replies)
Discussion started by: b.paramanatti
2 Replies

9. Shell Programming and Scripting

BASH find filenames in list that match certain "pattern."

I guess by "pattern," I mean something different from how that word is defined in the Linux world. If you take $ to mean a letter (a-z) and # to mean a number (0-9), then the pattern I'm trying to match is as follows: $$$##-####-###-###.jpg I'd like to write a script that reads in a list of files... (4 Replies)
Discussion started by: SilversleevesX
4 Replies

10. Shell Programming and Scripting

grep distinct values

this is a little more complex than that. I have a text file and I need to find all the distinct words that appear in a line after the word TABLESPACE when I grep for just the word tablespace, I get: how do i parse this a little better so i have a smaller file to read? This is just an... (4 Replies)
Discussion started by: guessingo
4 Replies
Login or Register to Ask a Question