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

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

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.

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.

Add a "z-" to the filename is easy:

#! /bin/csh

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

For this:

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:

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:

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:

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

I recieved an error report when I tried to use your 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

   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

   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

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:-

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

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:

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

What to do?

What's inputfile 's contents? Does that file exist?

If you were using a standards-conforming shell (such as bash or ksh ), you could use something like:

for inputfile in AC-*
do	sort -n -o z-"$inputfile" "$inputfile" && rm "$inputfile"
done

Since you choose to use csh , it is up to you to translate this code to create a loop in csh and to only run the rm command after if verifies that the sort command completed successfully.

I don't have a file called inputfile. I tried to install bash to my pc but I didn't succeed, so I have no other option to use C Shell and Unix subsystems for Windows. Could somebody help me to make the codes that have been offered in this thread before suitable for C Shell?

What output do you get when you run the command:

whereis sh bash csh ksh

in csh ?

Perhaps not, but you do *have* some (file that serves as) inputfile, no? It makes sense to call a variable holding the name of it "inputfile". Corollary advice: when the navigation system in your car tells you to "turn right" it doesn't mean you should rotate clockwise on your seat either.

bash is part of the SUA, so it should be already there when you install it (correctly). I suggest you get the installation of the SUA right first and only then start to worry about C shell - if it is still necessary at all, that is. Doing something wrong first and then patch it somehow to almost work is a sure way to disaster in the long run anyway, so it is better to correct that now better than later.

I hope this heps.

bakunin

I would like to make the question simpler - how to simply sort all of the files in one folder. Just like

sort  > 

I have 6000 files in that folder and I would like to know if it is possible to sort all of them with one command.

A reply to Don Cragun

If I will run

whereis sh bash csh ksh

I will get

whereis: Command not found.

OK. What output do you get from the commands:

echo "PATH=$PATH"
ls /bin/*sh /usr/bin/*sh

This simpler question of yours has been answered in post#9, albeit in bourne shell "language". You even had an attempt of a solution to a similar problem in csh "speak" in post#4.
You insist on a full blown csh solution (for not being able to use that bourne one) for two weeks now - chances are dwindling you'll get one unless you translate / create it yourself.

A reply to Don Cragun

echo "PATH=$PATH"   gives

PATH=/bin:/opt/gcc.3.3/bin:/usr/contrib/bin:/usr/X11R6/bin:/usr/local/bin:/usr/c
ontrib/win32/bin

ls /bin/*sh /usr/bin/*sh  gives

/bin/chsh       /bin/rsh        /usr/bin/chsh   /usr/bin/rsh
/bin/csh        /bin/sh         /usr/bin/csh    /usr/bin/sh
/bin/ksh        /bin/tcsh       /usr/bin/ksh    /usr/bin/tcsh

I can't understand why you steadfastly insist on using the C-shell. From your output:

/bin/csh
/usr/bin/csh      # these two are C-shells

/bin/sh
/usr/bin/sh       # these two are Bourne-shells, the ancestor of both bash and ksh

/bin/ksh
/usr/bin/ksh      # these are Korn-shells, the preferable tool

/bin/tcsh         # these are Tenex-C-shells, a more modern and matured
/usr/bin/tcsh     # (but unfortunately non-standard) version of the C-shell

You could run most of the scripts suggested to you in ksh as well as in bash and even if you would insist on C-shell you should use tcsh instead of the original csh.

I hope this helps.

bakunin

2 Likes

As bakunin has already implied, if you run the code I suggested in post #9 in this thread with ksh it should do exactly what you said you want to do. (You might note that in that post I said it would work with either bash or ksh .)

Depending on the vintage of sh on your system, that code might also work if you run it with sh or even rsh .

1 Like

Hi! Thank you for your help! I really didn't know that I have the necessary shells in my pc since I'm relatively unexperienced. Now that I've found them evetything works great! Solution offered in post #9 works great in ksh! Thank you Don Cragun! This thread can be marked solved!