LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-15-2014, 06:38 AM   #1
iwtbf
Member
 
Registered: Apr 2009
Location: cybernetic space
Distribution: debian
Posts: 42

Rep: Reputation: 1
Moving every file of certain type from file tree?


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.

Is there a neat way to do this?
 
Old 04-15-2014, 06:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
have you tried the command find? You can find a lot of examples, just google...
 
Old 04-15-2014, 06:56 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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?
 
Old 04-15-2014, 07:03 AM   #4
iwtbf
Member
 
Registered: Apr 2009
Location: cybernetic space
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 1
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.
 
Old 04-15-2014, 07:12 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Code:
find . -name "*.type" -exec mv {} /desired-location/. \;
Explanation:
  1. Linux find command
  2. . DOT means start at the current directory I am at "now", one can also type a directory name fully out, like /home/user1
  3. -name - designate the find to locate via names
  4. "*.type" - the file type search string, your wildcard, which you seem to already have an idea what you want to use
  5. -exec - "execute" the following command, up until the \; terminator
  6. mv - the Linux move command
  7. {} - replace this with the results of each find, i.e. it will find all *.type files and then execute the mv command on each individual file found
  8. /desired-location/. - Where to move the files too
  9. \; - exec command terminator
Note also that you can just run the following command, or commands:
Code:
find . -name "*.type"
find . -name "*.type" > log.txt
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.
 
1 members found this post helpful.
Old 04-15-2014, 07:15 AM   #6
iwtbf
Member
 
Registered: Apr 2009
Location: cybernetic space
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 1
That's exactly what I'm looking for! Thank you so much!
 
Old 04-15-2014, 07:15 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by iwtbf View Post
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.
 
1 members found this post helpful.
Old 04-15-2014, 08:43 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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
 
1 members found this post helpful.
Old 04-15-2014, 08:55 AM   #9
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by TobiSGD View Post
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.
 
Old 04-15-2014, 10:11 AM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by rtmistler View Post
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?
 
  


Reply



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
[SOLVED] Unziping & moving(mv) a .sh file to user directory is adding(?) ^M to file lines waddles Slackware 7 12-13-2013 05:37 PM
file type for the Linux kernel image file, (vmlinuz) is unknown james2b Linux - Software 6 11-12-2009 02:39 PM
Any good software to produce file system tree and file size report? ginda Linux - General 1 06-23-2009 04:50 AM
Get file icon from extension, file type, or mime-type Guitarist88 Programming 3 04-21-2008 10:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:08 PM.

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
Open Source Consulting | Domain Registration