How to delete only certain files?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to delete only certain files?
# 1  
Old 04-07-2015
How to delete only certain files?

I have a directory which contains files that are the some name with only one exception -- one of the files is a duplicate of its original and has "copy" at the end of the name, and they all end in .doc

What I want to do is delete only the originals and keep the copy, but I can't figure out how to do it. I've tried listing them, but I don't know how to restrict what I list/delete only the desired files using just the standard wildcard characters. Can anyone help me?
# 2  
Old 04-07-2015
Code:
for file in $(ls *.doc |grep -v copy); do
echo rm $file
done

Verify your list is correct, then remove the echo...
# 3  
Old 04-07-2015
If I understand correctly, you want to delete the original files if copies exist but leave them intact if no copy is there. Try
Code:
for FN in *copy.doc; do echo rm "${FN/copy}"; done
rm f1.doc
rm f2.doc

Remove echo if happy with the result. Should the be spaces etc. in the filenames, try
Code:
ls *copy.doc | { while read FN; do echo rm ${FN/copy}; done; }
rm f1.doc
rm f2.doc

# 4  
Old 04-07-2015
I think previous solutions are not exact about where the copy is.
E.g.
Code:
FN=copy123_copy.doc
echo ${FN/copy}
123_copy.doc

The following is more exact and also works in all Posix shells:
Code:
for file in *copy.doc
do
   ofile=${file%copy.doc}.doc # strip copy.doc from the end and add .doc again
   echo rm "$ofile"
done


Last edited by MadeInGermany; 04-07-2015 at 02:05 PM..
# 5  
Old 04-07-2015
I'd guess the pattern *copy.doc will allow for only the desired files.

BTW, what is the star in your %* needed for?
# 6  
Old 04-07-2015
You are right, the * shouldn't be there because % always strips from the end.
Removed in my previous post.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete only two files

Hello all I am new at this forum. I needed little help with unix Display first two files on screen and at the same time send to recycle bin. I tried following command and it deleted all files in my current directory. ls -l | grep ^- | head -2 | rm * let me know how do to this, is recycle... (2 Replies)
Discussion started by: dnemo12
2 Replies

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

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

4. UNIX for Dummies Questions & Answers

Concatenate files and delete source files. Also have to add a comment.

- Concatenate files and delete source files. Also have to add a comment. - I need to concatenate 3 files which have the same characters in the beginning and have to remove those files and add a comment and the end. Example: cat REJ_FILE_ABC.txt REJ_FILE_XYZ.txt REJ_FILE_PQR.txt >... (0 Replies)
Discussion started by: eskay
0 Replies

5. Shell Programming and Scripting

Delete all files but exclude some files

Hi, I have a software install in a directory, says "/opt/tibco". If I want to remove all the things in this directory and its sub-directories, but exclude all log files (*.log in different directories) and keep empty directories exist. What should my command look like? I have no idea on how... (2 Replies)
Discussion started by: dirkaulo
2 Replies

6. Shell Programming and Scripting

how to delete the older files other than the recently added 5 files

Number of files will get created in a folder automatically daily.. so i hav to delete the older files other than the recently added 5 files.. Could u help me through this..?? (5 Replies)
Discussion started by: shaal89
5 Replies

7. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 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 Files except the Latest Files

Hi, In my scenario, i just want to delete all the files except the latest one. How can I do? Please reply me. (3 Replies)
Discussion started by: spkandy
3 Replies

10. UNIX for Dummies Questions & Answers

when I try to run rm on multiple files I have problem to delete files with space

Hello when I try to run rm on multiple files I have problem to delete files with space. I have this command : find . -name "*.cmd" | xargs \rm -f it doing the work fine but when it comes across files with spaces like : "my foo file.cmd" it refuse to delete it why? (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question