Name directories with "0s" | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Name directories with "0s"

UNIX for Dummies Questions & Answers


Tags
directory naming, dividing files

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-11-2012
raya raya is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 11 October 2012, 1:41 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Name directories with "0s"

Hi everyone,

I want to create 4000 directories with names like dir0001 ... dir 0100 till dir4000 and move 100 files in each.

I am using this :

Code:
ls|xargs -n100|awk ' {i++;system("mkdir dir"i);system("mv "$0" -t dir"i)}'

I get the directory names at dir1, dir2 etc. which is not good for later sorting of directories.

Thanks.

Moderator's Comments:
Please use code tags next time for your code and data.

Last edited by vbe; 10-11-2012 at 08:36 AM..
Sponsored Links
    #2  
Old 10-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
try something like this..



Code:
  for i in {1..1000}
 do
 dir_name=$(printf "%04d" "$i")
 mkdir "$dir_name"
 mv files "$dir_name"
 done


Last edited by pamu; 10-11-2012 at 09:12 AM.. Reason: corrected after elixir's advise..:)
Sponsored Links
    #3  
Old 10-11-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Quote:
Originally Posted by pamu View Post
try something like this..



Code:
 for i in {1..1000}
 do
 dir_name=$(echo $i | awk '{ printf "%04d",$0}{print ""}')
 mkdir "$dir_name"
 mv files "$dir_name"
 done

You forget that there is an external command/shell built-in called printf , too.
So, that's a useless use of awk .

Last edited by elixir_sinari; 10-11-2012 at 09:11 AM..
    #4  
Old 10-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by elixir_sinari View Post
You forget that there is an external command/shell built-in called printf , too.
So, that's a useless use of awk .
Yeah. Thanks...
edited in original post.
This might be the first time useless use of awk ...
Sponsored Links
    #5  
Old 10-11-2012
raya raya is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 11 October 2012, 1:41 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for all replies !!
Sponsored Links
    #6  
Old 10-12-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 3 October 2014, 8:50 AM EDT
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts
Quote:
Originally Posted by pamu View Post
try something like this..



Code:
  for i in {1..1000}
 do
 dir_name=$(printf "%04d" "$i")
 mkdir "$dir_name"
 mv files "$dir_name"
 done

Need to add dir in the printf


Code:
 
printf "dir%04d" "$i"

Sponsored Links
Closed Thread

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
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
ps -ef | grep "string1" "string2" " "string3" steve2216 Shell Programming and Scripting 11 09-03-2010 11:08 AM
"find command" to find the files in the current directories but not in the "subdir" swamymns Shell Programming and Scripting 9 07-22-2008 11:23 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM



All times are GMT -4. The time now is 07:59 PM.