Unix/Linux Go Back    


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

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Maxwill
Maxwill's Unix or Linux Image
Maxwill Maxwill is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 14 November 2017, 6:51 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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; 4 Weeks Ago at 05:16 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Maxwill
RudiC's Unix or Linux Image
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 December 2017, 6:44 AM EST
Location: Aachen, Germany
Posts: 11,815
Thanks: 341
Thanked 3,659 Times in 3,357 Posts
Welcome to the forum.

Any attempts / ideas / thoughts from your side?
Sponsored Links
    #3  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Maxwill
Maxwill's Unix or Linux Image
Maxwill Maxwill is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 14 November 2017, 6:51 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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; 4 Weeks Ago at 06:41 AM..
    #4  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Maxwill
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 December 2017, 12:04 AM EST
Location: Amsterdam
Posts: 11,660
Thanks: 521
Thanked 3,386 Times in 2,985 Posts
Have you tried:

Code:
prefix=${fname%%_*}

?
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:06 AM
Mv series out of mixed folder & identify substring Zack Shell Programming and Scripting 3 02-22-2014 06:10 PM
Compare filename to file folder sw_and Shell Programming and Scripting 7 04-02-2012 06:30 AM
Find all text files in folder and then copy to a new folder cgkmal Shell Programming and Scripting 4 06-20-2009 02:12 PM
Jar/Tar to a diffent folder/same folder w/ filename reis3k UNIX for Dummies Questions & Answers 0 03-25-2009 11:00 AM



All times are GMT -4. The time now is 07:52 AM.