[SOLVED] Moving every file of certain type from file tree?
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If I have a big file tree (for the sake of argument /home/me) and I want to move every file of a certain type recursively from /home/me to some location how can I do this?
I'm looking for something like "mv *.type /tosomelocation" but it should walk through every directory "under" the current one.
Define what you meant with type. Do you mean the difference between normal files and directory entries? Or do you mean for example that you only want text files to be moved, but not PDFs or others?
But isn't find just for searching for files in a directory hierarchy?
The point of this or rather the reason why I'm asking is because I have a pretty large and somewhat irregular file tree and I could of course search for every file with a certain type and moving it manually but this would be really tedious and time consuming work.
By type I of course mean a file with a filename ending with ".type", filename.type, grocerylist.txt, package.deb, pr0n.torrent etc.
The first one will display all located files in your command prompt, the second command will find them and place that output into the file named log.txt; for instance if the list is very large and you wish to review it. It's probably a good idea to do the find commands without the -exec first to observe what files are located and then evaluate whether or not the -exec action is what you want. For instance you may find a ton of files within a given directory structure and decide that you want to retain that directory structure; however there are other files which you do not wish to move; therefore the end result may be the original directory structure with leftover files and a newer directory structure, up to, or less than the original in scope but solely with the files you desire moved into it. To do something like that, you'd need to do a script. So be-ware that the find command I've shown you will do what you asked, but to a flat directory location, not retain a tree of files intact.
But isn't find just for searching for files in a directory hierarchy?
The point of this or rather the reason why I'm asking is because I have a pretty large and somewhat irregular file tree and I could of course search for every file with a certain type and moving it manually but this would be really tedious and time consuming work.
By type I of course mean a file with a filename ending with ".type", filename.type, grocerylist.txt, package.deb, pr0n.torrent etc.
I apologize if this is a stupid question.
Yes, it will find files, but as I've shown in my last post, you can also use the -exec argument to do stuff once you've found those files.
I really recommend you run a smaller test to observe how it works. Also be aware that if you need/want to run this on system directories (1) that's potentially dangerous depending on the files you intend to move, and (2) you'll have to do it using sudo or as root. I'm guessing/hoping that your intention is to just perform this on a data directory under a certain user structure.
Dependent on which shell you use it may be possible that you don't even have to use find. In zsh (with the extended_glob option set) all you would have to do would be
Dependent on which shell you use it may be possible that you don't even have to use find. In zsh (with the extended_glob option set) all you would have to do would be
Code:
mv **/*.type /path/to/destination
Interesting point, however it moves me to ask; is the find solution universal, versus this one which may be specific to zsh only?
I'm more of a proponent for that, being universal.
Long time ago when emacs was new, and also not yet fully GNU, we had two versions at my work. Two engineers fought profusely over them, and the styling of them and the shortcuts. The main definition file set up default key functions and mouse actions and one could also have a local profile which would read the main file first and then customize further; so a lot of people passed around profiles to grow their widgets and shortcuts. Just watching the periphery of it, I always looked at it with the same amusement those two people also did. Because they were fighting on purpose and laughing about it at the same time. One person had to sit at the keyboard of another persons login to make a change and then they'd be screaming CTRL-ALT-F1!!!! No-NO---NO!!! And their continued fight would ensue, because they had different shortcuts.
A more seasoned bystander who I worked with left the lab with me hearing the echoes of the fight declining in volume as we exited and they commented that if the two of them just threw away the whole "settings" thing and learned the actual commands that emacs natively supplied, then they'd be able to universally use emacs at any terminal, through anyone's login. The point was apt, because once I took the time to learn a few regular commands for emacs, I could then sit at a terminal that was logged in by a peer, with all their special settings for emacs, and it didn't matter, I could still select, copy, paste, cut, split windows, and so forth.
And not attacking your recommendation, sorry if it sounds that way. It firstly made me wary of the universal concern and then made me remember those former colleagues.
Interesting point, however it moves me to ask; is the find solution universal, versus this one which may be specific to zsh only?
I'm more of a proponent for that, being universal.
If you want to be universal then of course the find solution is better. I just pointed out a different solution that is much simpler if you fulfill the prerequisites (zsh with extended_glob). In the end, what would be the point in using advanced shells (I think something similar exists in Bash 4) if you don't let them make your life easy?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.