0

I have several folders with names like:

  • 4553, 6723, 7765.

Inside I have a file with the same name in pdf:

  • 4553.pdf, 6723.pdf, 7765.pdf

I would like to move to each folder and move the .pdf's so that they are all in one folder like "allpdf" with a script

I had tried this:

for folder in *; do
  cd $folder
  mv "*.pdf" "../"
done
Toto
  • 89,455
  • 62
  • 89
  • 125
GuiEpi
  • 161
  • 8
  • Does this answer your question? [Bash move file to folder of same name](https://stackoverflow.com/questions/49099062/bash-move-file-to-folder-of-same-name) – Renat May 12 '21 at 11:02
  • In his case I think he moves the files to a folder of the same name, I would like to remove them from their folder – GuiEpi May 12 '21 at 11:05
  • Have you tried anything? Seems like a `for` loop with `mv` should do. – choroba May 12 '21 at 11:09
  • I'm posting what I tried with no results in my answer – GuiEpi May 12 '21 at 11:11
  • `files=(*/*.pdf); mv -v "${files[@]}" ../allpdf/` probably needs `shopt -s nullglob` – Jetchisel May 12 '21 at 16:26

2 Answers2

0

Your first cd puts you in the directory, and you stay there, so the next cd cannot work (you're one level lower than what you may think).

So, either you do that:

for folder in *; do
  cd $folder
  mv "*.pdf" "../"
  cd ..
done

Or (preferably, in my opinion), you do that:

for folder in *; do
  mv "$folfer/*.pdf" "."
done
  • I have this error: 'mv: unable to evaluate "*.pdf": No such file or folder'. you think it is looking for a file named "*.pdf"? Moreover thank you for your answer – GuiEpi May 12 '21 at 11:24
  • There is a * in front of the .pdf – GuiEpi May 12 '21 at 11:26
  • 1
    You probably want "${folder}/${folder}.pdf" or you might get more than you want. – Wayne Vosberg May 12 '21 at 11:27
  • Yes Wayne Vosberg is right. This should work as well with *.pdf as long as you're using double quotes " around the * and not simple quote ' – Jean-Loup Sabatier May 12 '21 at 11:29
  • Yes @WayneVosberg the solution is good thanks! But I don't know why when my script is in the folder that contains all the folders '5566', '4445', '6435'... and when I do a 'ls' I see that it applies the script in another folder... – GuiEpi May 12 '21 at 11:35
  • It runs the command in my home folder '/home/myname'. – GuiEpi May 12 '21 at 11:37
0

Minimal and Simple Solution:

If you're using bash shell, then try this: You should be in the parent directory of all the subdirectories with pdf files in it. So, let's call your parent directory as pdf_parent_dir and this directory should contain in it the folders 4553, 6723, 7765 etc with pdf files in them, then run the commands as below:

# moving to the desired parent directory    
cd pdf_parent_directory

# to move files to this directory    
mkdir allpdf 

# to enable bash star globbing    
shopt -s globstar 

# finally moving all pdf files from subdirectories to allpdf directory    
mv **/*.pdf allpdf/

Based on what you mentioned there shouldn't be any files with same name, but if there are, then the mv command will fail as it tries to move files with same name into the final allpdf/ directory

I tried this on my Linux machine by creating a similar folder and file structure as you mentioned and it worked as expected. If you face any errors, let me know

Pavan
  • 632
  • 5
  • 9