Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Sort all files in one folder

UNIX for Beginners Questions & Answers


Tags
for loop, korn shell, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-13-2018   -   Original Discussion by Eve
Eve's Unix or Linux Image
Eve Eve is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 8 May 2018, 10:21 AM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
Sort all files in one folder

Could it be possible to sort all of files in a folder and add z- to the name of the sorted file?
The names of the files to be sorted before the files are sorted



Code:
AC-FOUR-136-ZEL2-ZECO-111
AC-SEVEN-56-ZEL4-ZECO-68
AC-NINE-994-ZEL3-ZECO-811
AC-ONE-4-ZEL1-ZECO-544


The names of the files to be sorted after the files are sorted



Code:
z-AC-FOUR-136-ZEL2-ZECO-111
z-AC-SEVEN-56-ZEL4-ZECO-68
z-AC-NINE-994-ZEL3-ZECO-811
z-AC-ONE-4-ZEL1-ZECO-544

I'm using Windows 7 , Unix subsystems for Windows and C Shell

Thank you!
Sponsored Links
    #2  
Old Unix and Linux 03-13-2018   -   Original Discussion by Eve
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 21 May 2018, 5:26 AM EDT
Location: San Jose, CA, USA
Posts: 11,295
Thanks: 633
Thanked 3,932 Times in 3,365 Posts
Quote:
Originally Posted by Eve View Post
Could it be possible to sort all of files in a folder and add z- to the name of the sorted file?
The names of the files to be sorted before the files are sorted



Code:
AC-FOUR-136-ZEL2-ZECO-111
AC-SEVEN-56-ZEL4-ZECO-68
AC-NINE-994-ZEL3-ZECO-811
AC-ONE-4-ZEL1-ZECO-544


The names of the files to be sorted after the files are sorted



Code:
z-AC-FOUR-136-ZEL2-ZECO-111
z-AC-SEVEN-56-ZEL4-ZECO-68
z-AC-NINE-994-ZEL3-ZECO-811
z-AC-ONE-4-ZEL1-ZECO-544

I'm using Windows 7 , Unix subsystems for Windows and C Shell

Thank you!
I don't see any way of sorting filenames to get the order you have specified for the filenames to be processed, and I don't see why it matters which of the files are sorted first. If you just wanted to sort these four files (or all files whose filename starts with AC-), it would be a trivial task to do this using a standard shell.

Since you insist on using csh instead of bash or ksh or some other standard shell, I'll let someone else with more experience with more experience with csh syntax and semantics try to help you with a loop to do what you want.

It would help if you would show us what you have tried to solve this problem on your own. If you can code your own loop, the sort and mv or rm commands needed inside the loop won't vary much from shell to shell.
Sponsored Links
    #3  
Old Unix and Linux 03-14-2018   -   Original Discussion by Eve
Eve's Unix or Linux Image
Eve Eve is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 8 May 2018, 10:21 AM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
The filenames doesn't have to be sorted, only the files themselves need to be sorted and after sorting add z- to the beginning of the filename. It doesn't matter which of the files are sorted first. I have 2000 files in my folder. I only gave 4 examples of the filenames before.
    #4  
Old Unix and Linux 03-14-2018   -   Original Discussion by Eve
bakunin's Unix or Linux Image
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 May 2018, 8:39 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,763
Thanks: 112
Thanked 1,682 Times in 1,235 Posts
Quote:
Originally Posted by Eve View Post
Could it be possible to sort all of files in a folder and add z- to the name of the sorted file?
Add a "z-" to the filename is easy:



Code:
#! /bin/csh

foreach FILE in (*)
     mv "$FILE" z-"$FILE"
end

For this:


Code:
AC-FOUR-136-ZEL2-ZECO-111
AC-SEVEN-56-ZEL4-ZECO-68
AC-NINE-994-ZEL3-ZECO-811
AC-ONE-4-ZEL1-ZECO-544

Forgive me for being a bit slow, but what should these be sorted for? Sorting alphabetically would yield:



Code:
AC-FOUR-136-ZEL2-ZECO-111
AC-NINE-994-ZEL3-ZECO-811
AC-ONE-4-ZEL1-ZECO-544
AC-SEVEN-56-ZEL4-ZECO-68

or, sorting alphabetically for the part between the second and the third "-" would yield:



Code:
AC-FOUR-136-ZEL2-ZECO-111
AC-ONE-4-ZEL1-ZECO-544
AC-SEVEN-56-ZEL4-ZECO-68
AC-NINE-994-ZEL3-ZECO-811

whereas sorting numerically for the same part would yield:



Code:
AC-ONE-4-ZEL1-ZECO-544
AC-SEVEN-56-ZEL4-ZECO-68
AC-FOUR-136-ZEL2-ZECO-111
AC-NINE-994-ZEL3-ZECO-811

etc., etc.. So please tell us the sort criteria you want to apply.

I hope this helps.

bakunin

Last edited by bakunin; 03-14-2018 at 06:40 PM.. Reason: only now noticed csh instead of std shell
Sponsored Links
    #5  
Old Unix and Linux 03-16-2018   -   Original Discussion by Eve
Eve's Unix or Linux Image
Eve Eve is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 8 May 2018, 10:21 AM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
I recieved an error report when I tried to use your code:



Code:
% #! /bin/csh
#!: Command not found.
%
% foreach FILE in (*)
foreach: Words not parenthesized.
%      mv "$FILE" z-"$FILE"
FILE: Undefined variable.

I only need simple sorting, but I don't know how to sort 2000 files that are in one folder with one command

The content of one file before sorting


Code:
   3   88   99  543  876   988
   7   45   54   99  120   987
  13   23  167  334 2378  8765
  15   17   18 1125 2356  6765
  54   78   79   90  344  3399
 111  233  788  999 3421  7654
 223  299  388  455  477   566
   4    9   77  890  977  7655
  33  122  665  888  997   999
 228  332  339  453  988  1299
   5   35   84   98 1889  2300
   7    8   23  854 1276  3343
  45  443  556  887  889   987

The content of one file after sorting



Code:
   3   88   99  543  876   988
   4    9   77  890  977  7655
   5   35   84   98 1889  2300
   7    8   23  854 1276  3343
   7   45   54   99  120   987
  13   23  167  334 2378  8765
  15   17   18 1125 2356  6765
  33  122  665  888  997   999
  45  443  556  887  889   987
  54   78   79   90  344  3399
 111  233  788  999 3421  7654
 223  299  388  455  477   566
 228  332  339  453  988  1299

Sponsored Links
    #6  
Old Unix and Linux 03-16-2018   -   Original Discussion by Eve
rbatte1's Unix or Linux Image
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 21 May 2018, 11:17 AM EDT
Location: Lancashire, UK
Posts: 3,534
Thanks: 1,557
Thanked 692 Times in 622 Posts
So, you want to numerically sort the content of each file to create a new file with a prefix of z-. Is that correct?

You need a loop that uses all your file names and the processes them with something like:-

Code:
sort -bn "$inputfile" > "z-$inputfile"

I'm not so sure of the foreach syntax in csh. The flags -bn asks sort to ignore blank/whitespace and sort numerically - which I think is what you want.




Does this help?



Robin
Sponsored Links
    #7  
Old Unix and Linux 03-18-2018   -   Original Discussion by Eve
Eve's Unix or Linux Image
Eve Eve is offline
Registered User
 
Join Date: Feb 2018
Last Activity: 8 May 2018, 10:21 AM EDT
Posts: 19
Thanks: 3
Thanked 0 Times in 0 Posts
A reply to rbatte1. Yes, you have understood everything correctly. But I'm getting a error report in my C Shell window when I'm trying to use your code:


Code:
% sort -bn "$inputfile" > "z-$inputfile"
inputfile: Undefined variable.

What to do?
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
Shell scripting for moving folder specific files into target directory of that country folder. naresh2389 Shell Programming and Scripting 3 08-23-2017 07:51 AM
Bash script to sort files into folder according to a string in the filename ace47 Shell Programming and Scripting 1 01-31-2012 06:49 AM
URGENT!!! bash script to sort files into folder according to a string in the filename ace47 Shell Programming and Scripting 1 01-31-2012 06:04 AM
Help with sort folder results mrlayance Shell Programming and Scripting 0 07-21-2010 02:14 PM
how to sort the file in specified folder maximas Shell Programming and Scripting 0 10-07-2007 04:16 AM



All times are GMT -4. The time now is 04:34 PM.