ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I'm trying to write a simple bash script to sort files based on their extensions. Mostly this is for sorting images. I would like to be able to go into a directory which may contain any combination of several filetypes and put them into separate directories.
Basically I'm thinking of this being a series of if/thens... sort of like this:
if [ there are any files ending with .tif ]
then
mkdir tif
mv *.tif tif/
fi
Originally posted by otheralex I'm trying to write a simple bash script to sort files based on their extensions. Mostly this is for sorting images. I would like to be able to go into a directory which may contain any combination of several filetypes and put them into separate directories.
Basically I'm thinking of this being a series of if/thens... sort of like this:
if [ there are any files ending with .tif ]
then
mkdir tif
mv *.tif tif/
fi
and so on for .jpg, .dng, etc...
thanks!
Did you have a specifc question about what you wanted to do?
yes, sorry for not making my original post more clear.
What I don't know how to do is write a script that will look for any files whose names end with a specific extension and if there are any such files, put them in a new directory.
So if the action of the script was written in plain English is would say "if there are any files ending with ".tif" then do the following..."
Originally posted by otheralex yes, sorry for not making my original post more clear.
What I don't know how to do is write a script that will look for any files whose names end with a specific extension and if there are any such files, put them in a new directory.
So if the action of the script was written in plain English is would say "if there are any files ending with ".tif" then do the following..."
I hope that clears is up.
Thanks.
Well, you can probably just use pattern matching in the shell. E.g.
Code:
#Move all .txt files to subdir txt.
#Only create txt subdir if at least 1 file with .txt extension exists.
for i in $(ls *.txt); do
mkdir txt
break
done
mv *.txt txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.