LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to write a script to delete files? (https://www.linuxquestions.org/questions/linux-general-1/how-to-write-a-script-to-delete-files-604352/)

AGazzaz 12-04-2007 03:54 AM

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

[KIA]aze 12-04-2007 04:20 AM

This should be enough ("-r" stands for recursive):
Code:

rm -r *~ *.dolphinview
The "dolphinview files" are files ending in ".dolphinview", right?

colucix 12-04-2007 04:32 AM

Quote:

Originally Posted by [KIA]aze (Post 2979568)
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.

matthewg42 12-04-2007 05:06 AM

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. :)

colucix 12-04-2007 05:31 AM

Good insight, Matthew! Thank you for the detailed notice :)

AGazzaz 12-04-2007 12:38 PM

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?:study:

[KIA]aze 12-04-2007 07:42 PM

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.

matthewg42 12-05-2007 01:34 AM

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! :D

AGazzaz 12-05-2007 04:20 AM

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 :D

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

matthewg42 12-05-2007 04:55 AM

Quote:

Originally Posted by AGazzaz (Post 2980774)
.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 (Post 2980774)
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 (Post 2980774)
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 (Post 2980774)
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 (Post 2980774)
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 (Post 2980774)
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 (Post 2980774)
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 :D

Fair enough. The most important thing to know about learning is how to learn, and that's different for each person.

AGazzaz 12-05-2007 06:17 AM

Quote:

Originally Posted by matthewg42 (Post 2980790)
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:
archie:(ARCHIvE) 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.

matthewg42 12-05-2007 06:43 AM

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


All times are GMT -5. The time now is 06:34 AM.