0

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.

2 Answers 2

0

You can do:

Assuming you're getting:

filename='Downloads/directory1/math.pdf'

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

mkdir -p "$dir"

will create mathdirectory

Sign up to request clarification or add additional context in comments.

2 Comments

although, ia get the following error,syntax erroe near unexpected token 'dir="${f%.*}', this is my script so far files='Downloads/directory1/*.pdf' for f in "${files##*/}" dir="${f%.*}" mkdir -p "$dir" done
@user2927819: Make sure you're using BASH
0

try with:

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

4 Comments

Instead of the sed pipeline, consider using basename.
The sed pipeline can be used for adapting the name to whatever you need, e.g filename_dir. It's more flexible than basename, but it's just my opinion.
however, i get several "cannot create directory" errors and the following folders are created instead | , echo and $f
I've updated the answer. I forgot an important issue: files might contain spaces. BTW, pay attention to the ` character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.