Delete all files from the directory except the ones in the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete all files from the directory except the ones in the log file
# 1  
Old 09-19-2014
Tools Delete all files from the directory except the ones in the log file

I have a log file with contents like below.
Code:
Repository: https://someserver:9443/
Workspace: (1000) "test_scripts_ws"
  Component: (1001) "some_Automated_Scripts"
    Change sets:
      (1002) ---$ john "test memory" 17-Sep-2014 02:24 PM
        Changes:
          --a-- \Libs\test1\test1\test1.txt
          --m-- \Libs\test1\test1\test11.txt
          --d-- \Libs\file\in\some\folder\lock.lck
          ---c- \Libs\Libs\test2\test2\test22.txt
          ---c- \Libs\Libs\test2\test2\test2.txt
          
      (1003) ---$ john "Test exist:"  17-Sep-2014 12
:33 PM
        Changes:
          ---c- \Libs\Libs\test3\test33.txt
      (1004) ---$ john "Memory Fix #2" 17-Sep-2014 12:47 PM
        Changes:

I want to delete all the files in the directory except the ones starting with the strings
Code:
--a--
--m--
---c-

for now I have :
Code:
cat required.txt | grep -e --a-- -e --m-- -e ---c-

Output:

Code:
--a-- \Libs\test1\test1\test1.txt
 --m-- \Libs\test1\test1\test11.txt
 ---c- \Libs\Libs\test2\test2\test22.txt
 ---c- \Libs\Libs\test2\test2\test2.txt
 ---c- \Libs\Libs\test3\test33.txt

The script should not error even if say test33.txt doesn't exist in the directory

Last edited by Franklin52; 09-25-2014 at 10:26 AM.. Reason: Adding code tags
# 2  
Old 09-19-2014
What shell do you use? In bash, you can set the extglob option to perform extended pattern matching like !(...) for "not matching pattern".
# 3  
Old 09-19-2014
Hi ,

I am using bash in cygwin. Can you please elaborate ?

So far I have

Code:
cat required.txt | grep -e --a-- -e --m-- -e ---c- | awk '{print $(NF-1)}'

Output:
Code:
\Libs\test1\test1\test1.txt 
 \Libs\test1\test1\test11.txt  
\Libs\Libs\test2\test2\test22.txt 
 \Libs\Libs\test2\test2\test2.txt  
\Libs\Libs\test3\test33.txt

---------- Post updated at 03:36 PM ---------- Previous update was at 03:34 PM ----------

Trying to get the file name using cut in reverse order ..not successful so far..

Last edited by Franklin52; 09-25-2014 at 10:26 AM.. Reason: Adding code tags
# 4  
Old 09-19-2014
Your long pipe can be replaced by sth like
Code:
awk '/--(a-|m-|-c)-/ {print $(NF-1)' required.txt

For the extglob, try (in bash!)
Code:
shopt -s extglob
ls !(test1*)

If happy, extend to the result of above awk and do the rm on it.
# 5  
Old 09-19-2014
Quote:
Originally Posted by RudiC
Your long pipe can be replaced by sth like
Code:
awk '/--(a-|m-|-c)-/ {print $(NF-1)' required.txt

For the extglob, try (in bash!)
Code:
shopt -s extglob
ls !(test1*)

If happy, extend to the result of above awk and do the rm on it.


then add " |xargs rm -rf "


but to make sure

use this first

" |xargs echo "
# 6  
Old 09-19-2014
He/she wants to NOT remove the files listed!
# 7  
Old 09-19-2014
Code:
$ cat r
--a-- \Libs\test1\test1\test1.txt
--m-- \Libs\test1\test1\test11.txt
--d-- \Libs\file\in\some\folder\lock.lck
---c- \Libs\Libs\test2\test2\test22.txt
---c- \Libs\Libs\test2\test2\test2.txt
 $ cat r|egrep -v '(--m--|---c-)'
--a-- \Libs\test1\test1\test1.txt
--d-- \Libs\file\in\some\folder\lock.lck
$


Last edited by Franklin52; 09-23-2014 at 09:43 AM.. Reason: Please use 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

Delete files directory

amigos necesito dejar en un directorio solo los archivos del dia anterio, como puedo hacer eso con una shell Hello. Per our forum rules, all posts must be in English. We do provide translation services for posts from English to a number of languages as a benefit to users. However,... (16 Replies)
Discussion started by: tricampeon81
16 Replies

2. Shell Programming and Scripting

Want to delete the junk files from a directory which are not listed in a TEXT file

Hello Everyone, I want to delete the image files from a directory, which are not listed in a TEXT file. The directory contains large number of image files (in millions) required / not required. I want to delete the image files which are "not required". I have generated a Text file having... (3 Replies)
Discussion started by: Praveen Pandit
3 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 if another files in the same directory has a matching occurrence of a specific word

he following are the files available in my directory 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... (7 Replies)
Discussion started by: kridhick
7 Replies

5. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

6. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

7. UNIX for Dummies Questions & Answers

Cannot delete files and directory

hello i am trying to delete some files and also some directories. However, despite having the required permissions (i m the owner), Permission is being denied. I also tried to delete using find and inode number, but again Permission was denied. :wall: I am new to unix so please dumb down... (8 Replies)
Discussion started by: curiosity
8 Replies

8. 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

9. Shell Programming and Scripting

delete all but one files in a directory

what`s the script to do that? assuming my text file is "test.txt" (10 Replies)
Discussion started by: finalight
10 Replies

10. Shell Programming and Scripting

Delete Some Old files from Particular Directory

Hi Team, I am new to scripting. I want to create a script, which needs to keep only 5 days directories and want to remove the old directory from a particular directory. Can Somebody help me with starting this script. All my directories will be created in the name <YYYYMMDD>. Thanks... (2 Replies)
Discussion started by: siva80_cit
2 Replies
Login or Register to Ask a Question