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.

Script for files

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 04-23-2018   -   Original Discussion by P V Ranga Rao
P V Ranga Rao's Unix or Linux Image
P V Ranga Rao P V Ranga Rao is offline
Registered User
Join Date: Apr 2018
Last Activity: 24 April 2018, 6:16 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Script for files

In a directory below tar files are there, the value between first and second delimiter(-) indicates
store name
i,e "ban", "can", "tan" etc are store names


i want to retain only one tar file per store in my current directory and move the remaining tar files to Temp directory
so in this case for stores "moc", "can" we have more than one tar file
so for stores "moc", "can" the tar file which got created first should be retained in current dicrectory and remaining
files should be moved to temp directory

Last edited by Scrutinizer; 04-23-2018 at 11:43 AM.. Reason: code tags
Sponsored Links
Old Unix and Linux 04-23-2018   -   Original Discussion by P V Ranga Rao
rdrtx1's Unix or Linux Image
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts

ls -1tr *.tar | awk -F- 'store[$2]++' | while read file ; do echo mv $file temp_directory ; done

test first. if desired files to move are shown, remove echo and run again.
Sponsored Links
Old Unix and Linux 04-23-2018   -   Original Discussion by P V Ranga Rao
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 22 June 2018, 3:55 PM EDT
Location: Aachen, Germany
Posts: 12,919
Thanks: 429
Thanked 3,968 Times in 3,649 Posts
Please be aware that NO two or more files with the same, identical name can exist an a directory. So the sample data given are not representative. Still, if the names' structure persists, rdrtx1's proposal will work as desired.
Sponsored Links

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
Shell script for field wise record count for different Files .csv files Kirands Shell Programming and Scripting 14 01-21-2016 04:06 PM
Read files in shell script code and run a C program on those files shoaibjameel123 Shell Programming and Scripting 2 09-17-2013 04:48 AM
need a shell script to extract the files from source file and check whether those files existonserve muraliinfy04 Shell Programming and Scripting 13 02-22-2012 07:14 AM
Need help creating a script to FTP files to a server and then delete the files that were transfered. jmalfhs Shell Programming and Scripting 4 01-18-2012 09:55 AM
How to retrieve all the linked script files/ctl files/sql files? franksubramania Shell Programming and Scripting 1 05-04-2009 01:47 PM

All times are GMT -4. The time now is 08:28 PM.