Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Find all pdf an get make a folder from filename substring

👤 Login to reply

    #1  
Old 11-14-2017
Maxwill Maxwill is offline
Registered User
 
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:
Find all pdf an get make a folder from filename substring Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-14-2017 at 04:16 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 11-14-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
Sponsored Links
    #3  
Old 11-14-2017
Maxwill Maxwill is offline
Registered User
 
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 05:41 AM..
    #4  
Old 11-14-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Have you tried:
Code:
prefix=${fname%%_*}

?
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find a filename of a folder? cmarzan UNIX for Dummies Questions & Answers 1 03-18-2015 03:06 AM
Mv series out of mixed folder & identify substring Zack Shell Programming and Scripting 3 02-22-2014 05:10 PM
Compare filename to file folder sw_and Shell Programming and Scripting 7 04-02-2012 05:30 AM
Find all text files in folder and then copy to a new folder cgkmal Shell Programming and Scripting 4 06-20-2009 01:12 PM
Jar/Tar to a diffent folder/same folder w/ filename reis3k UNIX for Dummies Questions & Answers 0 03-25-2009 10:00 AM



All times are GMT -4. The time now is 01:44 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?