Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Many years ago, I converted a portion of my files to an arbitrary format with a specific extension. i no longer desire to have them in this format and i would like begin the process of replacing them because conversion is not an appropriate solution. unfortunately, they are mixed in separate folders of the same root folder with files in my current format of a different extension. I feel it would make this process easier if I were to move every folder that contained a file with the undesired format to a separate root folder. The files are stored on a Linux server and shared via samba. How can I do this with a couple of commands or a script? I am open to other suggestions as well. I want to avoid time spent editing text files. Ultimately, I'd like a command that produced a list of full paths for folders, sorted by the number of levels would be a nice touch. A list of all of the files is clearly not what I'm looking for.

share|improve this question
up vote 2 down vote accepted

find . -type f | sed "s#^.#$(pwd)#"

returns full paths for all files + a leading / when run at root you could pipe that to grep and match the extension followed by and endline

drop that in a text file for manipulations

you can now count the /'s in a line and split the filename off the end and mv $line to /adir/filename

this could all be in a single bash script...i am not quit good enough at the bash scripting to make it a oneliner but this gets you most of the way there. you may want to watch out for file clobbering if you do this though.

just how i would handle it


since evidently it's my job to write you a fully functional script since you can't read online faqs or respond when asked in what way it fails:

find . -type f | sed "s#^.#$(pwd)#" | grep '.xml'> test.txt  
./ > test2.txt begin----  
while read line  
dirname $line  

What else do you need scripts for?? because i really have nothing better to do

the output from that script is only directory names that contained files ending in .xml or directories ending in .xml

share|improve this answer
this hasn't really worked for me. it's very incomplete and it seems like i've been able to reproduce the same (incorrect) output without involving sed. – Joshua Kersey Oct 3 '10 at 5:01
then you add duplicate removal – RobotHumans Oct 4 '10 at 0:27
I was able to add duplicate removal by using the uniq command. since the results of the find all duplicate lines were adjacent so '''uniq''' worked for me. – Joshua Kersey Oct 7 '10 at 19:15

The SOURCE should be the upper most directory you are searching. If the files are stored all throughout your file system then SOURCE should be the file system itself.

find ~/SOURCE -name '*.YOUR_EXTENSION' -exec mv {} ~/DESTINATION \;

Rename them like this:


It would be a good idea to run this with the -n option instead of the -v option BEFORE you actually want to do the rename, because the -n option will just run a simulation and give you the results of that simulation.

share|improve this answer
Ok, this still isn't what I want, however you were able to move me a little closer and perhaps can provide an intermediary step. The problem with your solution is it still is dealing with presenting output of a list of files. I instead want output of a list of folders that contain the file. With your example I'm able to use "dirname" to obtain a list of folders, but each folder is listed once for each file. There's tens of thousands of files and hundreds of folders. I just need the folders. – Joshua Kersey Oct 3 '10 at 16:20
@Good Time Tribe ahh, I see. I think you can simply add /*/ in front of .YOUR_EXTENSION on the find command I gave you so the command looks like find ~/SOURCE -name '/*/*.YOUR_EXTENSION' -exec mv {} ~/DESTINATION \; – ubiquibacon Oct 4 '10 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .