Script to delete all something.txt~ file from a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete all something.txt~ file from a directory
# 1  
Old 12-17-2010
Question Script to delete all something.txt~ file from a directory

There are some files in a directory like a.tx~ , b.txt~,c.txt~.
I want to delete all these files inside that directory and sub directory.
How can i do this?
Code:
#!/bin/bash
cd thatdirectory
......
rm -rf *~
......

# 2  
Old 12-17-2010
See find with the exec option.
# 3  
Old 12-17-2010
Any example?
# 4  
Old 12-17-2010
Eg
Code:
find somewhere -name '*~' -exec command {} \;

Note that the most required ability in the computing field is the ability to read and understand technical documentation, instead of relying on being fed read-to-go solutions.
# 5  
Old 12-17-2010
Quote:
Originally Posted by pludi
Eg
Code:
find somewhere -name '*~' -exec command {} \;

Note that the most required ability in the computing field is the ability to read and understand technical documentation, instead of relying on being fed read-to-go solutions.
I know this command.
Confusion was, will it search recursively inside directory?
# 6  
Old 12-17-2010
Yes, see DESCRIPTION section in find manual find.
# 7  
Old 12-17-2010
From the manual there are:
Code:
       -exec command ;
          Execute  command;  true  if 0 status is returned.  All following
          arguments to find are taken to be arguments to the command until
          an  argument  consisting of `;' is encountered.  The string `{}'
          is replaced by the current file name being processed  everywhere
          it occurs in the arguments to the command, not just in arguments
          where it is alone, as in some versions of find.  Both  of  these
          constructions might need to be escaped (with a `\') or quoted to
          protect them from expansion by the shell.  See the EXAMPLES sec-
          tion for examples of the use of the -exec option.  The specified
          command is run once for each matched file.  The command is  exe-
          cuted  in  the starting directory.   There are unavoidable secu-
          rity problems surrounding use of the -exec  action;  you    should
          use the -execdir option instead.

       -exec command {} +
          This  variant  of the -exec action runs the specified command on
          the selected files, but the command line is built  by  appending
          each  selected file name at the end; the total number of invoca-
          tions of the command will  be  much  less  than  the  number  of
          matched  files.    The command line is built in much the same way
          that xargs builds its command lines.  Only one instance of  `{}'
          is  allowed  within the command.    The command is executed in the
          starting directory.

From example i got:
Code:
find . -type f -exec file '{}' \;

Why is there no \; at last from manual?
Or why is there no + in example?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Delete files in a txt file

Hi, I have very old files in my server like from 2012 and i want to delete them, Please help. Thanks in advance.. (2 Replies)
Discussion started by: nanz143
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. UNIX for Dummies Questions & Answers

Delete .txt file from current directory

I have created few text file during run time by redirecting the txt file echo USER_LIST_"$(date '+%d%h%Y')".csv > report_location.txt report_location.txt is creating in the same location where I run script home/script When I try to remove the txt file at the end of the... (3 Replies)
Discussion started by: stew
3 Replies

4. Shell Programming and Scripting

Delete specific parts in a .txt file

Hi all, I desperately need a small script which deletes everything in a particular .txt file when "Abs = {" appears till "},", and also when "B-1 = {" appears till "}," I would like all the text in between of those instances to be deleted, however, other text to be unedited (kept as it is).... (12 Replies)
Discussion started by: c_lady
12 Replies

5. UNIX for Dummies Questions & Answers

Delete files whose file names are listed in a .txt file

hi, I need a help. I used this command to list all the log files which are for more than 10 days to a text file. find /usr/script_test -type f -mtime +10>>/usr/ftprm.txt I want all these files listed in the ftprm.txt to be ftp in another machine and then rm the files. Anyone can help me... (8 Replies)
Discussion started by: kamaldev
8 Replies

6. Shell Programming and Scripting

Delete file2.txt from file1.txt using scripting

Hi, I`m a total newbie, well my requirement is that i have 2 files I want to identify which countries i do not currently have in db.. how can i use the grep or another command to find this file .. i want to match all-countries.txt with countries-in-db.txt so the output is equal to... (11 Replies)
Discussion started by: beanbaby
11 Replies

7. Shell Programming and Scripting

delete only particular file in directory shell script

Hi, what function do we use to delete only particular file that we want in directory shell script for example I want only to delete test.txt in directory how to do it ? in sh Thank (1 Reply)
Discussion started by: guidely
1 Replies

8. Shell Programming and Scripting

script to delete lines from a txt file if pattern matches

File 6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04 42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36 6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies

9. UNIX for Dummies Questions & Answers

recursive delete files from txt file or?

i have a txt file of image names that have to be deleted in pwd how can i use the txt file to delete the files in pwd and is it possible?--i might be able to import the txt files into a spreadsheet ahd same it as a csv file. i want it to be done recursive lly --what i mean is teh sysem goes thru... (4 Replies)
Discussion started by: plener
4 Replies

10. Red Hat

How to get the name of the first *.txt file in a directory?

Please tell me the command to get the name of the first txt file found in a directory in a variable? I need to use it in a shell script. (3 Replies)
Discussion started by: omariqbalnaru
3 Replies
Login or Register to Ask a Question