Delete all files if another files in the same directory has a matching occurrence of a specific word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files if another files in the same directory has a matching occurrence of a specific word
# 1  
Old 11-13-2014
Computer Delete all files if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory
Code:
RSK_123_20141113_031500.txt
RSK_123_20141113_081500.txt
RSK_126_20141113_041500.txt
RSK_126_20141113_081800.txt
RSK_128_20141113_091600.txt

Here, "RSK" is file prefix and 123 is a code name and rest is just timestamp of the file when its created.

File prefix will not change, however the code name changes.

How to delete the old files ( RSK_123_20141113_031500.txt, RSK_126_20141113_041500.txt) alone relevant to that code name.

I am looking for solution to delete these old file using script.

Any help on this much appreciated. Many thanks in advance.

I am using Linux.

Last edited by Franklin52; 11-13-2014 at 07:34 AM.. Reason: Please use code tags
# 2  
Old 11-13-2014
Not clear. Why wouldn't
Code:
CODE=123
rm RSK_${CODE}_*.txt

work?

EDIT: Ah - I think I got you. Do you want the oldest files per code deleted and keep only the modst recent one?
# 3  
Old 11-13-2014
Quote:
Originally Posted by RudiC
Not clear. Why wouldn't
Code:
CODE=123
rm RSK_${CODE}_*.txt

work?

EDIT: Ah - I think I got you. Do you want the oldest files per code deleted and keep only the modst recent one?
Yes, I want the oldest files per code needs to be deleted and retain only the most recent one related to that code.
# 4  
Old 11-13-2014
Try:
Code:
ls RSK_* | awk -F'_' '
$2 == last2 {
	printf("rm -f \"%s\"\n", last0)
}
{	last0 = $0
	last2 = $2
}'

If that correctly lists the rm commands you want to run, change the last line of the script to:
Code:
}' | sh

to execute the commands instead of just printing them.

With your sample list of files, it produces the output:
Code:
rm -f "RSK_123_20141113_031500.txt"
rm -f "RSK_126_20141113_041500.txt"

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-14-2014
SmilieSmilieSmilie

Thanks a lot Don. It works perfectly.
# 6  
Old 11-18-2014
I having minor issue if my directory name contains "_" symbol

Code:
ls system_inbound/RSK_* | awk -F'_' '
$2 == last2 {
	printf("rm -f \"%s\"\n", last0)
}
{	last0 = $0
	last2 = $2
}'

Here "system_inbound" is my directory name, the above code is not producing the intended result if the directory name contains "_" ( field separator).

This field separator may occur in my directory name more than once like ( system_inb_123 or user_inbound_124).

I tried using NF in awk, but its not producing any output at all.

Code:
ls ${inbound_target_folder}/${inbound_data_file_prefix}_* | awk -F'_' '$NF == last2 { printf("%s\n%s\n", last0, NF-2)} {last0 = $0} {last2 = $3}'

Any help is much appreciated. Many Thanks in advance.

New to UNIX shell scripting.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code samples.

Last edited by Don Cragun; 11-18-2014 at 01:20 PM.. Reason: Fix tags.
# 7  
Old 11-18-2014
Quote:
Originally Posted by kridhick
I having minor issue if my directory name contains "_" symbol

Code:
ls system_inbound/RSK_* | awk -F'_' '
$2 == last2 {
	printf("rm -f \"%s\"\n", last0)
}
{	last0 = $0
	last2 = $2
}'

Here "system_inbound" is my directory name, the above code is not producing the intended result if the directory name contains "_" ( field separator).

This field separator may occur in my directory name more than once like ( system_inb_123 or user_inbound_124).

I tried using NF in awk, but its not producing any output at all.

Code:
ls ${inbound_target_folder}/${inbound_data_file_prefix}_* | awk -F'_' '$NF == last2 { printf("%s\n%s\n", last0, NF-2)} {last0 = $0} {last2 = $3}'

Any help is much appreciated. Many Thanks in advance.

New to UNIX shell scripting.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code samples.
The obvious, simple thing to do to fix this is to change the line:
Code:
ls system_inbound/RSK_* | awk -F'_' '

to:
Code:
cd system_inbound && ls RSK_* | awk -F'_' '

Then the awk script is dealing with filenames in a directory (as it was designed to do) instead of pathnames that contain an arbitrary number of underscores.

If you're trying to use this script to handle multiple directories in a single invocation using something like:
Code:
ls */RSK_* | awk -F'_' '
...

then you need to restate your requirements so we know what is supposed to happen if the number after RSK_ has matches in multiple directories. This would be a lot more complex than dealing with matches in a single directory.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

2. Shell Programming and Scripting

Command to delete a word in all files

can anyone tell me what is the commands to delete the below particular word in the all files located in one particular file path files/ll>grep "/ftp/" test.kell ftp -m uskmc -d /ftp/ -i filename.zip output should be : ftp -m uskmc -d -i filename.zip (4 Replies)
Discussion started by: ramkumar15
4 Replies

3. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

4. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

5. UNIX for Dummies Questions & Answers

List all files with sum of matching word

grep -c 'avihai' 1.log will give me count of 'avihai' in log I want to have a list of files in the folder that show file name with count side by side. Please advice (2 Replies)
Discussion started by: avihaimar
2 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. Shell Programming and Scripting

Delete all files if another files in the same directory has a matching occurence of a specific word

Hello, I have several files in a specific directory. A specific string in one file can occur in another files. If this string is in other files. Then all the files in which this string occured should be deleted and only 1 file should remain with the string. Example. file1 ShortName "Blue... (2 Replies)
Discussion started by: premier_de
2 Replies

8. UNIX for Dummies Questions & Answers

How to delete specific files only

Hello, I have these files in my directory. ABC123 ABC12.sls.20080809111121 ABC233 ABC12.sls.20080403123212 ABC543 ABC12.sls.20080804231212 ABC323 ABC12.sls.20080809111232 ABC765 ABC12.sls.20080809112343 ABC654 ABC12.sls.20080809113133 I want to delete only files in first... (2 Replies)
Discussion started by: i.scientist
2 Replies

9. UNIX for Dummies Questions & Answers

How to search files containing a specific word in the content

Hi all, Lets say I have 3 files a.txt and b.txt and c.txt. a.txt has the following text ==================== apple is good for health b.txt has the following text ==================== apple is pomme in french c.txt has the following text ==================== orange has citric acid... (1 Reply)
Discussion started by: amjath78
1 Replies

10. Shell Programming and Scripting

delete files in specific directory

i have a directory "ABC" with lots of old files and sub directories in it. the issue now is i want to delete away files which are older than 15 days in "ABC" without deleting the files in the sub directories and without deleting the sub directory. i tried using find command but it will drill down... (2 Replies)
Discussion started by: legato
2 Replies
Login or Register to Ask a Question