LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-19-2010, 03:53 AM   #1
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 200
Blog Entries: 3

Rep: Reputation: 24
Bash command to remove all files within a specific folder


I want to delete all files within a specific folder without actually deleting the folder, what is a good bash command for this?. I found this one but encountered some errors even though I am executing it within the specific folder:

useratdebian:/home/user/folder# find . -type f -exec rm -rf {} \;
[1] 5052
useratdebian:/home/user/folder# find: missing argument to `-exec'

[1]+ Exit 1 find . -type f -exec rm -rf

The command as it appears is:

find . -type f -exec rm -rf {} \;

Any ideas of how to delete only the files contained within the folder called "folder" for example?
 
Old 10-19-2010, 03:57 AM   #2
T0sh1r0
Member
 
Registered: Oct 2010
Location: Liège, Belgium
Distribution: Debian / Slackware
Posts: 47
Blog Entries: 1

Rep: Reputation: 7
I am currently not a unix system so I can not test but I would go for something like

find . -type f -name "*{}*" -exec rm -fr {} \;

And make your tests with ls (this is much less risky!!!):

find . -type f -name "*{}*" -exec ls {} \;
 
Old 10-19-2010, 04:48 AM   #3
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 200
Blog Entries: 3

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by T0sh1r0 View Post
I am currently not a unix system so I can not test but I would go for something like

find . -type f -name "*{}*" -exec rm -fr {} \;

And make your tests with ls (this is much less risky!!!):

find . -type f -name "*{}*" -exec ls {} \;
I have about 36 other folders where I have to basically enter the folder pathname everytime, can the command be modified to delete the files contained within these folders, the folders are in this format:

A-ABC-20101020-0001 (numbers are in format DDMMYY-NN)
A-ABC-20100408-0002
A-ABC-20100306-0003
etc.....................................

So the command as it is, is:

find A-ABC-DDMMYY-NN/folder/subfolder -type f -delete

Can the command above be adjusted to delete files for each of the 36 folders in just one command without doing it for each separate folder?
 
Old 10-19-2010, 04:50 AM   #4
anugraha.sinha
LQ Newbie
 
Registered: Oct 2010
Posts: 4

Rep: Reputation: 0
reply:

well you really dont need to use the find and then exec a command.
cd to the directory whose contents need to be deleted,

$ rm -fR "{}*"

this would remove the file and directories that are present in the current directory. -R is recursive and -f is force remove. If you want and interactive removal, that is it asks you for confirmation for every file and directory, use the -i option instead of a -f option.
 
Old 10-19-2010, 05:07 AM   #5
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 200
Blog Entries: 3

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by anugraha.sinha View Post
well you really dont need to use the find and then exec a command.
cd to the directory whose contents need to be deleted,

$ rm -fR "{}*"

this would remove the file and directories that are present in the current directory. -R is recursive and -f is force remove. If you want and interactive removal, that is it asks you for confirmation for every file and directory, use the -i option instead of a -f option.
Well, I don't want to delete the directories, only the files contained within the directories of each folder for example the 36 folders each have the following format with the subfolder the target folder to have files deleted:

A-ABC-DDMMYY-NN/folder/subfolder

e.g.
A-ABC-20100102-0001/folder/subfolder
A-ABC-20100204-0006/folder/subfolder
A-ABC-20100307-0005/folder/subfolder
A-ABC-20100602-0009/folder/subfolder
etc....
 
Old 10-19-2010, 05:15 AM   #6
T0sh1r0
Member
 
Registered: Oct 2010
Location: Liège, Belgium
Distribution: Debian / Slackware
Posts: 47
Blog Entries: 1

Rep: Reputation: 7
Maybe you should write a little script.
If all the directories that you want to cleanup are under the "parent-directory", you could do something like this:

cd parent-directory
ls -1 | while read line do
cd $line
find . -type f -name "*{}*" -exec rm -fr {} \;
cd ..
done

This is just a quick not tested start that you should customize it to match your needs.
 
Old 10-19-2010, 05:35 AM   #7
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 200
Blog Entries: 3

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by T0sh1r0 View Post
Maybe you should write a little script.
If all the directories that you want to cleanup are under the "parent-directory", you could do something like this:

cd parent-directory
ls -1 | while read line do
cd $line
find . -type f -name "*{}*" -exec rm -fr {} \;
cd ..
done

This is just a quick not tested start that you should customize it to match your needs.
The home directory (/home/user) looks like this:

user@debian:~$ ls -1
folder
folder
folder
folder
folder
folder
folder
A-ABC-20100405-0009
A-ABC-20100809-0007
A-ABC-20100120-0006
A-ABC-20100122-0003
A-ABC-20100125-0001
A-ABC-20100202-0001


Before I execute how does this look:

cd /home/user
ls -1 | while read line do
cd $line
find A-ABC-XXXXXXXX-XXXX -type f -name "*{}*" -exec rm -fr {} \;
cd ..
done

Should I specify the pathname A-ABC-XXXXXXXX-XXXX/folder/subfolder above?
 
Old 10-19-2010, 06:05 AM   #8
T0sh1r0
Member
 
Registered: Oct 2010
Location: Liège, Belgium
Distribution: Debian / Slackware
Posts: 47
Blog Entries: 1

Rep: Reputation: 7
Not sure if you can use wild cards in the path provided to the find command.
So here is what I would do:

ls -1 | while read line do

to

ls -1 A-ABC-2010* | while read line do
 
Old 10-19-2010, 06:26 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Yes you can use a wildcard in the path description. Would you possibly show us what these unusual files actually look like?
 
Old 10-19-2010, 07:12 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Hi,

do you also want to delete the files in the subfolders? If not you should modify your command to something like
Code:
find /path/to/A-ABC-* -maxdepth 1 -type f -name '*{}*' -exec echo rm -f '{}' \; # UNTESTED
If you are not going to delete any folders then you do not need the '-r' option for 'rm'.
Please provide more info on the directory and file structure.
 
Old 10-19-2010, 11:16 AM   #11
shayno90
Member
 
Registered: Oct 2009
Distribution: Windows10 Linux Mint NST Kali CentOS
Posts: 200
Blog Entries: 3

Original Poster
Rep: Reputation: 24
Basically there are 36 folders in the parent directory each with the same subfolder and within each of these subfolders are files which need to be deleted.

user@debian:~$ pwd
/home/user
user@debian:~$ ls -1
book.txt
red.txt
A-ABC-20100102-0001
A-ABC-20100509-0008
A-ABC-20100104-0008
A-ABC-20100203-0008
A-ABC-20100604-0008
A-ABC-20100708-0009
ETC..............(same format for the other 30 files)

user@debian:~$ cd A-ABC-20100102-0001
user@debian:~/A-ABC-20100102-0001$ ls -1
csv
data
number
folder

user@debian:~/A-ABC-20100102-0001$ cd A-ABC-20100102-0001/folder
file.txt
line
subfolder



user@debian:~/A-ABC-20100102-0001$ cd A-ABC-20100102-0001/folder/subfolder
file.csv
file1.csv
file2.csv
 
Old 10-19-2010, 12:54 PM   #12
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Thumbs up

I use
Code:
rm -rv /some/directory/*
This will delete everything inside /some/directory/ (including other dirs!!) but won't delete /some/directory/.
 
Old 10-19-2010, 06:58 PM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
My query to see file format was more based on why the use of the convoluted search item:
Quote:
-name '*{}*'
If it is just the files under each A-ABC-* directory then crts' solution could be used without the -name option:
Code:
find /path/to/A-ABC-* -maxdepth 1 -type f -exec echo rm -f '{}' \;
 
Old 10-19-2010, 07:16 PM   #14
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
... and with the OP stating
Quote:
Basically there are 36 folders in the parent directory each with the same subfolder and within each of these subfolders are files which need to be deleted.
we can even scrap the 'maxdepth' option. Leaving just
Code:
find /path/to/A-ABC-* -type f -exec echo rm -f '{}' \;
Or am I missing something? I am still having problems to understand if all files are to be deleted in the found folder and all files in every subfolder or just files in one subfolder of the folder or ...
 
Old 10-19-2010, 09:11 PM   #15
mf93
Member
 
Registered: Jun 2009
Distribution: Debian Squeeze, centOS
Posts: 229

Rep: Reputation: 36
rm /your/directory/* will delete all the files within the directory and keep all the folders
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Moving files from folders and subfolders to a specific folder mrj2 Linux - Newbie 12 08-22-2010 12:40 PM
Remove files that contain a specific string poymode Linux - General 5 02-17-2010 03:01 AM
Remove ALL files from specific Directory bianchi Programming 9 11-23-2005 11:27 PM
/bin/bash how to remove specific file anywhere on harddrive cjagdish69 Linux - Software 1 01-07-2005 05:42 AM
remove folder from command line demmylls Linux - General 3 02-15-2004 09:52 AM


All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration