Sorting files by size in another directory


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Sorting files by size in another directory
# 1  
Old 05-04-2014
Wrench Sorting files by size in another directory

1. The problem statement, all variables and given/known data:
I'm trying to use a directory path to enter a new directory and sort the files there. I'm using the language C with a system call in Unix to sort the files from smallest to largest.


2. Relevant commands, code, scripts, algorithms:
Code:
system("ls -al | sort -k5n >> tempfile.txt");

This command enters the sorted data into a temporary directory named tempfile.txt. Tempfile.txt is later deleted. But the command:
system("ls -al | sort -k5n");
sorts only the data in the current directory. I would like to be able to sort data in any directory entered into my program.



3. The attempts at a solution (include all code and scripts):
i've tried:
Code:
       sprintf(cmd, "cd %s", argv[PATH]);
       system(cmd);

to change directories before using the sort command. But this doesn't work. I do not know very much about the Unix language to create a command that changes directories and sorts the files it finds there.


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

University at Albany, Albany NY, United States, S.S. Ravi, CSI 402


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by vbe; 05-04-2014 at 05:30 AM..
# 2  
Old 05-04-2014
Each call to system() creates a separate shell execution environment. Once the commands given to a call to system() complete, that environment is gone.

So, if you call system two times:
Code:
    system("cd some_directory");
    system("ls -al | sort -k5n");

the first call will not affect the directory in which the second call is run. But, if you perform the cd and the ls and sort pipeline in the same call to system() as in:
Code:
    system("cd some_directory
        ls -al | sort -k5n");
                or
    system("cd some_directory; ls -al | sort -k5n");

then the cd command will affect the directory where the pipeline executes.

Do you want the primary sort key of your output to be the file size and the secondary sort key to be the file type? Would you prefer to sort the output with the primary sort key being the file size and the secondary sort key being the file name? Have you looked at all of the options that the ls utility accepts on your system? Do you need both ls and sort? Is there an option to ls that will sort the output by file size and file name instead of having the primary sort key be the file name? If that output is backwards, is there an option to reverse the order of the output produced by ls?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

2. UNIX for Dummies Questions & Answers

How to get the set of files size as a sum in a directory.

Hi, Can someone help me to get the complete files size (sum) over a perod time (1 day,2days)in a directory. eg: Directory :ABC I have a1,a2,a3 files are created in last 24 hours so I need to get the some of all these files. I am using the below find command but its giving me the... (1 Reply)
Discussion started by: gaddamja
1 Replies

3. Shell Programming and Scripting

Sorting files by size. No ls.

I've got an assignment where I've made a script that searched and found files that meet certain requirement. (Files that are a size that is from "x" to "y" in size and has a certain name). The script sends the output to a file that gathers the info. The problem is I'd like to sort what's... (3 Replies)
Discussion started by: BobbyTee
3 Replies

4. Shell Programming and Scripting

Getting the total file size for certain files per directory

Hi, I am trying to get the total file size for certain files per directory. I am using find /DirectoryPath -name '*.dta' -exec ls -l {} \; | awk '{ print $NF ": " $5 }' > /users/cergun/My\ Documents/dtafiles.txt but this lists all the files in the directories. I need the total... (9 Replies)
Discussion started by: cergun
9 Replies

5. UNIX for Dummies Questions & Answers

Cp files (>5 Mb size) from one directory to another

Hi All, I have a requirement like below, I want to transfer few file of size > 5 Mb from one directory to anotehr directory. Please let me know the command how can i do that Sorry if it looks silly Senthil (6 Replies)
Discussion started by: skcontact
6 Replies

6. UNIX for Dummies Questions & Answers

listing files in a directory in bases of size

Hi , I want to list all files in the order of size . Just want to know which files occupies more size and which occupies less size . Is it possible with ls command ? :) Thanks, Arun. (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

7. Shell Programming and Scripting

files of size 0 need to be deleted inside a directory

Hiiii, I have written a script which takes backup of some log files. let say the backuplocation is --- /abc/backuplocation -rw-r--r-- 1 webmut2 spgroup 0 Jan 27 02:41 ansrpt23994.log -rw-r--r-- 1 webmut2 spgroup 0 Jan 27 02:41 ansrpt3601.log -rw-r--r-- 1... (2 Replies)
Discussion started by: namishtiwari
2 Replies

8. UNIX for Dummies Questions & Answers

Sorting files in a directory

Hi guys, Probably an easy one, but how do you sort a directory so that the files come first, then subdirectories? ie ./dir1 has file 1 subdir 1 file 2 i need it to become file 1 file 2 subdir 1 as i'm using it in a script to pass each one through a for loop, and would like all... (3 Replies)
Discussion started by: olimiles
3 Replies

9. Shell Programming and Scripting

Sorting files in a directory

Hi guys, Probably an easy one, but how do you sort a directory so that the files come first, then subdirectories? ie ./dir1 contains file 1 subdir 1 file 2 i need it to become file 1 file 2 subdir 1 as i'm using it in a script to pass each one through a for loop, and would... (2 Replies)
Discussion started by: olimiles
2 Replies

10. Shell Programming and Scripting

How to check if 3 files have same size in directory

I need to determine if any three files have the same file size in a specified directly? I have got as far as listing the file sizes but where to go from here? ls -al |sort -n -r +4 | awq '{print $5}' Thanks in anticipation (5 Replies)
Discussion started by: oggle
5 Replies
Login or Register to Ask a Question