Directory listing in the order of size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Directory listing in the order of size
# 8  
Old 03-04-2017
Quote:
Originally Posted by bakunin
[..]
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
Hi Bakunin,

This is not correct.

The assignment:
Code:
cmd='ls -l foo'

Assigns the string ls -l foo to variable cmd.

During assignment the single quotes are discarded.

Quote:
Each variable assignment shall be expanded for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal prior to assigning the value.
Shell command: Simple commands

and
Quote:
The quote characters ( <backslash>, single-quote, and double-quote) that were present in the original word shall be removed unless they have themselves been quoted.
Word Expansions: Quote Removal


If we execute the command
Code:
$cmd

Then, because of the unprotected variable expansion $cmd, the string contained in variable cmd gets field-split.

If the IFS is set to its default value of a space, a TAB and newline (denoted as $' \t\n' in some shells) , then this results in a command ls and two parameters, -l and foo

Last edited by Scrutinizer; 03-04-2017 at 04:06 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
Login or Register to Ask a Question