sorting files or folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting files or folders
# 1  
Old 11-22-2010
sorting files or folders

I have the small script to arrange files of a descending way.
Code:
ls -l |sort -r -k4

i wanted for example if I place -d one arranges only the folders or -a to arranges only the files.

Cheers

Last edited by Scott; 11-24-2010 at 02:40 AM.. Reason: Code tags
# 2  
Old 11-22-2010
Code:
ls -l | grep -v '^total ' | if [ "$1" = "-d" ]
then
 grep '^d'
else
 grep -v '^d'
fi | sort -r -k4

# 3  
Old 11-23-2010
hi When I execute the script the following mistake goes out.
Code:
root@ubuntu:~# sh scrph.cap
scrph.cap: 6: ls: not found
scrph.cap: 6:  grep: not found


Last edited by Scott; 11-24-2010 at 02:40 AM..
# 4  
Old 11-23-2010
Sort for files.

Code:
find . -type f -ls |sort -r -k4

Sort for folders.

Code:
find . -type d -ls |sort -r -k4

Maybe you need adjust the sort column.
# 5  
Old 11-23-2010
Quote:
Originally Posted by DGPickett
Code:
ls -l | grep -v '^total ' | if [ "$1" = "-d" ]
then
 grep '^d'
else
 grep -v '^d'
fi | sort -r -k4

For example when i execute the command sh scriptejemplo.cap -d /home one arranges only the folders.

sh scriptejemplo.cap -a /home one arranges only the files. of the directory home.
# 6  
Old 11-23-2010
You should move past calling "sh scriptname args" to putting a first line of "$#/bin/sh" (or wherever) in your script and "chmod u+x scriptname". See man 2 exec. Then, you just call "scriptname args".

You might move on to ksh, too, as sh has real limitations and ksh offers a proper superset of the sh commands and such. ksh93 is free. ubuntu should come with at least pdksh, which is not the real deal but better than sh!

If a script says "'ls' is not known", it is saying your $PATH is not exported so it could not find it in /usr/bin/ls or /bin/ls or wherever. Just setting an variable only affects it in the memory of the current instance of sh. In sh you must:
Code:
PATH=dir1:dir2:dir3 ; export PATH

but in ksh you can say it more cleanly:
Code:
export PATH=dir1:dir2:dir3

A directory can have thing other than files and dirs in it, like devices, symbolic links and named pipes, so my code does dirs or everything else.
# 7  
Old 11-24-2010
i have changed script
Code:
#!bin/bash
cd $1
ls -lS $1

I can to see the files for descending size /miscript.sh /root for example.
How i can to place at the end of the command, if I place -d one arranges only the folders or -a to arranges only the files.
Code:
./miscript.sh /root -d Folders
./miscript.sh /root -a Documents


Last edited by Scott; 11-24-2010 at 06:21 PM.. Reason: Code tags
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 copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

2. Shell Programming and Scripting

CAT 3 files with the same name in 3 different folders

I am trying to cat 3 files. They all have the same name MFExtract.txt. But, they are in 3 seperate file names. Any idea how I could cat the 3 together. It's throwing me off figuring out a way because the names are the same. I was thinking just doing a 3 cat commands with the full path names and... (2 Replies)
Discussion started by: risarose87
2 Replies

3. UNIX for Dummies Questions & Answers

Listing folders and files within

is there any command that can make listing files like this /data/seismic/prestack-4/eon5/PEP/JAWA/AKASIA-BAGUS/3D/F/BL3-4/F12AKB3D_SW82-128_ID1696-1850.segy /data/seismic/prestack-4/eon5/PEP/JAWA/AKASIA-BAGUS/3D/F/BL3-4/F12AKB3D_SW82-128_ID1851-1975.segy ... (2 Replies)
Discussion started by: muhnandap
2 Replies

4. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

5. Shell Programming and Scripting

List all the files in the present path and Folders and subfolders files also

Hi, I need a script/command to list out all the files in current path and also the files in folder and subfolders. Ex: My files are like below $ ls -lrt total 8 -rw-r--r-- 1 abc users 419 May 25 10:27 abcd.xml drwxr-xr-x 3 abc users 4096 May 25 10:28 TEST $ Under TEST, there are... (2 Replies)
Discussion started by: divya bandipotu
2 Replies

6. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

7. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

8. UNIX for Dummies Questions & Answers

Sorting data and place them in different folders

Hello Unix gurus, I am new to Unix. I am working on some dummy project which involves unix scripting. I have a query : There is a file Number.dat which is of form say AAA|123|4563|animal AAA|1234|45634|animal2 BBB|123444|456312|bird BBB|123445|456313|bird2 Here AAA,BBB are... (29 Replies)
Discussion started by: Vinaykumar1
29 Replies

9. HP-UX

to get the timestamp of files from the files and folders in Unix

Hi, I had a directory and many subdirectories and files with in it. Now i want to get the timestamp of files from the files and folders recursively. :( Please help me to generate a script fort he above mentioned requirement! Appreciate for ur qick response Thanks in advance! ... (2 Replies)
Discussion started by: kishan
2 Replies

10. Shell Programming and Scripting

removing old files except configuration files and folders

Dear all, I want to remove files older than 2 months in the /home/member directory. But except the configuration files (like .bash_profile .config/ .openoffice/ .local/ .kde/ etc..) I have tried with the command find . -mtime +60 -wholename './.*' -prune -o -print -exec mv {} \; but it... (1 Reply)
Discussion started by: jamcalicut
1 Replies
Login or Register to Ask a Question