Sort all files in one folder


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sort all files in one folder
# 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 07: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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

3. Shell Programming and Scripting

Bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

4. Shell Programming and Scripting

URGENT!!! bash script to sort files into folder according to a string in the filename

Hi all. I am very new to linux scripting and i have a task i can only solve with a script. I need to sort files base on the date string in their filenames and create a folder using the same date string then move the files to their respective folders. Scenario: Folder Path:... (1 Reply)
Discussion started by: ace47
1 Replies

5. Shell Programming and Scripting

Help with sort folder results

Here is the code, but the list is not sorted properly (alphabetically)? <?php function folderlist(){ $startdir = './'; $ignoredDirectory = '.'; $ignoredDirectory = '..'; if (is_dir($startdir)){ if ($dh = opendir($startdir)){ while (($folder = readdir($dh)) !== false){ if... (0 Replies)
Discussion started by: mrlayance
0 Replies

6. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

7. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

8. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. Shell Programming and Scripting

how to sort the file in specified folder

hi im having soo many .top files and i have to sort a set of files in one folder like tat so many sets of folders have to be created ... like say i have the file name j1 , g2 ,s3,k4,h1,j2,k3,s4..And i have to create one folder in tat i to have copy the files j1, g2 ,s3,k4 in one folder and... (0 Replies)
Discussion started by: maximas
0 Replies
Login or Register to Ask a Question