LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 12-04-2007, 04:54 AM   #1
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Rep: Reputation: 31
How to write a script to delete files?


Hello,
Before uploading a my site I have to search for all backup files (ie:index.html~) and also the .dolphinview files and delete them from the local web server and of course it is a multilevel directory structure.

Is it possible to create a simple script that would do this job for me?

Thank you...
 
Old 12-04-2007, 05:20 AM   #2
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
This should be enough ("-r" stands for recursive):
Code:
rm -r *~ *.dolphinview
The "dolphinview files" are files ending in ".dolphinview", right?
 
Old 12-04-2007, 05:32 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by [KIA]aze View Post
This should be enough ("-r" stands for recursive):
Code:
rm -r *~ *.dolphinview
Actually the -r option recursively removes directories, but does not look for files inside directories which are not arguments of the rm command. The find command is more suitable for this kind of job:
Code:
find . \( -name "*~" -o -name ".dolphinview" \) -exec echo rm {} \;
In the command line above I intentionally put an echo for testing, before actually removing files.

Last edited by colucix; 12-04-2007 at 05:35 AM.
 
Old 12-04-2007, 06:06 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
If any of the files or directories in your tree have spaces in their names (or worse, end-of-line characters), the find command above can cause problems. Also, if there are a very large number of files, using -exec is inefficient since one instance of the rm program is invoked for each file deleted.

I assume you are only interested in deleting files (as opposed to whole directory trees), so it is good to be explicit about it and use the -type f option.

Thus I would recommend using this alternative:
Code:
find . -type f \( -name "*~" -o -name ".dolphinview" \) -print0 | xargs -0 rm
By way of explanation, the -print0 tells find that instead of printing each output item on a separate line (i.e. de-limited by the \n end of line character), the output should be delimited with the ASCII NUL character. The -0 option to xargs tells xargs to expect this. The reason is that a file name might contain spaces or end-of-line characters which would cause problems otherwise.

xargs just reads a list of strings, and executes whatever command you specify for groups of those strings. The size of the group is usually large, but you can limit it using the -n option
e.g.
Code:
 seq 1 10 |xargs -n 3 echo
1 2 3
4 5 6
7 8 9
10
The seq command just prints the numbers 1 to 10 (try it without the xargs part to see how this looks). xargs reads this list in groups of 3 and executes the command "echo" on them, hence the output. I hope that makes it clear how it works.
 
Old 12-04-2007, 06:31 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Good insight, Matthew! Thank you for the detailed notice
 
Old 12-04-2007, 01:38 PM   #6
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Original Poster
Rep: Reputation: 31
First I would like thank you all for your help and replies.
I tried matthewg42 command and it worked perfectly
now I can clear the folder with just one click

but I have one more simple question, How can I learn to do this on my own?
 
Old 12-04-2007, 08:42 PM   #7
[KIA]aze
Member
 
Registered: Jun 2006
Distribution: Debian, Ubuntu, Windows XP
Posts: 146

Rep: Reputation: 16
http://www.tuxfiles.org/linuxhelp/cli.html
http://linuxcommand.org/

And read man pages.
Code:
man <command>
P.S: Should have tried my suggested command before posting it. ^^'
Now that I think of it, I also usually use find ... | xargs rm , but mainly because I want to list what I'm deleting before deleting it. Thought rm -r would be simpler.
 
Old 12-05-2007, 02:34 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Asking questions about how to do something, and then trying to understand the answers is a good way to get tips.

The way I learned the core *nix commands was from curiosity. Before I even saw a unix-like OS, I had had an MS-DOS box for a year or two, and learned the DOS command inside out. Finding myself sitting in front of a unix terminal for the first time I was horrified to find that even dir was different (ls), but curious to see if there was a 1-1 mapping of programs, or some new ones (to me) on the *nix side. Once I got started it was like a month of Christmas. Pipes worked properly, shell scripts were actually useful as a language to so stuff - as opposed to the digital junk of DOS bat files, and the set of basic programs was so well chosen that it was possible to accomplish a massive variety of tasks elegantly, and have fun at the same time.

What I did was to look at the PATH variable, and for each directory, I looked at each binary name and read the manual page for it (not in every detail, but tried to understand what the program did). I did a lot of mucking about with text file processing commands: fmt, cut, paste, sort, col, expand, fold, head, tail join etc. and kept myself amused.

This was at university, and the systems we used had thousands of user accounts, hundreds of which would be logged in at any one time. I had a lot of fun with write. A little smiley here, a little cheeky something there. On rare, mischievous and slightly evil occasions a huge binary file... aaah, those were the days. It's a pity more people aren't on multi-user systems. I guess the increase in use of LTSP systems re-open the potential for this sort of fun for a lot of students, yay!
 
Old 12-05-2007, 05:20 AM   #9
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Original Poster
Rep: Reputation: 31
Sorry [KIA]aze but I was convinced with what matthewg42 said I think I will try it when I have some backup files created
BTW .dolphinview is not an extension, it is a hidden file created equivalent to thumb.db on windows to save the thumbnails of photos and stuff (I am using dolphin instead of konqueror because it is much faster and simpler but I still can not give up konqueror)
And thank you for the sites, but about the man pages I already tried that, but I think I need to already know the basics, or even a general idea about the file in order to know which command I am going to man

matthewg42:
I was using DOS when I was about 10 years so the computer was just for games but when I got a little older I used batch files to run the games from a centralized location, of course Norton Commander was there so it was not so effective and then win95 came and no more command line until I installed Linux, and I already have the alternatives to command line most of the time (even if I am compiling a software), even in this 'search and destroy' I used to do it with the find program, but it was boring so I looked for the command line as an alternative (lucky for me the dir command is working now but after a while I found that 'ls' is much better and colourful)

I do not have the chance you got, so, I would rather stick to a book or an on-line site that would give me the basics and after that I would start tyring new commands and reading man pages.

judging from your age that was 12-14 years ago, I guess KDE was just a thought then, and the Internet was not as rich as today, right? you had a lot of time indeed.

what I am trying to say is that the way you learnt is not suitable for me. Although, it could have been if I knew you a long while earlier

But thank you for the advice and for taking the time to write it.

Last edited by AGazzaz; 12-05-2007 at 05:26 AM.
 
Old 12-05-2007, 05:55 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by AGazzaz View Post
.dolphinview is not an extension, it is a hidden file created equivalent to thumb.db on windows to save the thumbnails of photos and stuff (I am using dolphin instead of konqueror because it is much faster and simpler but I still can not give up konqueror)
I didn't know it was doing that. How annoying. I hate it when program leave ugly little traces of themselves all over the place. How rude!

Quote:
Originally Posted by AGazzaz View Post
Norton Commander
If you used that a lot, maybe you would like (or perhaps already use) Midnight Commander. I think it's meant to be something along the same lines. I was never a user of that sort of thing, so I am unable to compare them.

Quote:
Originally Posted by AGazzaz View Post
I do not have the chance you got, so, I would rather stick to a book or an on-line site that would give me the basics and after that I would start tyring new commands and reading man pages.
You have the chance now... just look in /usr/bin and start playing. It's just a matter of being curious. OK, you can't annoy other users by writing to their terminals, but that was only small thing.

Quote:
Originally Posted by AGazzaz View Post
judging from your age that was 12-14 years ago
That's about right. Holy bananas what happened to those years?!
Quote:
Originally Posted by AGazzaz View Post
I guess KDE was just a thought then
I first saw it some time around 2000, but I didn't have a machine with enough memory to use it effectively. There were also QT license issues in those days IIRC. I do use it now though.
Quote:
Originally Posted by AGazzaz View Post
and the Internet was not as rich as today, right? you had a lot of time indeed.
Rich? Are you kidding?! There was archie, gopher, Mosaic ran on the sparcs and I was around for the release of the first Netscape. Muds mushes, moos and the like were booming. They were heady days!

Quote:
Originally Posted by AGazzaz View Post
what I am trying to say is that the way you learnt is not suitable for me. Although, it could have been if I knew you a long while earlier
Fair enough. The most important thing to know about learning is how to learn, and that's different for each person.
 
Old 12-05-2007, 07:17 AM   #11
AGazzaz
Member
 
Registered: Nov 2005
Location: Egypt
Distribution: Fedora 17 KDE
Posts: 381

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by matthewg42 View Post
Rich? Are you kidding?! There was archie, gopher, Mosaic ran on the sparcs and I was around for the release of the first Netscape. Muds mushes, moos and the like were booming. They were heady days!
From all what you have mentioned I only know Netscape, it was much better than IE untill it suddenly disappeared.

I looked for the others and here is what I found:
archieARCHIvE) An earlier Unix utility used to search for file names on Internet FTP sites. Considered by some as the first search engine, in its heyday before the Web, there were approximately 30 Archie servers throughout the Internet that maintained catalogs of files available for downloading from various FTP sites. Periodically, Archie servers searched the FTP sites and recorded information about the files they found.

gopher:A protocol for the storage and retrieval of text on a computer network using a TCP/IP protocol.

Mosiac: No software related results yet.

Muds mushes: I think it is a game but I am not sure though

moos: is a text-based online virtual reality system to which multiple users (players) are connected at the same time.

Well, This those are history of computers' software , I think they should make a museum for such old software.
 
Old 12-05-2007, 07:43 AM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Mosaic was a web browser. It was extremely primitive by modern standards, but it was the seed which turned into modern browsers. I don't know if it was the first graphical browser, but it was certainly the first to really achieve some degree of popularity. I think you could also use it as a gopher client too, although I seem to remember the curses-based one which we had on the sparcs was better.

MUDs are an ancestor of the modern MMORPGs like WoW. Text based of course, but non the worse for it IMO. The brain has better visual effects generators even than NVIDIA and ATI put together and then squared. MUSHES and MOOS were a little like MUDs. I think it was MUSHES which were more a sort of interactive fiction system - less hitpoints and more making stuff up.

*edit* there are still MUDs around today. Check out Nanvaent

Last edited by matthewg42; 12-05-2007 at 07:44 AM.
 
  


Reply

Tags
find, rm, xargs


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files from specified folder mrrx7 Solaris / OpenSolaris 4 08-27-2007 04:50 AM
vsftpd: allow users to write and delete files me3 Linux - Software 4 03-04-2005 08:22 PM
bash script to delete files c0d3 Programming 9 12-05-2004 11:45 PM
clients can write to files but they cannot delete them sparsh Linux - Networking 1 05-10-2003 10:54 PM
Script on linux to delete certain files ForumKid Linux - General 2 06-22-2002 02:09 PM


All times are GMT -5. The time now is 09:27 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration