find all extensions than make directories of extensions using find command?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
find all extensions than make directories of extensions using find command?
so iam trying to use find to locate all extensions recursively from all files recovered from a drive.
After i find all extensions i need to make folders for these extensions....
So far what i got is
Code:
find . -iname "*" -exec mkdir {} \;
Problem is i only need to make folders with the same name as the extensions. I some how need to subtract all text before the last period and push the results back into mkdir....
been messing with this for hours... i have found a few bash scripts that do the job, but i would rather get bits of code and put it together my self to help me understand bash scripting a little more.
i have found a few bash scripts that do the job, but i would rather get bits of code and put it together my self to help me understand bash scripting a little more.
I don't understand why you think you can't learn by studying code that works. When you understand why it works, you should be most of the way along that particular road.
but it subtracts the extension... it seems simple enough but i need to reverse it...
This doesn't subtract anything. It just echoes foo.txt.
Use pan64's ## construct. It removes the longest string that matches *. from the beginning of the word, i.e. foo. including the dot.
You can use find to generate all files, read the file list and use the ## syntax to generate a list of extensions, use sort -u to remove redundancy, then mkdir each list element.
Untested code:
Code:
find . |
while read file
do echo ${file##*.}
done |
sort -u |
xargs mkdir
EDIT: Or, now that I cleared my mind, replace the while loop with sed
Code:
sed 's/^.*\.//'
Last edited by berndbausch; 09-19-2020 at 07:59 PM.
Reason: typos
The first line is overly complicated. It is equivalent to extension="${filename##*.}". You don't need the echo and the $(...) construct (named "command substitution").
Second line: It's not quite clear what you want to achieve. Normally, the parentheses have the effect of running the text between them in a subshell, but I am not sure how the shell i terprets your line. You also want to assign something to makedirs, but what? If you actually mean makedirs=$(mkdir $filename), makedirs will contain the empty string, since mkdir doesn't output anything.
Third line is correct. You assign the list of files named blabla.testing to the variable filename. Now, the question is how you want to use this variable.
Last edited by berndbausch; 09-19-2020 at 08:15 PM.
I suppose you get Permission Denied because you have no permissions to make this directory.
The first line is overly complicated. It is equivalent to extension="${filename##*.}". You don't need the echo and the $(...) construct (named "command substitution").
Second line: It's not quite clear what you want to achieve. Normally, the parentheses have the effect of running the text between them in a subshell, but I am not sure how the shell i terprets your line. You also want to assign something to makedirs, but what? If you actually mean makedirs=$(mkdir $filename), makedirs will contain the empty string, since mkdir doesn't output anything.
Third line is correct. You assign the list of files named blabla.testing to the variable filename. Now, the question is how you want to use this variable.
So this is for a recovery i did from a raid setup for a client. Iam a bit of a bash n00b to be honest.....
He has an incredible amount of files and its near impossible for me to sort through it manually. So iam setting variables in order to get the job done. This is about as far as i know bash, but iam learning slowly. Here are the goals iam trying to achieve in steps with this small script.
1. Find all files recursively
2. Remove all text but extension from files.
3. Make directories based on all found extensions.
4. Move files by extension to the created folders.
This allows me to present the recover data in a way that makes sense to the client.
Last edited by finalturismo; 09-19-2020 at 10:08 PM.
After the first line, the filename variable contains a list of all files found.
The second line removes EVERYTHING from that list, up to the last period. This is not your intention.
It seems to me that in the third line, you want to create directories, however I have no idea why you put parentheses around the mkdir command and what you want to assign to the makedirs variable. In any case, at this point extensions probably contains the very last extension from the find command, and not all extensions.
Quote:
1. Find all files recursively
2. Remove all text but extension from files.
3. Make directories based on all found extensions.
4. Move files by extension to the created folders.
The script I suggested should take care of the first three points. To incorporate point 4, I would write a script that
checks the extension of the file
creates a corresponding directory if it doesn't exist
moves the file there
then uses that script as a find -exec parameter.
The following script assumes that all files have an extension. If that is not the case, more testing needs to be added. Again I did not try it out:
Code:
#!/bin/bash
filename=$1
extension=${filename##*.} # obtain extension
if [ ! -d $extension ] # check if directory exists
then mkdir $extension
fi
mv $filename $extension
Be aware that all the suggestions above appear to take no cognisance of hidden files. May not matter, but you might wind up with directories that don't seem like what a user might consider an extension - bash_history as a trivial example.
thats just what i was looking for , nice and simple but is there anyway i can put the bottom command inside of the bash file too? and why are we checking if a directory exists? sorry for all the questions i understand how to write most of the command but what does the ! mean
Be aware that all the suggestions above appear to take no cognisance of hidden files. May not matter, but you might wind up with directories that don't seem like what a user might consider an extension - bash_history as a trivial example.
Well, the find command explicitly excludes files that start with a dot. This may or may not be the intention; impossible to say.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.