h.l.m
h.l.m

Reputation: 13485

unix command to change directory name

Hi this is a simple question but the solution eludes me at the moment..

I can find out the folder name that I want to change the name of, and I know the command to change the name of a folder is mv

so from the current directory if i go

ls ~/relevant.directory.containing.directory.name.i.want.to.change

to which i get the name of the directory is called say lorem-ipsum-v1-3

but the directory name may change in the future but it is the only directory in the directory: ~/relevant.directory.containing.directory.name.i.want.to.change

how to i programmatically change it to a specific name like correct-files

i can do it normally by just doing something like

 mv lorem-ipsum-v1-3 correct-files

but I want to start automating this so that I don't need to keep copying and pasting the directory name....

any help would be appreciated...

Upvotes: 0

Views: 6308

Answers (3)

Stephen Darlington
Stephen Darlington

Reputation: 52575

cd ~/relevant.directory.containing.directory.name.i.want.to.change 
find . -type d -print | while read a ;
do
  mv $a correct-files ;
done

Caveats:

  • No error handling
  • There may be a way of reversing the parameters to mv so you can use xargs instead of a while loop, but that's not standard (as far as I'm aware)
  • Not parameterised
  • If there any any subdirectories it won't work. The depth parameters on the find command are (again, AFAIK) not standard. They do exist on GNU versions but seem to be missing on Solaris
  • Probably others...

Upvotes: 1

user507577
user507577

Reputation:

Something like:

find  . -depth -maxdepth 1 -type d | head -n 1 | xargs -I '{}' mv '{}' correct-files

should work fine as long as only one directory should be moved.

Upvotes: 3

dogbane
dogbane

Reputation: 274838

If you are absolutely certain that relevant.directory.containing.directory.name.i.want.to.change only contains the directory you want to rename, then you can simply use a wildcard:

mv ~/relevant.directory.containing.directory.name.i.want.to.change/*/ ~/relevant.directory.containing.directory.name.i.want.to.change/correct-files

This can can also be simplified further, using bash brace expansion, to:

mv ~/relevant.directory.containing.directory.name.i.want.to.change/{*/,correct-files}

Upvotes: 2

Related Questions