I want to display directory listing in the order of size. I do not have -S option in my version of UNIX. So I wrote a simple script. But it takes "| sort -n -k5,5" part as file names. Any suggestion?
Firstly, I would add the -b flag to ignore leading blanks, just in case. You also need to beware that LDAP supported group names can contain spaces, so that throws things out of what as to what is the fifth field.
As you your problem, I would try splitting up your command more like this:-
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:
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:
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?
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:
you now have a file awkwardly named ls -l foo in your current directory. Now execute this:
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:
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:
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)
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)
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)
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)
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)
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)
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)
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)