Thursday, August 22, 2013

How to Use Awk or Sed Commands to Move Files Within a Subdirectory

looking for help in awk command 

Basically I want to move some files of 2005 in a directory to a subdirectory within. 

This is how list the files of 2005. 

ls -l|grep "2005" 

Now from here how can I use awk or sed command to move these files within a subdirectory
You might get more specific with the criteria that "awk" uses, so that the match for 2005 MUST be in the date portion of "ls" (and you don't match with a file that accidently has a size/len of 2005, or a file from 2009 that has 2005 in the file name) 

$ ls -l | awk '$8= 
05{print "mv",$9,"new/dir"}' | sh -xIf your files do NOT have 2005 in the file name 
(and you were just grepping for DATE column that contained 2005) 
then the suggestions above won't really work. 
And you might go with: 
$ ls -l | awk '/2005/{print "mv",$9,"new/dir"}' | sh -xno need any awk or sed command!!! 
if your files's name characterized as *2005* then you coould use; 
mv *2005* /subdir_path_addressAwk or sed are not required, they would only have to spawn a shell to call 
'mv' anyway and create more overhead. Try: 
ls -l | grep 2005 | xargs -I{} mv {} somedir 

If there are a lot (thousands) of files, it would be faster to use perl 
since it can directly call 'rename' without spawning multiple 'mv' 
ls -l | grep 2005 | perl -ne 'chomp();$old=$_;s#^#somedir/#;rename$old,"$_"' 


Post a Comment

Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM