create multiple folders using existing file names in linux

≯℡__Kan透↙ 提交于 2019-12-13 04:55:13

问题


i have downloaded several pdf files(with various names) and stored them in a specific directory (for example, Downloads/directory1).

i would like to create folders to store these files under the same directory, using the names of the original files, but without the extension pdf.

for example, for a file named maths.pdf, i want to create the folder maths and store the pdf in it.

i made several attempts creating a script using commands as basename and for, but i didn't have any luck.


回答1:


You can do:

Assuming you're getting:

filename='Downloads/directory1/math.pdf'

f="${filename##*/}"
dir="${f%.*}"

mkdir -p "$dir"

will create mathdirectory




回答2:


try with:

  cd Downloads/directory1
  for f in *.pdf; do
     fname=${echo $f | sed "s/.pdf//" }
     echo "Creating directory [$fname]"
     mkdir "$fname"
  done


来源:https://stackoverflow.com/questions/19633452/create-multiple-folders-using-existing-file-names-in-linux

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