Find all pdf an get make a folder from filename substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find all pdf an get make a folder from filename substring
# 1  
Old 11-14-2017
Find all pdf an get make a folder from filename substring

Hi ,

i need your advice.

i will find all *.pdf files and make a folder for every different prefix of file names.

for example:

Code:
test_21424234.pdf
new_242342.pdf

at the and i will that i create ( if not exits ) a new folder "test" and "new" , afterwards i will move the file in this new folder.

i hope you kann help me

so long Maxwill

PS: sry for my engilsh


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-14-2017 at 05:16 AM.. Reason: Added CODE tags.
# 2  
Old 11-14-2017
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
# 3  
Old 11-14-2017
Hi,

yes i have bulid this

Code:
VAR=`find . -name *.pdf -print`
for file in $VAR;
do
   fname=`basename $file`
   prefix=${fname%_*}
   mkdir -p ./$prefix
   mv $file ./$prefix/
done

but i get a problem when i have files like :
Code:
test_234234_22433.pdf

i will only the first letters until the first underscore.

so long Maxwill

Last edited by Maxwill; 11-14-2017 at 06:41 AM..
# 4  
Old 11-14-2017
Have you tried:
Code:
prefix=${fname%%_*}

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to find a filename of a folder?

Hi, Please help me i have a file directory in .txt. inside this directory is a list of names of folders to find inside in a folder. to make more easy to understand below is my list inside .txt: foldernameA foldernameB foldernameC I need to check if those folder names are inside... (1 Reply)
Discussion started by: cmarzan
1 Replies

2. UNIX for Dummies Questions & Answers

Command - filename as arguments - make executable to all users.

Edit: Sorry. Mistakenly posted - please delete (3 Replies)
Discussion started by: Reddax
3 Replies

3. Shell Programming and Scripting

How to make parallel execution on folder ?

I have few very huge files ~ 2 Billion rows of 130 column(CDR data) in a folder, I have written shell script need to read on each file in a folder and will create a new files based on some logic. But problem is it's taking time to create a new file due to the size , So i dont want to corrupt... (6 Replies)
Discussion started by: rspwilliam
6 Replies

4. Shell Programming and Scripting

Mv series out of mixed folder & identify substring

Dear unix-Community, great to be here! Actually i try to build a script to sort out my serials into an series-folder. Reason is: plex cant handle mixed folder filled with other stuff than series only. First shot was ls in combination with grep and regex. Got no positiv result. Then i... (3 Replies)
Discussion started by: Zack
3 Replies

5. Shell Programming and Scripting

Compare filename to file folder

Hi. I have a problem, could you help me ? Situation: there are alot of files in directory_1, with the names like "notneeded1blabla.bla", "notveryneededblabla.gla". there is directory_2 that has directories with names like that: "bla_needed1", "blab_veryneeded". What i need so much is to place... (7 Replies)
Discussion started by: sw_and
7 Replies

6. Shell Programming and Scripting

Checking files in folder using starting string for filename

Hi, How do i check if there are any files present in the folder with some specific starting string. For eg :- I have used this where Source_File is filename parameter. if then return 2 fi But in my case the source file name is not constant. The only constant thing is... (10 Replies)
Discussion started by: chetancrsp18
10 Replies

7. Shell Programming and Scripting

Find folder within folder, then find other folder in same dir

Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do: I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory... (9 Replies)
Discussion started by: DC Slick
9 Replies

8. UNIX for Dummies Questions & Answers

How to make a make from other folder

Hi, Sorry for my English. I want Execute a make from other folder but no it's a normal make. The comand is: make telosb install,3 And for example if i have to execute this comand in /tmp and i am in /$HOME how he would be now the comand? thx (7 Replies)
Discussion started by: Grobix
7 Replies

9. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. UNIX for Dummies Questions & Answers

Jar/Tar to a diffent folder/same folder w/ filename

Hi, I want to extract myfile.war to a folder which is in the same folder with war file.I did this as normal: jar -xvf myfile.war But it exploded all the content of file to the same level folder instead of that I was expecting to create a folder called myfile. This works with tar: ... (0 Replies)
Discussion started by: reis3k
0 Replies
Login or Register to Ask a Question