Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Command Line Goodness - Part One

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
Home » Discuss » DU Groups » Computers & Internet » Open Source and Free Software Group Donate to DU
 
RoyGBiv Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Jun-08-10 11:32 AM
Original message
Command Line Goodness - Part One
Edited on Tue Jun-08-10 11:44 AM by RoyGBiv
FIND and DELETE

Here's the scenario. I've downloaded a bunch of pictures and PDF files that were enclosed in compressed archives. When I decompressed them, several sub-directories were created. I've moved some of the files around, and now it's a mess with no organization. What I want to do is delete all the .rar files and keep the .jpg, and .pdf files. Here's how it looks in the file explorer Dolphin:



To do this via the file browser, I would have to open all the directories and then look at each file, make sure it's one I want to get rid of and then either delete them one at a time or select them and then using CTL-<left click> select all of them then send them to the trash. To complicate matters, I actually want to keep all these directories, just not everything that's in them. Via the GUI, this is tedious.

So, I use the 'find' command.

'Find' is a very powerful application with a brazillion options. One can create extremely complex search strings and then even more complex strings of commands to act on what's found. I'm not going into all that, but here's a simple method of doing what I propose above that, when you use it enough, is very easy to remember.

I start in the top directory, below which are all the files I want to organized or delete.



I then enter this command, press enter, and let it do its work:

find . -name \*.rar -exec rm {} \;



In this particular case, this command took less than a second to complete. If I had a larger number of directories or if the files to be deleted were a lot larger in size, it would have taken longer. But all I actually had to do was type it out. Now, all the .rar files are gone, leaving me only with .pdf and .jpg files.



I find it easier on me to remember these commands if I think of what they do in terms of plain English. So, this is what that command is saying.

Find starting in the current directory in the name of the file any file ending in .rar and the execute the command to remove all these files.

I could further manipulate these files using a variation of the same command. For example, say I wanted to move all the .jpg files to a single directory. From the same directory above, I could modify the command like this:

find . -name \*.jpg -exec mv {} ~/JPG \;

That would move all the files ending in .jpg to a directory in my /home directory called JPG.

Many variations are possible and easy to do if you remember the basic structure.

FIND (find) in the CURRENT DIRECTORY AND ALL ITS SUBDIRECTORIES (.) in the FILENAME (-name) all files ENDING IN (\*) some string of characters.

You could also have it find all file names with the characters 123 somewhere in the middle name, e.g. find . \*123\*
Refresh | 0 Recommendations Printer Friendly | Permalink | Reply | Top
Syrinx Donating Member (1000+ posts) Send PM | Profile | Ignore Mon Jun-14-10 04:10 AM
Response to Original message
1. excellent stuff
Edited on Mon Jun-14-10 04:11 AM by Syrinx
I share your view that doing stuff like that is tedious with a file manager. I'm not really up to speed on 'find' and a lot of other commands, because the man pages are, well, so tedious. ;)

What I've been doing in situations like that is to typically type out a ten to twenty line python script, when it would have been much more efficient to type out a one-line command line. :)

That's a great example. Keep 'em coming!
Printer Friendly | Permalink | Reply | Top
 
RoyGBiv Donating Member (1000+ posts) Send PM | Profile | Ignore Mon Jun-14-10 06:19 AM
Response to Reply #1
2. Well, good ...
Edited on Mon Jun-14-10 06:20 AM by RoyGBiv
See, that's what I was hoping. Sometimes it's the simple things that are overlooked.

"Find" is an excellent tool, but you're right about the man pages. I know some hardcore Linux/Unix/BSD gurus with a pedigree that goes back to Berkley who don't know much about using "find." Nowadays most people just use "locate" when they want to find a specific file, which consults a database you can either update on the fly or via a cron job, but acting on the output of what you find isn't as seamless. And, "locate" isn't nearly as powerful.

I'm going to post another one sometime today.
Printer Friendly | Permalink | Reply | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Wed May 01st 2024, 11:39 PM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » DU Groups » Computers & Internet » Open Source and Free Software Group Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC