Re: create directories based on directory contents



On 29 Jan 2007, in the Usenet newsgroup comp.os.linux.misc, in article
<1170129250.226737.308410@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
gmail@xxxxxxxxxxxxxxxx wrote:

Suppose I have the following directory structure:

[...]

is it possible to issue some commands that will automatically create 3
subdirectories, AAA, XXXXX, ZZ and subsequently move all the files
into these folders?

Yes, but you'd spend more time creating a script to do so than just
doing

ls *jpg | sed 's/_[0-9]*.jpg//' | column

to get a list of the letter prefixes, then manually creating the
directories, and finally doing

mv AAA*jpg AAA

and so on. A UNIX guru will often spend six hours to create a tool that
will do a two hour job in ten minutes - but only if it looks as if the
tool will be used several times. If it's a one-time job, doing things
manually is actually more efficient.

Now, if this is a homework assignment, there are several ways to create
a script to do this job. I'd probably use a shell glob through a 'sed' to
get the prefix, through a 'sort -u' to get a variable, and use that in a
simple loop to create the directory, and do the move. Something on the
idea of

for VAR in `ls *jpg | sed 's/_[0-9]*.jpg//' | sort -u`
do
mkdir $VAR
mv $VAR_* $VAR
done

although that probably has at least one error (hey - I haven't finished
my first cup of coffee), and may blow up violently. As a matter of fact,
I even see one error already - but this is just displaying the concept.

Old guy
.



Relevant Pages

  • Re: window object
    ... Within a script, every reference to an object amounts to two ... calls from the scripting engine to the DHTML Object Model. ... var sText = document.all.div1.innerText; ...
    (comp.lang.javascript)
  • Re: Change IP related values due to WAN design change
    ... Unknown network, ... For the cost of testing a dangerous script in a big ... > recurse your text file into an array. ... > var newSettings = new NetworkSettings; ...
    (microsoft.public.windows.server.scripting)
  • Re: problem with logoff script
    ... the script engine as a process on the workstation and should execute cleanly ... var fso = new ActiveXObject; ... sho.popup('An error occured attempting to get the Operating System Type. ... // Retrieve the script application ...
    (microsoft.public.win2000.group_policy)
  • Re: Script for adding and deleting rows to a table
    ... that is one long script. ... var numRows = document.getElementById.rows.length; ... //Get Reference to cell that needs to be changed ...
    (comp.lang.javascript)
  • Re: Display a block of text in Firefox & Safari
    ... in FireFox and Safari it appears as a narrow ... There seems to be a needless reliance on the global variable 'NumberOfQuestionsShown', I've suggested a different strategy below that should be easier to maintain - it uses a single class to hide/show questions so the script doesn't need to know how many questions there are nor do they need to be consecutively numbered. ... var questionNum; ... Instead of going through all questions, how about giving them all the same style, then just modify the style to change display from 'none' to ''. ...
    (comp.lang.javascript)