0

i am still new in this shell script. I have a task that given to me which i have difficulty to execute it. The task is i have a lot of directory which is based on ddmmyy.

03Mar2014 08Aug2013 11Jan2015 16Jan2014 22Feb2014 26Mar2014 03Nov2013 08Jan2014 11Jul2013 16Jul2013 22Jul2013 26Oct2014 03Oct2013 08Jan2015 11Nov2014 16May2014 22Mar2014 26Sep2013 

The task is to make the directory to mmyy.

So far, my code is

foreach file(`find . -type d | awk -F/ 'NF == 3'`) echo $file set newmove = `echo $file | cut -c 1-2,5-` echo $newmove mv $file $newmove output: for find . -type d | awk -F/ 'NF == 3': ./24Jan2015/W51A `echo $file | cut -c 1-2,5-` ./Jan2015/W51A mv $file $newmove mv: cannot rename ./24Jan2015/W51A to ./Jan2015/W51A: No such file or directory 

but the script didnt work. Do you guys have any idea how to do this?

1
  • 2
    sh and especially bash are much better alternatives than csh. This is a bit over-the-top, but its technical points are accurate: perl.com/doc/FMTEYEWTK/versus/csh.whynot Commented Oct 20, 2015 at 0:55

1 Answer 1

0

First note: your example shows files in the ddmmyy format (in the same dir), not dd/mm/yy. If it's indeed so your find . -type d | awk -F/ 'NF == 3' won't find them, thus foreach would complain due to empty arg list:

> foreach ( '' ) foreach: Variable name must begin with a letter. 

Second note: assuming you find the files your cut statement doesn't quite get the proper corresponding destination dir (regardless which of ddmmyy or dd/mm/yy is the correct filename format):

> echo '08Aug2013' | cut -c 1-2,5- 08g2013 > echo '08/Aug/2013' | cut -c 1-2,5- 08ug/2013 

Your echo statements should have pointed out that your script is not doing the expected job.

Trying each piece of the code by itself to make sure it does what's expected of it is usually a good starting point in debugging scripts. Start from the beginning, with find . -type d. Comment out the move statement until the echo statements show correct/expected script operation.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.