Sort all files in a directory retaining originals


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort all files in a directory retaining originals
# 1  
Old 03-19-2008
Sort all files in a directory retaining originals

Hi,

I need to sort all the files in a directory whilst retaining the originals. So if in the directory I have:

File1
File2
File3

I want to sort these files so that I would have:

File1
File1.sort
File2
File2.sort
File3
File3.sort

where I have added the .sort extension to show the sorted version.

I would use the command sort File1 > File1.sort to sort an individual file but how do I do it so that it sorts every file in the directory. I tried using sed but got in a bit of a mess.

Thanks!

-Gary
# 2  
Old 03-19-2008
You can do something like:

Code:
ls |grep -v .sort| while read file
do
  sort "$file" > "$file".sort
done

Regards
# 3  
Old 03-19-2008
Quote:
Originally Posted by Franklin52
You can do something like:

Code:
ls |grep -v .sort| while read file
do
  sort "$file" > "$file".sort
done

Regards
Thanks this is good! Is there a way to make it ignore directories and only attempt to sort files?
# 4  
Old 03-19-2008
Use find instead of ls:

Code:
find <dir> -type f -prune

The -prune option tells find to search only in the current directory.
You can use it if your find version don't support the maxdept option.

Regards
# 5  
Old 03-19-2008
Thanks!

Got it working the way I need now. Final version is

Code:
 
find * -prune -type f |grep -v .sort| while read file
do
sort "$file" > "$file".sort
done

I had to put the -prune option before the -type or it didn't seem to work on my system.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort files in directory numerically?

Trying to sort a bunch of files numerically but can't seem to get the command just right. This is in a IBM AIX machine. I have a directory that has... backup.bk1 backup.bk100 backup.bk2 backup.bk200 backup.bk3 backup.bk300 There are a lot more files but this is shortened for the... (5 Replies)
Discussion started by: c3rb3rus
5 Replies

2. UNIX for Beginners Questions & Answers

How do I custom sort the files in a directory using the filenames in a text file.?

Hi all, (5 Replies)
Discussion started by: KMusunuru
5 Replies

3. Shell Programming and Scripting

sed Command on Multiple Files and retaining the same file name

Hi All I am trying to run sed command to remove first 2 charcaters from a file on Multiple Files in my directory and what to retain the same file name . I want to know how to retain the same file name but with changes . Can some one please let me know how to proceed with this . ... (7 Replies)
Discussion started by: honey26
7 Replies

4. Shell Programming and Scripting

Joining two files by retaining the headers.

Hi Experts, I want to join two files(file1 ,file2) which are having tab separated values,by sorting them on key column(ID) and want to redirect the output to other file(output file) along with the headers from both the files. cat file1: ID Name phone_no 205 mno 90808 200 xyz 32003... (9 Replies)
Discussion started by: bharathbangalor
9 Replies

5. UNIX for Dummies Questions & Answers

Redirecting tmp files of SORT into different directory

Hey Guys, I am facing an annoying scenario, fewer times when I execute the sort command, it throws out on error saying that "No Space on available on /var/tmp/<temp file name>. May be it is set to /var/tmp directory. I was wondering, if I cant redirect the temporary file creation to any other... (3 Replies)
Discussion started by: abhisheksunkari
3 Replies

6. Shell Programming and Scripting

Zip files deleting originals

Hi guys, I have folder1, folder2 and file1, I want to compress them in a unique file with zip extension (Output.zip). I've been trying with zip -r Output folder1 folder2 file1 and does the job, but how can I compress them in zip format and removing the originals in the same command? ... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

7. UNIX for Dummies Questions & Answers

using gsed with cp to sort files in directory - every N file copy to new place

Hi all, I'm having a problem with some basic piping issues... I have been able to get in a directory and ls | gsed in order to list every N file for instance: ls | gsed -n '2~5p' The thing is I want to be able to copy the output files to a new directory. Basically directory /all has a... (4 Replies)
Discussion started by: dgoss
4 Replies

8. UNIX for Dummies Questions & Answers

Shell script which will sort all the files in a directory with the timestamp they were created

Team, Pls help writing a shell script which will sort all the files in a directory with the timestamp they were created. (like ls -lrt) (6 Replies)
Discussion started by: asappidi
6 Replies

9. Shell Programming and Scripting

How to sort a set of files by date in a directory?

hi there, I have a directory which contents I can parse dynamically. I end up with a file list. I then want to display those files sorted by date, oldest files first. I have very very little PERL experience...Would anyone know how to do that ? Thanks in advance. (8 Replies)
Discussion started by: alexf
8 Replies

10. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies
Login or Register to Ask a Question