unix command to change directory name

左心房为你撑大大i 提交于 2019-12-23 05:47:14

问题


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...


回答1:


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.




回答2:


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}



回答3:


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...


来源:https://stackoverflow.com/questions/13341114/unix-command-to-change-directory-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!