Sponsored Content
Top Forums Shell Programming and Scripting Directory listing in the order of size Post 302992890 by bakunin on Friday 3rd of March 2017 05:33:26 AM
Old 03-03-2017
Quote:
Originally Posted by Soham
But I am surprised why in my version it takes sort part as file names.
The reason is the way the shell parses its input. We tend to implicitly take for granted that spaces (or other whitespace) separate parts of the shells input (the "command line"), but that is not necessarily so. In fact the shell knows a character called "IFS" (the "internal field separator"), which is by default a blank but could be redefined to any other character:

Code:
$ echo abc:def:ghi | IFS=':' read field1 field2 field3
$ echo $field1 $field2 $field3

So we have an "internal field separator" and it is used to separate - fields. Because it is normally a blank the shell looks at a normal command line like this:

Code:
|| |-| |------| |------|
ls -la /bla/foo /foo/bar

The shell sees four fields, separated by the IFS character. The first field (ls)is taken - in absence of anything else - as a command. The next part (-la) is taken as the first argument and, because it is introduced by a hyphen and ls accepts options - it is interpreted as options. (Notice that, because of how getopts works, "-l-a", "-l -a" and "-la" work the same.) As the next argument comes (because getopts removes all options from the command line it is in fact the first) it is interpretd as argument to ls and so on.

This process is called "field splitting" and the shell does this to every (command) line it interpretes.

Now, what would happen if we slightly alter the command?
Code:
|| |-| |-----------------|
ls -la "/bla/foo /foo/bar"

the string /bla/foo /foo/bar would no longer be split because by enclosing it into quotes (in this case double and single quotes work the same) we told the shell to forego this field splitting process. It will therefore look for a directory /bla/foo /foo/bar and - if there is one - it will perform the ls -la command on it.

Now back to your original line:

cmd='ls -l *.TXT'
$cmd


With the first line (the single quotes) we told the shell to switch off field splitting for the string, so it is ONE string: ls -l *.TXT. This "holds" for exactly one evaluation of the string, therefore a command named ls -l *.TXT (literally!) is searched for - but perhaps not found.

You can try for yourself, execute this in your home dir:

Code:
cat - > ./"ls -l foo" <<EOF
#! /bin/sh

echo "here is the awkward command"
exit 0
EOF
chmod 754 "./ls -l foo"

you now have a file awkwardly named ls -l foo in your current directory. Now execute this:

Code:
cmd='ls -l foo'
./$cmd

And you will see that in fact this script is called. This in turn means that the whole string, including the whitespace, was intepreted as one field. Remove this script with the command rm "./ls -l foo".

This is all because the quoting protected the string from being subjugated to the field splitting process for one time. Notice, that if the string is interpreted a second time, this protection wears off:

Code:
cmd='ls -l foo'
eval $cmd

eval is a keyword which restarts the whole parsing process (and hence the field splitting) of the shell when reading this line. The line is interpreted twice and because of this the field splitting takes place (but only on the second pass, as some more intricate examples will show you - you may want to experiment and find out how to prevent/enforce this).

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

list file's by size order in sepecfied directory and sub directories

How do I list files of type "*.file" for example by size order recursively ? (2 Replies)
Discussion started by: ferretman
2 Replies

2. UNIX for Dummies Questions & Answers

Recursive directory listing without listing files

Does any one know how to get a recursive directory listing in long format (showing owner, group, permission etc) without listing the files contained in the directories. The following command also shows the files but I only want to see the directories. ls -lrtR * (4 Replies)
Discussion started by: psingh
4 Replies

3. Shell Programming and Scripting

Listing files in numerical order

Hi, I'm trying to write a ksh script to copy a specified number of files from one directory to another. The files are named in the convention <switchname>_log.<num> and the numbers are sequential single digit onwards. I figured I could find some parameter for ls which would list the files in... (3 Replies)
Discussion started by: Steve_H
3 Replies

4. UNIX for Dummies Questions & Answers

order by size

Hi all, I want to do a command that list all files from a directory e subdirectories order by size (MAX to MIN) Can you help me? Tkz (2 Replies)
Discussion started by: sliver
2 Replies

5. 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

6. Shell Programming and Scripting

Listing of all the files in the order of last update (including those in the subdiret

I want to list all the files in the current directory including those in the subdirectories as well as a single lot in the order of last updated. (Not as separate list given by ls -lRt). Any suggestion? Thanks (4 Replies)
Discussion started by: Soham
4 Replies

7. Shell Programming and Scripting

Just listing size, timestamp & name of files in a directory

How can I list the files in a directory and just show the file size, date stamp, timestamp and file name.. I've been trying to ls -lrt the directory to a file and then use the cut command but I'm not having any luck with getting the proper results.. I thought i could use the -f switch and count... (4 Replies)
Discussion started by: Jazmania
4 Replies

8. 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

9. Red Hat

Listing all child pid and deleting it in reverse order

Hi , My problem is that I am not able to list all process id of any process. If you see pstree command it shows many process id under https. But if I run ps command its not listing all the process id for httpd. It is just listing the PPID and immediate child process id only. I... (4 Replies)
Discussion started by: pratapsingh
4 Replies

10. Shell Programming and Scripting

File listing in Ascending order

I have a multiple file with the following name like. file_0.csv file_1.csv file_2.csv file_3.csv file_4.csv file_5.csv file_6.csv file_7.csv file_7.csv file_8.csv file_9.csv file_10.csv file_11.csv file_12.csv file_13.csv file_14.csv (2 Replies)
Discussion started by: rakesh_arxmind
2 Replies
All times are GMT -4. The time now is 05:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy