Home Man
Search
Today's Posts
Register

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

Tags
for loop, korn shell, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 03-13-2018
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!
# 2  
Old 03-13-2018
Quote:
Originally Posted by Eve
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.
# 3  
Old 03-14-2018
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 03-14-2018
Quote:
Originally Posted by Eve
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
# 5  
Old 03-16-2018
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

# 6  
Old 03-16-2018
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
# 7  
Old 03-18-2018
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?
Login to Reply

« Previous Thread | Next 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
Request for Shell script to move files from Subfolder to Parent folder and delete sub folder srivarun15 Shell Programming and Scripting 6 06-13-2018 03:24 PM
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
How can i sort this listing in PYTHON by folder creation? ZerO13 Shell Programming and Scripting 2 01-19-2017 10:01 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
Find all text files in folder and then copy to a new folder cgkmal Shell Programming and Scripting 4 06-20-2009 01:12 PM
check how many files in folder or total files in folder durgaprasad Shell Programming and Scripting 3 06-16-2009 03:26 AM
Auto copy for files from folder to folder upon instant writing Bashar UNIX for Advanced & Expert Users 2 08-21-2008 02:44 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 05:30 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password