11-13-2018, 05:30 PM
Beyond Finding Stuff with the Linux find Command
![[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/11/beyond-finding-stuff-with-the-linux-find-command.jpg)
![[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/11/beyond-finding-stuff-with-the-linux-find-command.jpg)
Continuing the quest to become a command-line power user, in this installment, we will be taking on the command.
Jack Wallen already covered the basics of in an article published recently here on Linux.com. If you are completely unfamiliar with , please read that article first to come to grips with the essentials.
Done? Good. Now, you need to know that can be used to do much more than just for search for something, in fact you can use it to search for two or three things. For example:
This will cough up all the files with the extensions (or ) and (or ) in the path/to/directory directory. You can add more things to search for using the over and over.
You can also search in more than one directory simultaneously just be adding them to the route bit of the command. Say you want to see what is eating up all the space on your hard drive:
This will return all the files bigger than 500 Megabytes () in your home directory, /var and /etc.
Additionally, also lets you do stuff with the files it… er… finds. For example, you can use the action to remove everything that comes up in a search. Now, be careful with this one. If you run
will erase everything in the current directory ( is shorthand for “the current directory“) and everything in the subdirectories under it, and then the subdirectories themselves, and then there will be nothing but emptiness and an unbearable feeling that something has gone terribly wrong.
Please do not put it to the test.
Instead, let’s look at some more constructive examples…
Moving Stuff Around
Let’s say you have bunch of pictures of Tux the penguin in several formats and spread out over dozens of directories, all under your Documents/ folder. You want to bring them all together into one directory (Tux/) to create a gallery you can revel in:
Let’s break this down:
Once you have the basics of modifying files using under your belt, you will discover all sorts of situations where it comes in handy. For example…
A Terrible Mish-Mash
Client X has sent you a zip file with important documents and images for the new website you are working on for them. You copy the zip into your ClientX folder (which already contains dozens of files and directories) and uncompress it with and, gosh darn it, the person who made the zip file didn’t compress the directory itself, but the contents in the directory. Now all the images, text files and subdirectories from the zip are all mixed up with the original contents of you folder, that contains more images, text files, and subdirectories.
You could try and remember what the original files were and then move or delete the ones that came from the zip archive. But with dozens of entries of all kinds, you are bound to get mixed up at some point and forget to move a file, or, worse, delete one of your original files.
Looking at the files’ dates () won’t help either: the Zip program keeps the dates the files were originally created, not when they were zipped or unzipped. This means a “new” file from the zip could very well have a date prior to some of the files that were already in the folder when you did the unzipping.
You probably can guess what comes next: to the rescue! Move into the directory (), make a new directory where you want the new stuff to go (), and then try this:
Breaking that down:
If you are unsure of anything may do, you can swap for . The option forces to check with you before it runs the command you have given it. Accept an action by typing y or reject it with n.
Next Time
We’ll be looking at environmental variables and a way to search even more deeply into files with the command.
![[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/11/beyond-finding-stuff-with-the-linux-find-command.jpg)
![[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]](http://www.sickgaming.net/blog/wp-content/uploads/2018/11/beyond-finding-stuff-with-the-linux-find-command.jpg)
Continuing the quest to become a command-line power user, in this installment, we will be taking on the
Code:
findJack Wallen already covered the basics of
Code:
findCode:
findDone? Good. Now, you need to know that
Code:
findCode:
find path/to/some/directory/ -type f -iname '*.svg' -o -iname '*.pdf'This will cough up all the files with the extensions
Code:
svgCode:
SVGCode:
pdfCode:
PDFCode:
-oYou can also search in more than one directory simultaneously just be adding them to the route bit of the command. Say you want to see what is eating up all the space on your hard drive:
Code:
find $HOME /var /etc -size +500MThis will return all the files bigger than 500 Megabytes (
Code:
-size +500MAdditionally,
Code:
findCode:
-deleteCode:
# WARNING: DO NOT TRY THIS AT $HOME find . -iname "*" -deleteCode:
findCode:
.Please do not put it to the test.
Instead, let’s look at some more constructive examples…
Moving Stuff Around
Let’s say you have bunch of pictures of Tux the penguin in several formats and spread out over dozens of directories, all under your Documents/ folder. You want to bring them all together into one directory (Tux/) to create a gallery you can revel in:
Code:
find $HOME/Documents/ \( -iname "*tux*png" -o -iname "*tux*jpg" -o -iname "*tux*svg" \) -exec cp -v '{}' $HOME/Tux/ \;Let’s break this down:
Once you have the basics of modifying files using
Code:
findA Terrible Mish-Mash
Client X has sent you a zip file with important documents and images for the new website you are working on for them. You copy the zip into your ClientX folder (which already contains dozens of files and directories) and uncompress it with
Code:
unzip newwebmedia.zipYou could try and remember what the original files were and then move or delete the ones that came from the zip archive. But with dozens of entries of all kinds, you are bound to get mixed up at some point and forget to move a file, or, worse, delete one of your original files.
Looking at the files’ dates (
Code:
ls -la *You probably can guess what comes next:
Code:
findCode:
cd path/to/ClientXCode:
mkdir NewStuffCode:
find . -cnewer newwebmedia.zip -exec mv '{}' NewStuff \;Breaking that down:
- The period () tellsCode:
.to do its thing in the current directory.Code:find
- tellsCode:
-cnewerto look for files that have been changed at the same time or after a certain file you give as reference. In this case the reference file isCode:find. If you copied the file over at 12:00 and then unpacked it at 12:01, all the files that you unpacked will be tagged as changed at 12:01, that is, afterCode:newwebmedia.zipand will match that criteria! And, as long as you didn’t change anything else, they will be the only files meeting that criteria.Code:newwebmedia.zip
- The part of the instruction simply tellsCode:
-execto move the files and directories to theCode:finddirectory, thus cleaning up the mess.Code:NewStuff/
If you are unsure of anything
Code:
findCode:
-execCode:
-okCode:
-okCode:
findNext Time
We’ll be looking at environmental variables and a way to search even more deeply into files with the
Code:
grep
