[opensuse] Scripting help
I'm looking for shell help.. I have a directory with thousands of files named in the format 'file_XX_YY.txt' where XX would represent 2 characters and/or digits and YY is numbers from 00 to 99. What I want to do is to move all files named 'file_XX*' to directories named as 'file_XX'. Any suggestions for a command line or script doing just that? -- Anders Norrbring Norrbring Consulting -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders, On Monday 12 March 2007 08:42, Anders Norrbring wrote:
I'm looking for shell help.. I have a directory with thousands of files named in the format 'file_XX_YY.txt' where XX would represent 2 characters and/or digits and YY is numbers from 00 to 99.
What I want to do is to move all files named 'file_XX*' to directories named as 'file_XX'.
Generate the list of directories: dirNames=( $(ls |sed -rn 's/file_(..)_[0-9][0-9].txt/file_\1/p' |sort -u ) ) Iterate over those directory names, creating the directory if necessary and then moving all the corresponding files into that directory: for dirName in "${dirNames[@]}"; do mkdir -p "$dirName" for fileName in "$dirName"_[0-9][0-9].txt; do mv "$fileName" "$dirName" done done
Any suggestions for a command line or script doing just that? --
Anders Norrbring Norrbring Consulting
What sort of consulting do you do? Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz skrev:
Anders,
On Monday 12 March 2007 08:42, Anders Norrbring wrote:
I'm looking for shell help.. I have a directory with thousands of files named in the format 'file_XX_YY.txt' where XX would represent 2 characters and/or digits and YY is numbers from 00 to 99.
What I want to do is to move all files named 'file_XX*' to directories named as 'file_XX'.
Generate the list of directories:
dirNames=( $(ls |sed -rn 's/file_(..)_[0-9][0-9].txt/file_\1/p' |sort -u ) )
Iterate over those directory names, creating the directory if necessary and then moving all the corresponding files into that directory:
for dirName in "${dirNames[@]}"; do
mkdir -p "$dirName"
for fileName in "$dirName"_[0-9][0-9].txt; do mv "$fileName" "$dirName" done
done
Thanks Randall, I bet you didn't even think about it either.. :) I really suck on shells.
Any suggestions for a command line or script doing just that? --
Anders Norrbring Norrbring Consulting
What sort of consulting do you do?
Mostly hardware and networks, I'm specialized in network design and security. Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders, On Monday 12 March 2007 09:02, Anders Norrbring wrote:
Randall R Schulz skrev:
Anders,
On Monday 12 March 2007 08:42, Anders Norrbring wrote:
I'm looking for shell help.. ...
done
Thanks Randall, I bet you didn't even think about it either.. :)
_That_ would be a big mistake! However, this is a straightforward task. I do use shells the way many people use Perl (I used to use Perl a lot, but we've had a falling out, as it were...). That's not necessarily all for the better. There are certainly some things that are very clumsily done in the shell.
I really suck on shells.
Well, they've become very powerful programming languages over the years. It would probably be worth your while to get a good book (I've used "Linux Shell Scripting with Bash" by Burtch) and force yourself to acquire the basic knowledge and techniques. It will serve you well, I think.
...
Anders
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Norrbring wrote:
I'm looking for shell help.. I have a directory with thousands of files named in the format 'file_XX_YY.txt' where XX would represent 2 characters and/or digits and YY is numbers from 00 to 99.
What I want to do is to move all files named 'file_XX*' to directories named as 'file_XX'.
Any suggestions for a command line or script doing just that?
get also a look at "mmv" RTFM necessary, but powerfull jdd -- http://www.dodin.net Lucien Dodin, inventeur http://lucien.dodin.net/index.shtml -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (3)
-
Anders Norrbring
-
jdd
-
Randall R Schulz